JavaScript Novice Learning Notes 3--three sort methods (bubble sort, Insert sort, Quick sort)

Source: Internet
Author: User

Each programming linguistics to the array, will talk about the sorting algorithm, when learning C language, card in the sorting algorithm. today, Let's summarize how three sorting algorithms are implemented in Javascript.

1. Bubble sort (default Ascending order Oh)

 Principle:

The principle of bubble sort, as the name implies, is the decimal to go up, large number toward Sinking. Starting with the first number, if it is larger than the second number, swap the position, then compare the size with the third number, swap the position, and so On.

For example, there is an array [2,4,3,5,1]

The first cycle: 2<4 does not exchange, 4>3 exchange, 4<5 does not exchange, 5>1 exchange, So the result is [2,3,4,1,5];

Second cycle: 2<3,3<4 does not exchange, 4>1 exchange, 4<5 not exchange, So the result is [2,3,1,4,5]

Similarly:

Third Cycle: The result is [2,1,3,4,5]

Fourth cycle: The result is [1,2,3,4,5]

 JavaScript code implementation:

functionbubblesort (arr) { for(varr=1;r<arr.length-1;r++){         for(vari=0;i<arr.length-r;i++){            if(arr[i]>arr[i+1]){                vartemp=arr[i]; arr[i]=arr[i+1]; Arr[i+1]=temp; }        }    }    returnarr;}vararr=[2,4,3,1,5];arr=bubblesort (arr); console.log (arr);//[1,2,3,4,5]

2. Insert Sort (default Ascending)

Principle:

The default first number is the smallest, then the second number is removed for insertion, and if the second number is less than the first digit, the first digit is shifted one bit, and the second number is placed in the first Position. Then take out the third number, starting at the first position and comparing the insert until the last Number.

Example: there is an array [2,4,3,5,1]

First Insert: 2<4 not inserted, The result is [2,4,3,5,1]

Second insert: 3>2,2 does not move back, 3<4,4 one bit after, 3 inserted, The result is [2,3,4,5,1]

Similarly:

Third Insert: The result is [2,3,4,5,1]

Fourth Insert: result is [1,2,3,4,5]

  JavaScript code implementation:

 var  arr=[2,4,3,5,1];  //  insert sort  function   insertsort (arr) { for  (var  i=1;i<arr.length;i++) {         var  t=arr[i];         var  p=i-1;  while  (t<arr[p]&&p>=0 +1]=arr[p];        P --;    } arr[p  +1]=t; }}  insertsort (arr); console.log (arr);//[1,2,3,4,5]  

3. Quick Sort (default Ascending order)

Principle:

The elements in the middle of the array, larger than the intermediate elements, are placed in the right array, less than the intermediate elements placed in the left array, one recursion at a time, until the elements in each left and right are empty or only one element is Stopped. The final concatenation of the Subarray can Be.

Example: an existing array [2,4,3,5,1]

First time sort: left=[2,1] middle=[3] righ t=[4,5]

Second order: left.left=[] left.middle=[1] left.right=[2] middle=[3] right.left=[4] right.middle=[5] right.right=[]

Stitching can be: [1,2,3,4,5]

 JavaScript code implementation:

vararr=[2,4,3,5,1];functionquickSort (arr) {if(arr.length<=1){        returnarr; }    Else{        varC=arr.splice (math.floor (arr.length/2), 1) [0];varleft=[],right=[];  for(vari=0;i<arr.length;i++){            if(arr[i]>C) {right.push (arr[i])}Else{left.push (arr[i])}}returnquickSort (left). concat (c,quicksort (right)); }}arr=quickSort (arr); console.log (arr);

  Three sorting algorithms as above, if compared, the complexity of the bubble sort is o (n^2), the insertion sorting complexity is O (1), the fast sorting complexity is O (n*log (n)).

The algorithm tells if it is wrong, hope to criticize ~

JavaScript Novice Learning Notes 3--three sort methods (bubble sort, Insert sort, Quick sort)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.