Several common sorting algorithms in JavaScript summary _javascript skills

Source: Internet
Author: User
Tags array length
Description
Writing this is mainly to exercise their own, and no practical significance.
Each browser test results in a different data. For example, I use the chrome test General fast sort will be fastest, IE is based on the length of the array is likely to hill the fastest.
Don't test bubble sort with too much data (browser crashes I don't care)
Download the test page if you are interested

Personal Understanding

Bubble sort: Simplest, also slowest, seemingly length less than 7 optimal
Insert sort: Faster than bubbling, slower than quick sort and hill sort, and smaller data has the advantage
Quick sort: This is a very quick sort of way, V8 's sort method uses a combination of quick sort and insert sorting
Hill sort: In non-chrome array length less than 1000, hill sort faster than fast
System method: This method of Forfox system is very fast

algorithm Source
Copy Code code as follows:

----------some sort algorithms
JS uses sort for sorting
Systemsort:function (Array) {
Return Array.Sort (function (A, b) {
return a-b;
});
},
Bubble sort
Bubblesort:function (Array) {
var i = 0, Len = array.length,
J, D;
for (; i<len; i++) {
For (j=0 j<len; j + +) {
if (Array[i] < array[j]) {
d = array[j];
ARRAY[J] = Array[i];
Array[i] = D;
}
}
}
return array;
},
Quick Sort
Quicksort:function (Array) {
var array = [8,4,6,2,7,9,3,5,74,5];
var array = [0,1,2,44,4,324,5,65,6,6,34,4,5,6,2,43,5,6,62,43,5,1,4,51,56,76,7,7,2,1,45,4,6,7];
var i = 0;
var j = array.length-1;
var Sort = function (i, j) {
End condition
if (i = = j) {return};
var key = Array[i];
var Stepi = i; Record start position
var stepj = j; Record End Position
while (J > i) {
J <<--------------look forward
if (Array[j] >= key) {
j--;
}else{
Array[i] = Array[j]
i++------------>> Look Back
while (J > ++i) {
if (Array[i] > key) {
ARRAY[J] = Array[i];
Break
}
}
}
}
If the first key to remove is the smallest number
if (Stepi = = i) {
Sort (++i, STEPJ);
return;
}
Last vacancy left to key
Array[i] = key;
Recursion
Sort (Stepi, i);
Sort (J, STEPJ);
}
Sort (i, j);
return array;
},
Insert Sort
Insertsort:function (Array) {
http://baike.baidu.com/image/d57e99942da24e5dd21b7080
Http://baike.baidu.com/view/396887.htm
var array = [0,1,2,44,4,324,5,65,6,6,34,4,5,6,2,43,5,6,62,43,5,1,4,51,56,76,7,7,2,1,45,4,6,7];
var i = 1, J, step, Key,
len = Array.Length;
for (; i < Len; i++) {
Step = j = i;
key = Array[j];
while (--j >-1) {
if (Array[j] > key) {
ARRAY[J+1] = Array[j];
}else{
Break
}
}
ARRAY[J+1] = key;
}
return array;
},
Hill sort
Jun.array.shellSort (JUN.ARRAY.DF (10000));
Shellsort:function (Array) {
Http://zh.wikipedia.org/zh/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F
var array = [13,14,94,33,82,25,59,94,65,23,45,27,73,25,39,10];
var Steparr = [1750, 701, 301, 132, 57, 23, 10, 4, 1]; Reverse () See this optimal step size array on the wiki
var Steparr = [1031612713, 217378076, 45806244, 9651787, 2034035, 428481, 90358, 19001, 4025, 836, 182, 34, 9, 1]//for large arrays The Step selection
var i = 0;
var steparrlength = steparr.length;
var len = array.length;
var len2 = parseint (LEN/2);
for (; i < steparrlength; i++) {
if (Steparr[i] > Len2) {
Continue
}
Stepsort (Steparr[i]);
}
Sort one Step
function Stepsort (step) {
Console.log (step) statistics for steps used
var i = 0, j = 0, F, tem, key;
var Steplen = len%step > 0? parseint (len/step) + 1:len/step;

for (; i < step; i++) {//Loop columns sequentially
For (J=1;/*j < Steplen && */step * j + i < Len J + +) {//loops each row of each column sequentially
TEM = F = Step * j + i;
key = Array[f];
while ((Tem-=step) >= 0) {//Search up in turn
if (Array[tem] > key) {
Array[tem+step] = Array[tem];
}else{
Break
}
}
Array[tem + step] = key;
}
}
}
return array;
}

Test Code package download

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.