1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="gb2312"> 5 <title>选择排序法</title> 6 </head> 7 8 <body> 9 <script> 10 var array=[15,8,5,32,3,56,36,98,15]; 11 // var array=[5,4,3,2,1],indexMin; 12 console.info("%c" + ["原数组",array].join(" "),['color:red'].join(";")) 13 14 function swap(index1,index2){ 15 var aux=array[index1]; 16 array[index1]=array[index2]; 17 array[index2]=aux; 18 } 19 20 for(var i=0;i<array.length-1;i++){ 21 indexMin=i; 22 for(var j=i; j<array.length;j++){ 23 if(array[indexMin]>array[j]){ 24 debugger; 25 indexMin=j; 26 27 } 28 } 29 if(i!= indexMin){ 30 swap(i,indexMin); 31 } 32 console.log(array); 33 } 34 35 </script> 36 </body> 37 </html>