JS Common bubble Sort, quick sort, insert sort code sharing

Source: Internet
Author: User

JS Common bubble sort, quick sort, insert sort code sharing

4.2.1 Bubble Sort Algorithm Introduction parsing: Compare adjacent two elements, if the previous one is larger than the latter, swap position.
In the first round, the last element should be the largest one.

Follow the steps one method to compare the adjacent two elements, this time because the last element is already the largest, so the last element does not have to compare. JS Code implementation function Bubble_sort (arr) {for (Var i=0;i<arr.length-1;i++) {for (Var j=0;j<arr.length-i-1;j++) {if (Arr[j]>arr[j+1])
        {var swap=arr[j];
        ARR[J]=ARR[J+1];
      Arr[j+1]=swap;
'}}} var arr=[3,1,5,7,2,4,9,6,10,8];
Bubble_sort (arr);
Console.log (arr); 4.2.2 Fast Sort JS Code implementation resolution: A quick sort is an improvement to the bubble sort, which divides the data into two parts, one part smaller than the other.
Then recursive invocation, on both sides of the implementation of a quick sort.
  function Quick_sort (arr) {if (arr.length<=1) {return arr;
  } var Pivotindex=math.floor (ARR.LENGTH/2);

  var pivot=arr.splice (pivotindex,1) [0];
  var left=[];
  var right=[];
    for (Var i=0;i<arr.length;i++) {if (Arr[i]<pivot) {Left.push (arr[i]);
    }else{Right.push (Arr[i]);
} return Quick_sort (left). Concat ([Pivot],quick_sort (right));
} var arr=[5,6,2,1,3,8,7,1,2,3,4,7];
Console.log (Quick_sort (arr)); 4.2.3 Insert Sorting Algorithm introduction parsing: Starting with the first element, theVegetarian can think that has been sorted out the next element, scan from backward forward in a sorted sequence of elements if the element (sorted) is greater than the new element, move the element to the next position repeat step 3 until you find the sorted element less than or equal to the new element repeat step 2 in the next location
  JS Code implementation function Insert_sort (arr) {var i=1, j,key,len=arr.length;
    for (; i<len;i++) {var j=i;
    var key=arr[j];
      while (--j>-1) {if (Arr[j]>key) {arr[j+1]=arr[j];
      }else{break;
  }} Arr[j+1]=key;
return arr;

} insert_sort ([2,34,54,2,5,1,7]); Author: Geek Tutorial Source: Nuggets copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.

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.