Description
This is mainly to exercise yourself and has no practical significance.
The data tested by each browser is different. For example, if I use chrome for testing, the fast sorting will usually be the fastest, and IE may be the fastest in the shile according to the array length.
Do not use too much data to test Bubble Sorting (I don't care if the browser crashes)
If you are interested, download the test page.
Personal Understanding
Bubble Sorting: the simplest and slowest. It seems that the length is smaller than 7.
Insert sorting: It is faster than bubble, and slower than fast sorting and Hill sorting. Smaller data has advantages.
Fast sorting: This is a very fast sorting method. the V8 sort method uses the combination of fast sorting and insert sorting.
Hill sorting: in non-chrome, the array length is less than 1000, and the hill sorting is faster
System Method: The system method in forfox is very fast.
Algorithm source code
Copy codeThe Code is as follows:
// ---------- Some sorting 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 sorting
QuickSort: function (array ){
// Var array = [,];
// Var array = [4,324, 45, 4, 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; // the start position of the record
Var stepj = j; // record end position
While (j> I ){
// J <-------------- forward lookup
If (array [j]> = key ){
J --;
} Else {
Array [I] = array [j]
// I ++ ------------> Search backward
While (j> ++ I ){
If (array [I]> key ){
Array [j] = array [I];
Break;
}
}
}
}
// If the first retrieved key is the smallest number
If (stepi = I ){
Sort (++ I, stepj );
Return;
}
// Leave the last blank space to the key
Array [I] = key;
// Recursion
Sort (stepi, I );
Sort (j, stepj );
}
Sort (I, j );
Return array;
},
// Insert sorting
InsertSort: function (array ){
// Http://baike.baidu.com/image/d57e99942da24e5dd21b7080
// Http://baike.baidu.com/view/396887.htm
// Var array = [4,324, 45, 4, 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;
},
// Sort by hill
// 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 = [,];
Var stepArr = [1750,701,301,132, 57, 23, 10, 4, 1]; // reverse () the optimal step size is small on the Wiki.
// Var stepArr = [1031612713,217 378076, 45806244,965 1787, 2034035,428 481, 90358,190 01, 4025,836,182, 34, 9, 1] // select the step size for large arrays
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 ){
// Step statistics used by console. log (step)
Var I = 0, j = 0, f, tem, key;
Var stepLen = len % step> 0? ParseInt (len/step) + 1: len/step;
For (; I <step; I ++) {// round-robin Column
For (j = 1;/* j <stepLen & */step * j + I <len; j ++) {// loops each row of each column in sequence
Tem = f = step * j + I;
Key = array [f];
While (tem-= step)> = 0) {// search up sequentially
If (array [tem]> key ){
Array [tem + step] = array [tem];
} Else {
Break;
}
}
Array [tem + step] = key;
}
}
}
Return array;
}
Package and download test code