先來張圖片算法
算法描述shell
代碼實現api
設置一標誌性變量pos,用於記錄每趟排序中最後一次進行交換的位置。因爲pos位置以後的記錄均已交換到位,故在進行下一趟排序時只要掃描到pos位置便可。數組
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function bubbleSort(arr) { var i = arr.length-1; //初始時,最後位置保持不變 while ( i> 0) { var pos= 0; //每趟開始時,無記錄交換 for (var j= 0; j< i; j++) if (arr[j]> arr[j+1]) { pos= j; //記錄交換的位置 var tmp = arr[j]; arr[j]=arr[j+1];arr[j+1]=tmp; } i= pos; //爲下一趟排序做準備 } console.timeEnd('改進後冒泡排序耗時'); return arr; } |
算法描述ui
代碼實現prototype
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function selectionSort(arr) { var len = arr.length; var minIndex, temp; for (var i = 0; i < len - 1; i++) { minIndex = i; for (var j = i + 1; j < len; j++) { if (arr[j] < arr[minIndex]) { //尋找最小的數 minIndex = j; //將最小數的索引保存 } } temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } return arr; } |
算法描述blog
代碼描述排序
1 2 3 4 5 6 7 8 9 10 11 12 |
function insertionSort(array) { for (var i = 1; i < array.length; i++) { var key = array[i]; var j = i - 1; while (j >= 0 && array[j] > key) { array[j + 1] = array[j]; j--; } array[j + 1] = key; } return array; } |
算法分析遞歸
算法描述索引
代碼實現
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function shellSort(arr) { var len = arr.length, temp, gap = 1; console.time('希爾排序耗時:'); while(gap < len/5) { //動態定義間隔序列 gap =gap*5+1; } for (gap; gap > 0; gap = Math.floor(gap/5)) { for (var i = gap; i < len; i++) { temp = arr[i]; for (var j = i-gap; j >= 0 && arr[j] > temp; j-=gap) { arr[j+gap] = arr[j]; } arr[j+gap] = temp; } } console.timeEnd('希爾排序耗時:'); return arr; } |
算法分析
算法描述
代碼實現
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
function mergeSort(arr) { //採用自上而下的遞歸方法 var len = arr.length; if(len < 2) { return arr; } var middle = Math.floor(len / 2), left = arr.slice(0, middle), right = arr.slice(middle); return merge(mergeSort(left), mergeSort(right)); } function merge(left, right) { var result = []; console.time('歸併排序耗時'); while (left.length && right.length) { if (left[0] <= right[0]) { result.push(left.shift()); } else { result.push(right.shift()); } } while (left.length) result.push(left.shift()); while (right.length) result.push(right.shift()); console.timeEnd('歸併排序耗時'); return result; } |
算法分析
算法描述
代碼實現
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
var arr = [12,3,23,5,17,9,15,46]; function quickSort(arr,left, right){ var i,j,t,temp; if(left>right){ return; } i = left; j = right; temp = arr[left]; while(i!==j){ while(temp<=arr[j]&&i<j){ j--; } while(temp>=arr[i]&&i<j){ i++ } if(i<j){ [arr[j], arr[i]] = [arr[i], arr[j]]; } } arr[left] = arr[i]; arr[i] = temp; quickSort(arr,left,i-1); quickSort(arr,i+1,right); } quickSort(arr,0, arr.length-1); console.log(arr); |
算法分析
算法描述
代碼實現
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
function heapify(arr, x, len) { if (Object.prototype.toString.call(arr).slice(8, -1) === 'Array' && typeof x === 'number') { var l = 2 * x + 1, r = 2 * x + 2, largest = x, temp; if (l < len && arr[l] > arr[largest]) { largest = l; } if (r < len && arr[r] > arr[largest]) { largest = r; } if (largest != x) { temp = arr[x]; arr[x] = arr[largest]; arr[largest] = temp; heapify(arr, largest, len); } } else { return 'arr is not an Array or x is not a number!'; } } /*方法說明:維護堆的性質 @param arr 數組 @param x 數組下標 @param len 堆大小*/ function heapify(arr, x, len) { if (Object.prototype.toString.call(arr).slice(8, -1) === 'Array' && typeof x === 'number') { var l = 2 * x + 1, r = 2 * x + 2, largest = x, temp; if (l < len && arr[l] > arr[largest]) { largest = l; } if (r < len && arr[r] > arr[largest]) { largest = r; } if (largest != x) { temp = arr[x]; arr[x] = arr[largest]; arr[largest] = temp; heapify(arr, largest, len); } } else { return 'arr is not an Array or x is not a number!'; } } |
算法分析