JS Array sorting

Source: Internet
Author: User

Description

Writing this is mainly to exercise yourself, and there is no practical significance.

Each browser test results in a different data. For example, I use the chrome Test General quick Sort will be the fastest, IE will be based on the length of the array May hill the fastest.

Don't use too much data to test bubble sort (browser crashes I don't care)

If you are interested in online sort animations download test page

Personal understanding

Bubble sort: Simplest, slowest, seemingly shorter than 7 optimal

Insert sort: Faster than bubbling, slower than quick sort and hill sort, smaller data has advantages

Quick sort: This is a very quick sort method, V8 's sort methods are used to combine quick sort and insert sort

Hill sort: Under non- chrome, the array length is less than 1000, and the hill sort is faster than fast

System method: This method of the system under Forfox is very fast

//----------some sort algorithms//js using sort for sortingSystemsort:function(array) {returnArray.Sort (function(A, b) {returnAb; });},//Bubble SortBubblesort:function(array) {vari = 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; }        }    }    returnarray;},//Quick SortQuickSort: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];    vari = 0; varj = Array.length-1; varSort =function(i, j) {//End Condition        if(i = =j) {return        }; varKey =Array[i]; varStepi = i;//Record start position        varSTEPJ = j;//Record End Position         while(J >i) {//J <<--------------forward lookup            if(Array[j] >=key) {J--; } Else{Array[i]=Array[j]//i++------------>> backwards Search                 while(J > + +)i) {if(Array[i] >key) {Array[j]=Array[i];  Break; }                }            }        }        //if the first key taken is the smallest number        if(Stepi = =i) {Sort (++I, STEPJ); return; }        //The last vacancy is reserved for keyArray[i] =key; //RecursiveSort (Stepi, i);    Sort (J, STEPJ);    } Sort (I, j); returnarray;},//Insert SortInsertsort: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];    vari = 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; }    returnarray;},//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];    varSteparr = [1750, 701, 301, 132, 57, 23, 10, 4, 1];//reverse () on the wiki see this optimal step size of the smaller array    //var Steparr = [1031612713, 217378076, 45806244, 9651787, 2034035, 428481, 90358, 19001, 4025, 836, 182, 34, 9, 1]//for large The step selection of an array    vari = 0; varSteparrlength =steparr.length; varLen =Array.Length; varLen2 = parseint (LEN/2);  for(; i < steparrlength; i++) {        if(Steparr[i] >len2) {            Continue;    } stepsort (Steparr[i]); }    //Sort a step    functionStepsort (step) {//Step Statistics used by Console.log (step)        vari = 0, J= 0, F, TEM, key; varSteplen = 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 + +) {//loop each row of each column in turnTEM = F = Step * j +i; Key=Array[f];  while(TEM-= Step) >= 0) {//look up in turn                    if(Array[tem] >key) {Array[tem+ Step] =Array[tem]; } Else {                         Break; }} Array[tem+ Step] =key; }        }    }    returnArray;}

JS Array sorting

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.