JavaScript version of several common sorting algorithms to share

Source: Internet
Author: User
Tags array array length insert key return sort version

Description

· 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)

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

 
 
  1. ----------some sort algorithms
  2. JS uses sort for sorting
  3. Systemsort:function (Array) {
  4. Return Array.Sort (function (A, b) {
  5. return a-b;
  6. });
  7. },
  8. Bubble sort
  9. Bubblesort:function (Array) {
  10. var i = 0, Len = array.length,
  11. J, D;
  12. for (; i<len; i++) {
  13. For (j=0 j<len; j + +) {
  14. if (Array[i] < array[j]) {
  15. d = array[j];
  16. ARRAY[J] = Array[i];
  17. Array[i] = D;
  18. }
  19. }
  20. }
  21. return array;
  22. },
  23. Quick Sort
  24. Quicksort:function (Array) {
  25. var array = [8,4,6,2,7,9,3,5,74,5];
  26. 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];
  27. var i = 0;
  28. var j = array.length-1;
  29. var Sort = function (i, j) {
  30. End condition
  31. if (i = = j) {return};
  32. var key = Array[i];
  33. var tempi = i; Record start position
  34. var tempj = j; Record End Position
  35. while (J > i) {
  36. J <<--------------look forward
  37. if (Array[j] >= key) {
  38. j--;
  39. }else{
  40. Array[i] = Array[j]
  41. i++------------>> Look Back
  42. while (J > ++i) {
  43. if (Array[i] > key) {
  44. ARRAY[J] = Array[i];
  45. Break
  46. }
  47. }
  48. }
  49. }
  50. If the first key to remove is the smallest number
  51. if (tempi = = i) {
  52. Sort (++i, TEMPJ);
  53. return;
  54. }
  55. Last vacancy left to key
  56. Array[i] = key;
  57. Recursion
  58. Sort (tempi, i);
  59. Sort (J, TEMPJ);
  60. }
  61. Sort (i, j);
  62. return array;
  63. },
  64. Insert Sort
  65. Insertsort:function (Array) {
  66. http://baike.baidu.com/image/d57e99942da24e5dd21b7080
  67. Http://baike.baidu.com/view/396887.htm
  68. 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];
  69. var i = 1, j, temp, Key,
  70. len = Array.Length;
  71. for (; i < Len; i++) {
  72. temp = j = i;
  73. key = Array[j];
  74. while (--j >-1) {
  75. if (Array[j] > key) {
  76. ARRAY[J+1] = Array[j];
  77. }else{
  78. Break
  79. }
  80. }
  81. ARRAY[J+1] = key;
  82. }
  83. return array;
  84. },
  85. Hill sort
  86. Jun.array.shellSort (JUN.ARRAY.DF (10000));
  87. Shellsort:function (Array) {
  88. Http://zh.wikipedia.org/zh/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F
  89. var array = [13,14,94,33,82,25,59,94,65,23,45,27,73,25,39,10];
  90. var Temparr = [1750, 701, 301, 132, 57, 23, 10, 4, 1];
  91. Reverse () See this optimal step size array on the wiki
  92. var Temparr = [1031612713, 217378076, 45806244,
  93. 9651787, 2034035, 428481, 90358, 19001, 4025, 836, 182, 34, 9, 1]
  94. Step selection for large arrays
  95. var i = 0;
  96. var temparrtemparrlength = temparr.length;
  97. var len = array.length;
  98. var len2 = parseint (LEN/2);
  99. for (; i < temparrlength; i++) {
  100. if (Temparr[i] > Len2) {
  101. Continue
  102. }
  103. Tempsort (Temparr[i]);
  104. }
  105. Sort one Step
  106. function Tempsort (temp) {
  107. Step statistics used by Console.log (temp)
  108. var i = 0, j = 0, F, tem, key;
  109. var Templen = len%temp > 0? parseint (len/temp) + 1:len/temp;
  110. for (; i < temp; i++) {//Loop columns sequentially
  111. For (J=1;/*j < Templen && */temp * j + i < Len; j + +) {
    Loops each row of each column in turn
  112. TEM = F = Temp * j + i;
  113. key = Array[f];
  114. while ((tem-=temp) >= 0) {
  115. Look up in turn
  116. if (Array[tem] > key) {
  117. Array[tem+temp] = Array[tem];
  118. }else{
  119. Break
  120. }
  121. }
  122. Array[tem + temp] = key;
  123. }
  124. }
  125. }
  126. return array;
  127. }

Original link: http://www.cnblogs.com/idche/archive/2011/02/16/1956397.html

"Edit Recommendation"

    1. 10 amazing HTML5 and JavaScript effects
    2. JavaScript objects and the built-in objects of the inheritance tutorial
    3. JavaScript memory recovery mechanism in depth interpretation
    4. JavaScript beginners should pay attention to seven details
    5. wrote a continuous assignment operation that JavaScript may not fully understand for 10 years.
"Responsible editor: Chen Yu new TEL: (010) 68476606"


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.