Several common sorting algorithms for JavaScript edition

Source: Internet
Author: User

Today found an article about "JavaScript version of several common sorting algorithm", look good, recommend the original: http://www.w3cfuns.com/blog-5456021-5404137.html

Algorithm Description:

* 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: In non-chrome below the array length is less than 1000, hill sort faster than fast
* System method: This method of the system under Forfox is very fast

1 //----------some sort algorithms2 //js using sort for sorting3Systemsort:function(array) {4     returnArray.Sort (function(A, b) {5         returnAb;6     });7 },8 //Bubble Sort9Bubblesort:function(array) {Ten     vari = 0, OneLen =Array.Length, A J, D; -      for(; i < Len; i++) { -          for(j = 0; J < Len; J + +) { the             if(Array[i] <Array[j]) { -D =Array[j]; -ARRAY[J] =Array[i]; -Array[i] =D; +             } -         } +     } A     returnArray; at }, - //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]; -     vari = 0; in     varj = Array.length-1; -     varSort =function(i, j) { to  +         //End Condition -         if(i = =j) { the             return *         }; $ Panax Notoginseng         varKey =Array[i]; -         varStepi = i;//Record start position the         varSTEPJ = j;//Record End Position +          while(J >i) { A             //J <<--------------forward lookup the             if(Array[j] >=key) { +j--; -}Else { $Array[i] =Array[j] $                 //i++------------>> backwards Search -                  while(J > + +)i) { -                     if(Array[i] >key) { theARRAY[J] =Array[i]; -                          Break;Wuyi                     } the                 } -             } Wu         } -  About         //if the first key taken is the smallest number $         if(Stepi = =i) { -Sort (+ +I, STEPJ); -             return; -         } A  +         //The last vacancy is reserved for key theArray[i] =key; -  $         //recursive Sort (Stepi, i); the Sort (J, STEPJ); the     } the  the Sort (i, j); -  in     returnArray; the }, the  About //Insert Sort theInsertsort:function(array) { the  the     //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]; the     vari = 1,BayiJ, step, key, Len =Array.Length; the  the      for(; i < Len; i++) { -  -Step = j =i; theKey =Array[j]; the  the          while(--j >-1) { the             if(Array[j] >key) { -Array[j + 1] =Array[j]; the}Else { the                  Break; the             }94         } the  theArray[j + 1] =key; the     }98  About     returnArray; - },101 102 //Hill Sort103 //Jun.array.shellSort (JUN.ARRAY.DF (10000));104Shellsort:function(array) { the 106     //Http://zh.wikipedia.org/zh/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F107     //var array = [13,14,94,33,82,25,59,94,65,23,45,27,73,25,39,10];108     varSteparr = [1750, 701, 301, 132, 57, 23, 10, 4, 1];//reverse () on the wiki see this optimal step size of the smaller array109     //var Steparr = [1031612713, 217378076, 45806244, 9651787, 2034035, 428481, 90358, 19001, 4025, 836, 182, 34, 9, 1]//for large The step selection of an array the     vari = 0;111     varSteparrlength =steparr.length; the     varLen =Array.Length;113     varLen2 = parseint (LEN/2); the  the      for(; i < steparrlength; i++) { the         if(Steparr[i] >len2) {117             Continue;118         }119  - Stepsort (Steparr[i]);121     }122 123     //Sort a step124     functionStepsort (step) { the 126         //Step Statistics used by Console.log (step)127         vari = 0, -j = 0,129 F, TEM, key; the         varSteplen = len% Step > 0? parseint (len/step) + 1:len/Step;131  the          for(; i < step; i++) {//Loop columns sequentially133              for(j = 1;134             /*J < Steplen &&*/135Step * j + i < Len; J + +) {//loop each row of each column in turn136TEM = F = Step * j +i;137Key =Array[f];138 139                  while(TEM-= Step) >= 0) {//look up in turn $                     if(Array[tem] >key) {141Array[tem + Step] =Array[tem];142}Else {143                          Break;144                     }145                 }146 147Array[tem + Step] =key;148 149             } Max         }151  the     }153 154     returnArray;155 156}

Several common sorting algorithms for JavaScript edition

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.