Js implements Common sorting algorithms and js sorting algorithms

Source: Internet
Author: User

Js implements Common sorting algorithms and js sorting algorithms

This article shares with you how to implement common sorting algorithms in js. The specific content is as follows:

1. Bubble Sorting

 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 sorting

 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 sorting

 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]];   }   arr[j] = temp;  } }}; 

5. Merge Sorting

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-startRight + 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; // The Sentinel value leftArr [leftArr. length-1] = Infinity; // The 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 sorting

 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 all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.