_javascript skill of common sort algorithm in JS

Source: Internet
Author: User

This article for everyone to share the JS implementation of the common sorting algorithm, the specific contents are as follows

1. Bubble sort

 var bubblesort = function (arr) {
 var flag = true;
 var len = arr.length;
 for (var i = 0; i < len-1; i++) {
  flag = true;
  for (var j = 0; J < len-1-I; j + +) {
   if (Arr[j] > arr[j + 1]) {
    var temp = arr[j+1];
    ARR[J+1] = arr[j];
    ARR[J] = temp;
    Flag = false;
   }
  }
  if (flag) {break;}}}
; 

2. Select sort

 var selectsort = function (arr) {
 var min;
 for (var i = 0; i < arr.length-1; i++) {
  min = i;
  for (var j = i + 1; j < Arr.length; J + +) {
   if (Arr[min] > Arr[j]) {
    min = j;
   }
  }
  if (i!= min) {
   swap (arr, I, Min);}}}
;
function swap (arr, index1, index2) {
 var temp = arr[index1];
 ARR[INDEX1] = Arr[index2];
 ARR[INDEX2] = temp;

3. Insert Sort

 var insertsort = function (arr) {
 var len = arr.length, key;
 for (var i = 1; i < Len; i++) {
  var j = i;
  key = Arr[j];
  while (--j >-1) {
   if (Arr[j] > key) {
    Arr[j + 1] = Arr[j];
   } else {break
    ;
   }
  } Arr[j + 1] = key;
 }
; 

4. Hill sort

 var shellsort = function (arr) {
 var gaps = [5, 3, 1];
 for (var g = 0; g < gaps.length; ++g) {for
  (var i = gaps[g]; i < arr.length; ++i) {
   var temp = Arr[i];
   for (var j = i; J >= Gaps[g] && arr[j-gaps[g]] > Temp. J-= Gaps[g]) {
    Arr[j] = arr[j-gaps[g]];< c25/>}
   Arr[j] = temp;
  }
 }
; 

5. Merge sort

 function MergeSort (arr) {if (Arr.length < 2) {return;
 var step = 1;
 var left, right;
  while (step < arr.length) {left = 0;
  right = step;
   while (right + step <= arr.length) {mergearrays (arr, left, left + step, right, right + step);
   left = right + step;
  right = left + step;
  } if (right < Arr.length) {mergearrays (arr, left, left + step, right, arr.length);
 Step *= 2; } function Mergearrays (arr, Startleft, Stopleft, Startright, stopright) {var Rightarr = new Array (stopright-startrig
 HT + 1);
 var Leftarr = new Array (stopleft-startleft + 1);
 K = Startright;
  for (var i = 0; i < (rightarr.length-1); ++i) {rightarr[i] = arr[k];
 ++k;
 } k = Startleft;
  for (var i = 0; i < (leftarr.length-1); ++i) {leftarr[i] = arr[k];
 ++k; } rightarr[rightarr.length-1] = Infinity; Sentinel value leftarr[leftarr.length-1] = Infinity;
 Sentinel value var m = 0;
 var n = 0; for (var k = Startleft K < stopright; ++k) {if (leftarr[m] <= Rightarr[n]) {arr[k] = leftarr[m];
  m++;
   else {Arr[k] = Rightarr[n];
  n++; 
 }
 }
}

6. Quick Sort

 var quickSort = function (arr, left, right) {
 var i, J, T, Pivot;
 if (left >= right) {return
  ;
 }
 Pivot = Arr[left];
 i = left;
 j = right;
 while (I!= j) {while
  (Arr[j] >= pivot && i < j) {
   j--;
  }
  while (Arr[i] <= pivot && i < j) {
   i++;
  }
  if (I < j) {
   t = arr[i];
   Arr[i] = arr[j];
   ARR[J] = t;
  }
 }
 Arr[left] = arr[j];
 ARR[J] = pivot;
 QuickSort (arr, left, i-1);
 QuickSort (arr, i + 1, right);
} 

Summary: Algorithm efficiency comparison:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.