关于JS的算法
一.快速排序
function qSort(arr) { if(arr.length === 0) { return []; } var left = []; var right = []; var pivot = arr[0]; for (var i = 1; i < arr.length; i++) {//从i=1开始 if(arr[i] < pivot) { left.push(arr[i]); }else { right.push(arr[i]); } } return qSort(left).concat(pivot, qSort(right)); }//快速排序 window.onload = function() { var a = []; for (var i = 0; i < 10; ++i) { a[i] = Math.floor((Math.random()*100)+1); } console.log(a); console.log(qSort(a));//快速排序 }
二.插入排序
function insertionSort(arr) { var temp, inner; console.log(arr); for(var outer = 1; outer < arr.length; outer++) { temp = arr[outer]; inner = outer; while(inner > 0 && (arr[inner - 1] >= temp)) { arr[inner] = arr[inner - 1]; inner--; } arr[inner] = temp; } return arr; }//插入排序 window.onload = function() { var a = []; for (var i = 0; i < 10; ++i) { a[i] = Math.floor((Math.random()*100)+1); } console.log(a); console.log(insertionSort(a));//插入排序 }
三.选择排序法
function selectionSortMax(arr) { var max; for(var outer = 0; outer < arr.length; outer++) { max = 0; for(var inner = 0; inner < arr.length - outer; inner++) { if(arr[inner] > arr[max]) { max = inner; } } swap(arr, max, arr.length - outer - 1); } return arr; }//选择排序法选择最大值移到最后 function selectionSortMin(arr) { var min, temp; for(var outer = 0; outer < arr.length - 1; outer++) { min = outer; for(var inner = outer + 1; inner < arr.length; inner++) { if(arr[inner] < arr[min]) { min = inner; } } swap(arr, min, outer); } return arr; }//选择排序法选择最小值移到最前 function swap(arr, index1, index2) { var temp = arr[index2]; arr[index2] = arr[index1]; arr[index1] = temp; } window.onload = function() { var a = []; for (var i = 0; i < 10; ++i) { a[i] = Math.floor((Math.random()*100)+1); } console.log(a); console.log(selectionSortMax(a)); console.log(selectionSortMin(a)); }
四.冒泡排序法
function bubbleSort(arr) { var temp; for(var outer = arr.length; outer > 1; outer--) { for(inner = 0; inner < outer - 1; inner++) { if(arr[inner] > arr[inner + 1]) { swap(arr, inner, inner + 1); } } } return arr; }//冒泡排序法 function swap(arr, index1, index2) { var temp = arr[index2]; arr[index2] = arr[index1]; arr[index1] = temp; } window.onload = function() { var a = []; for (var i = 0; i < 10; ++i) { a[i] = Math.floor((Math.random()*100)+1); } console.log(a); console.log(bubbleSort(a));//冒泡排序法 }
五.归并算法
function mergeSortRec(array) { var length = array.length; if(length == 1) { return array; } var mid = Math.floor(length / 2); var left = array.slice(0, mid); var right = array.slice(mid, length); return merge(mergeSortRec(left), mergeSortRec(right)); } function merge(left, right) { var result = []; var il = 0; var ir = 0; while(il < left.length && ir < right.length) { if(left[il] < right[ir]) { result.push(left[il++]); }else { result.push(right[ir++]); } } while(il < left.length) { result.push(left[il++]); } while(ir < right.length) { result.push(right[ir++]); } return result; } window.onload = function() { var a = []; for (var i = 0; i < 10; ++i) { a[i] = Math.floor((Math.random()*100)+1); } console.log(a); console.log(mergeSortRec(a));//归并排序法 }
通过时间戳可以得到快速排序>选择排序>插入排序>冒泡排序。