全排序列
function swap(array,a,b){ var m=array[a]; array[a]=array[b]; array[b]=m; } function full_sort(array,from,to){ if(from==to){ console.log(array); return; }else{ for(var i=from;i<to;i++){ swap(array,from,i); full_sort(array,from+1,to); swap(array,from,i); } } }
快速排序
function quicksort(array) { if (array.length <= 1) return array var pivotIndex = 0; var pivot = array[0]; var left = []; var right = []; for (var i = 1; i < array.length; i++) { if (array[i] > pivot) { right.push(array[i]) } else { left.push(array[i]); } } return quicksort(left).concat([pivot], quicksort(right)); }
时间: 2024-10-30 06:11:36