1. Bubble sort (thought: Each cycle is started from the last, compared to the previous one, if it is smaller than the previous one, swap the position until it cannot be exchanged.) Slowest but most easily implemented)
<! DOCTYPE html>functionOrder (a) {varresult = ""; for(vari = 0; i < a.length; i++) { for(varj = a.length; J > 0; j--) { if(A[j] < a[j-1]) { vartemp = A[j-1]; A[j-1] =A[j]; A[J]=temp; }} result+ = "First" + (i + 1) + "secondary cycle, Result:"; for(vark = 0; K < A.length; k++) {result+ = A[k] + ","; } result+ = "<br/>"; } returnresult; } varOpro = document.getElementById ("Pro"); varOarr = document.getElementById ("arr"); vararr = []; Opro.onclick=function(){ for(vari = 0; I < 5; i++) {Arr[i]= Math.ceil (Math.random () * (100-0) +0) }; Oarr.innerhtml=arr; } varOdo = document.getElementById ("Do"); Odo.onclick=function(){ varOresult = document.getElementById ("Result"); Oresult.innerhtml=order (arr); }</script></body>2. Select sort (thought: each cycle is starting from the first, after each bit of comparison, after comparing all the digits, the smallest number in the first place)
<! DOCTYPE html>functionOrder (a) {varresult = ""; varmin; for(vari = 0; i < a.length-1; i++) {min=i; for(varj = i+1; J < A.length; J + +) { if(A[min] >A[j]) {min=J; } } if(min! =i) {vartemp =A[min]; A[min]=A[i]; A[i]=temp; } result+ = "First" + (i + 1) + "secondary cycle, Result:"; for(vark = 0; K < A.length; k++) {result+ = A[k] + ","; } result+ = "<br/>"; } returnresult; } varOpro = document.getElementById ("Pro"); varOarr = document.getElementById ("arr"); vararr = []; Opro.onclick=function(){ for(vari = 0; I < 5; i++) {Arr[i]= Math.ceil (Math.random () * (100-0) +0) }; Oarr.innerhtml=arr; } varOdo = document.getElementById ("Do"); Odo.onclick=function(){ varOresult = document.getElementById ("Result"); Oresult.innerhtml=order (arr); }</script></body>3. Insert sort (thought: the outer loop is used to move the array, the inner loop is used to compare the selected elements in the outer loop and the elements behind it)
functionOrder (a) {varresult = ""; for(vari = 1; i < a.length; i++) { vartemp=A[i]; for(varj=i-1;j>=0&&temp<a[j];j--) {a[j+1]=A[J];//move the value greater than temp to one unit after the whole} a[j+1]=temp; Result+ = "First" + (i + 1) + "secondary cycle, Result:"; for(vark = 0; K < A.length; k++) {result+ = A[k] + ","; } result+ = "<br/>"; } returnresult; }4. Quick sort (thought: Select a benchmark, less than its number is on the left, greater than his number is on the right, loop call function, implement sort)
functionOrder (arr) {varresult = ""; if(Arr.length <= 1) {returnarr;} varPivotindex = Math.floor (arr.length/2); varPivot = Arr.splice (pivotindex, 1) [0]; varleft = []; varright = []; for(vari = 0; i < arr.length; i++) {if(Arr[i] <pivot) {Left.push (arr[i]); } Else{Right.push (arr[i]); }} result=order (left). Concat ([pivot], order (right)); returnresult; };Several sorting algorithms