JavaScript Sort Instances

Source: Internet
Author: User
Tags arrays sort

Examples of JavaScript ordering are the following code:

 
 
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <title>www.cxybl.com</title>
  3. <style type= "Text/css" >
  4. *{font-family:arial, Helvetica, Sans-serif}
  5. . div1{
  6. Background-color: #35BDDB;
  7. Float:left;
  8. margin-left:1px;
  9. Color: #fff;
  10. Vertical-align:middle;
  11. font-size:12px;
  12. }
  13. . sorted{
  14. Background-color: #CFD0D2}
  15. #content {width:100%; height:601px; Background-color: #222222; text-align:center;}
  16. </style>
  17. <script type= "Text/javascript" src= "Jquery-1.4.2.js" ></script>
  18. <script type= "Text/javascript" >
  19. var numbs = [];
  20. var numbers = 40;
  21. var divwidth = Math.floor (1000/numbers);
  22. var ti = 1;
  23. var step = 0;
  24. var step1 = 0;
  25. function init () {
  26. Step1 = 0;
  27. Numbers = $ ("#numb"). Val ();
  28. if (isNaN (numbers) number (numbers) <= 0) {
  29. Numbers = 40;
  30. $ ("#numb"). Val (40);
  31. }
  32. if (number (numbers) > 500) {
  33. Numbers = 500;
  34. $ ("#numb"). Val (500);
  35. }
  36. Numbs = [];
  37. Step = 0;
  38. TI = 1;
  39. $ ("#content"). HTML ("");
  40. while (Numbs.length < numbers) {
  41. var t = Math.floor (600*math.random ());
  42. if (Numbs.indexof (T) < 0) {
  43. Numbs.push (t);
  44. }
  45. }
  46. $ ("#step"). HTML ("");
  47. $.each (numbs, function (i,n) {
  48. $ ("<div>"). AddClass ("Div1"). CSS ({width: (100/numbers-0.1) + "%", Height:n + "px", "margin-top": (600-n) + "px"})
  49. . attr ("id", N). Appendto ($ ("#content"));
  50. });
  51. }
  52. $ (function () {
  53. Init ();
  54. $ ("#random"). Click (init);
  55. $ ("#bubbleSort"). Click (function () {
  56. $ (": Input"). attr ("Disabled", "disabled");
  57. if (step1!= 0) {
  58. Init ();
  59. }
  60. Bubblesort ();
  61. });
  62. $ ("#QuickSort"). Click (function () {
  63. $ (": Input"). attr ("Disabled", "disabled");
  64. if (step1!= 0) {
  65. Init ();
  66. }
  67. QuickSort (numbs, 0, numbs.length);
  68. });
  69. });
  70. function Bubblesort () {
  71. for (i = 0; i< numbs.length;i++) {
  72. for (j = i + 1;j<numbs.length;j++) {
  73. if (Numbs[i] > Numbs[j]) {
  74. Swap (Numbs, I, J);
  75. }
  76. }
  77. }
  78. }
  79. function swap (arrays, I, j) {
  80. var t = arrays[i];
  81. var m = arrays[j];
  82. Arrays[i] = Arrays[j];
  83. ARRAYS[J] = t;
  84. step1++;
  85. settimeout ("Swapdiv" (' # ' + "+t+"), $ (' # ' + "+m+")) ", (ti++) *50);
  86. }
  87. function Swapdiv (DIV1, Div2) {
  88. $ (". Div1"). Removeclass ("sorted");
  89. var Tempdiv = $ ("<div>");
  90. Div1.after (TEMPDIV);
  91. Div2.after (DIV1);
  92. Tempdiv.after (DIV2);
  93. Tempdiv.remove ();
  94. Div1.addclass ("sorted");
  95. Div2.addclass ("sorted");
  96. $ ("#step"). HTML (++step);
  97. if (step = = Step1) {
  98. $ (": Input"). attr ("Disabled", "");
  99. $ (". Div1"). Removeclass ("sorted");
  100. }
  101. }
  102. function QuickSort (PData, left, right) {
  103. var i,j;
  104. var middle,temp;
  105. i = left;
  106. j = right;
  107. middle = pdata[left];
  108. while (true) {
  109. while ((++i) <right-1 && pdata[i]<middle);
  110. while ((--j) >left && pdata[j]>middle);
  111. if (I>=J)
  112. Break
  113. Swap (PData, I, J);
  114. }
  115. Swap (PData, left, J);
  116. if (LEFT<J)
  117. QuickSort (PDATA,LEFT,J);
  118. if (right>i)
  119. QuickSort (Pdata,i,right);
  120. }
  121. </script>
  122. <body>
  123. <div id= "num" ></div>
  124. <button id= "Bubblesort" >bubbleSort</button>
  125. <button id= "QuickSort" >QuickSort</button>
  126. <input id= "Numb" value= "/>"
  127. <button id= "Random" >init</button>
  128. Step:<span id= "Step" ></span>
  129. <div id= "Content" ></div>
  130. </body>


Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.