1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <input type="text" id="input" style="500px"/> 9 10 <input type="button" id="submit" value="确认" onclick="deal()"/> 11 <script> 12 var arr = [5,6,4,7,3,8,2,9,1,0]; 13 var l = 10; 14 document.getElementById("input").value = arr; 15 var q = document.getElementById("submit"); 16 function deal() { 17 //bubbleSort(); 18 //selectSort(); 19 //insertSort(); 20 //_inserSort(1); 21 //shellSort(); 22 //arr = qSort(arr); 23 //quickSort(0,l); 24 document.getElementById("input").value = arr; 25 } 26 function bubbleSort() 27 { 28 var flag = true; 29 var len = l -1; 30 var tem; 31 while(flag) 32 { 33 flag = false; 34 for(var i = 0;i<len;i++) 35 { 36 if(arr[i]>arr[i+1]) 37 { 38 tem = arr[i]; 39 arr[i] = arr[i+1]; 40 arr[i+1] = tem; 41 flag = true; 42 } 43 } 44 } 45 } 46 function selectSort() 47 { 48 var min ; 49 var len = l-1; 50 var tem; 51 for(var i = 0;i<l-1;i++) 52 { 53 min = i; 54 for(var j = i+1;j<l;j++) 55 { 56 if(arr[j]<arr[min]) 57 { 58 min = j; 59 } 60 } 61 tem = arr[i]; 62 arr[i] = arr[min]; 63 arr[min] = tem; 64 } 65 } 66 function insertSort() 67 { 68 var j; 69 var tem; 70 for(var i = 1;i<l;i++) 71 { 72 tem = arr[i]; 73 for(j = i;j>0&&tem<arr[j-1];j--) 74 { 75 arr[j] = arr[j-1]; 76 } 77 arr[j] = tem; 78 } 79 } 80 function _inserSort(gap) 81 { 82 //alert("_inserSort"); 83 var j; 84 var tem; 85 for(var i = gap;i<l;i++) 86 { 87 tem = arr[i]; 88 for(j = i;j>0&&tem<arr[j-gap];j-= gap) 89 { 90 arr[j] = arr[j-gap]; 91 } 92 arr[j] = tem; 93 } 94 } 95 function shellSort() 96 { 97 alert("shellSort"); 98 var gap = parseInt(l/2); 99 while(gap>=1) 100 { 101 _inserSort(gap); 102 gap = parseInt(gap/2); 103 } 104 } 105 function qSort(a) 106 { 107 //alert("qSort"); 108 var l = a.length; 109 if(l == 0) 110 { 111 return []; 112 } 113 var lesser = []; 114 var greater = []; 115 var p = a[0]; 116 for(var i = 1;i<l;i++) 117 { 118 if(a[i]<p) 119 { 120 lesser.push(a[i]); 121 } 122 else 123 { 124 greater.push(a[i]); 125 } 126 } 127 return qSort(lesser).concat(p,qSort(greater)); 128 } 129 function quickSort(s,e) 130 { 131 console.log(arr); 132 var i = s ; 133 var j = e ; 134 if(s<e-1) 135 { 136 while(1) 137 { 138 do 139 { 140 i++; 141 } 142 while(arr[i]<arr[s]&&i<e-1); 143 do 144 { 145 j--; 146 } 147 while(arr[j]>arr[s]&&j>s+1) 148 if(i<j) 149 { 150 var tem = arr[j]; 151 arr[j]= arr[i]; 152 arr[i] =tem; 153 } 154 else 155 { 156 break; 157 } 158 } 159 var tem = arr[s]; 160 arr[s] = arr[j]; 161 arr[j] = tem; 162 163 quickSort(s,j); 164 quickSort(j+1,e); 165 } 166 } 167 </script> 168 </body> 169 </html>