排序算法
js实现:
//1,冒泡排序
var arr = [9,1,5,2,8,4];
for(var j = 0 ;j < arr.length ; j ++){ for(var i = 0 ; i < arr.length-j ; i ++){ if( arr[i]>arr[i+1]){ var trans = arr[i]; arr[i] = arr[i+1]; arr[i+1] = trans; }; console.log(arr) }; };
//2,插入法
var arr = [3,2,6,4,9,1,5,7,8];
var aResult = []; for(var i = 0 ; i < arr.length ;i ++){ if(aResult.length == 0){ aResult.push(arr[0]); }; // console.log(aResult) for(var j = 0 ;j < i ; j ++){ if(arr[i]<aResult[j]){ aResult.splice(j,0,arr[i]); break; }else{ aResult.push(arr[i]); break; } }; }; console.log(aResult)
3,快速排序
4,希尔排序
5,归并排序
6,堆排序