(function () { /*The quickest and simplest sort of the pass sort*/ vardata = [+, 1, +, +, +, +, +, +, +], result =[], barrel=[], I, j, item; //Initialize Bucket m = number of barrels for(i = 0; i < 101; i++) {Barrel[i]= 0; } //Insert Bucket n = number of sorts for(i = 0; item = data[i]; i++) {Barrel[item]++; } //traversing barrel m for(i = 0; i < barrel.length; i++) { //output sort number n if(Barrel[i]!== 0) { for(j = 0; J < Barrel[i]; J + +) {Result.push (i); }}} Console.log (Result.join (", ")); //O (m+n+m+n) = O (m+n) = O (m+n)}());
(function () { /*Bubble sort Basic idea: compare two adjacent elements at a time, and swap them out if they're in the wrong order. Principle: Each trip can only be determined to return a number of digits. */ vardata = [100, 50, 75, 25, 1, 20, 90, 30, 80, 40, 60, 50], length= Data.length-1, Inlength, I, J, Vitem; for(i = 0; i < length; i++) {Inlength= Length-i; for(j = 0; J < Inlength; J + +) { if(Data[j] > data[j + 1]) {Vitem= Data[j + 1]; Data[j+ 1] =Data[j]; DATA[J]=Vitem; }}} Console.log (Data.join (", ")); //O (N²)}());
(function () { /*Quick Sort Quick-order each round of processing is actually the number of the base of this round, until all the numbers are returned to place. Each exchange is a leap, and the total number of comparisons and exchanges is less. Determines the order in which the base number is correct on both sides. Based on the idea of "dichotomy"*/ vardata = [6, 9, 3, 7, 1, 5, 4, 8, 2, 3], QuickSort=function(left, right) {varMID = Data[left], I = left, j =Right , Vitem; if(Left >Right ) { return; } while(I!==j) { while(Data[j] >= mid && i <j) {J--; } while(Data[i] <= mid && i <j) {I++; } if(I <j) {Vitem=Data[j]; DATA[J]=Data[i]; Data[i]=Vitem; }} Data[left]=Data[i]; Data[i]=mid; QuickSort (left, I-1); QuickSort (i+ 1, right); }; QuickSort (0, Data.length-1); Console.log (Data.join (", ")); //O (NLOGN)}());
Daily english-2015/01/30– Algorithm (sort)