<script type="text/javascript"> function get(){ var num = [10,5,2,1,3,6,4,7,9,8]; var k=1; for(i=0; i<num.length-1; i++){ for(j=0; j<num.length-1-i;j++){ //比較次數-i個數 var temp=""; if(num[j]>num[j+1]){ temp = num[j]; num[j] = num[j+1]; num[j+1] = temp; } console.log(k+"==="+num); k++; } } } //冒泡排序;至少要比較N(N-1)/2次;10個數也就是45次 </script>