function BubbleSort(array) { var length = array.length; for (var i = length - 1; i > 0; i--) { //用于缩小范围 for (var j = 0; j array[j+1]) { var temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } console.log(array); console.log("-----------------------------"); } return array; } var arr = [10,9,8,7,7,6,5,11,3]; var result = BubbleSort(arr); console.log(result); /* [ 9, 8, 7, 7, 6, 5, 10, 3, 11 ] ----------------------------- [ 8, 7, 7, 6, 5, 9, 3, 10, 11 ] ----------------------------- [ 7, 7, 6, 5, 8, 3, 9, 10, 11 ] ----------------------------- [ 7, 6, 5, 7, 3, 8, 9, 10, 11 ] ----------------------------- [ 6, 5, 7, 3, 7, 8, 9, 10, 11 ] ----------------------------- [ 5, 6, 3, 7, 7, 8, 9, 10, 11 ] ----------------------------- [ 5, 3, 6, 7, 7, 8, 9, 10, 11 ] ----------------------------- [ 3, 5, 6, 7, 7, 8, 9, 10, 11 ] ----------------------------- [ 3, 5, 6, 7, 7, 8, 9, 10, 11 ] */
二.选择排序
function SelectionSort(array) { var length = array.length; for (var i = 0; i三.插入排序
function InsertionSort(array) { var length = array.length; for (var i = 0; i= 0; j--) { if (insert 四.希尔排序
function ShellSort(array) { var length = array.length; var gap = Math.round(length / 2); while (gap > 0) { for (var i = gap; i= 0; j-=gap) { if (insert 五.归并排序
function MergeSort(array) { var length = array.length; if (length <= 1) { return array; } else { var num = Math.ceil(length/2); var left = MergeSort(array.slice(0, num)); var right = MergeSort(array.slice(num, length)); return merge(left, right); } } function merge(left, right) { console.log(left); console.log(right); var a = new Array(); while (left.length > 0 && right.length > 0) { if (left[0] <= right[0]) { var temp = left.shift(); a.push(temp); } else { var temp = right.shift(); a.push(temp); } } if (left.length > 0) { a = a.concat(left); } if (right.length > 0) { a = a.concat(right); } console.log(a); console.log("-----------------------------"); return a; } var arr = [ 13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10 ]; var result = MergeSort(arr); console.log(result); /* [ 13 ] [ 14 ] [ 13, 14 ] ----------------------------- [ 94 ] [ 33 ] [ 33, 94 ] ----------------------------- [ 13, 14 ] [ 33, 94 ] [ 13, 14, 33, 94 ] ----------------------------- [ 82 ] [ 25 ] [ 25, 82 ] ----------------------------- [ 59 ] [ 94 ] [ 59, 94 ] ----------------------------- [ 25, 82 ] [ 59, 94 ] [ 25, 59, 82, 94 ] ----------------------------- [ 13, 14, 33, 94 ] [ 25, 59, 82, 94 ] [ 13, 14, 25, 33, 59, 82, 94, 94 ] ----------------------------- [ 65 ] [ 23 ] [ 23, 65 ] ----------------------------- [ 45 ] [ 27 ] [ 27, 45 ] ----------------------------- [ 23, 65 ] [ 27, 45 ] [ 23, 27, 45, 65 ] ----------------------------- [ 73 ] [ 25 ] [ 25, 73 ] ----------------------------- [ 39 ] [ 10 ] [ 10, 39 ] ----------------------------- [ 25, 73 ] [ 10, 39 ] [ 10, 25, 39, 73 ] ----------------------------- [ 23, 27, 45, 65 ] [ 10, 25, 39, 73 ] [ 10, 23, 25, 27, 39, 45, 65, 73 ] ----------------------------- [ 13, 14, 25, 33, 59, 82, 94, 94 ] [ 10, 23, 25, 27, 39, 45, 65, 73 ] [ 10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94 ] ----------------------------- [ 10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94 ] */六.快速排序
function QuickSort(array) { var length = array.length; if (length <= 1) { return array; } else { var smaller = []; var bigger = []; var base = [array[0]]; for (var i = 1; i以上这篇几种经典排序算法的JS实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。