几种经典排序算法的JS实现方法(2)

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 < length; i++) { if (array[i] <= base[0]) { smaller.push(array[i]); } else { bigger.push(array[i]); } } console.log(smaller.concat(base.concat(bigger))); console.log("-----------------------"); return QuickSort(smaller).concat(base.concat(QuickSort(bigger))); } } var arr = [ 8, 10, 100, 90, 65, 5, 4, 10, 2, 4 ]; var result = QuickSort(arr); console.log(result); /* [ 5, 4, 2, 4, 8, 10, 100, 90, 65, 10 ] ----------------------- [ 4, 2, 4, 5 ] ----------------------- [ 2, 4, 4 ] ----------------------- [ 2, 4 ] ----------------------- [ 10, 10, 100, 90, 65 ] ----------------------- [ 90, 65, 100 ] ----------------------- [ 65, 90 ] ----------------------- [ 2, 4, 4, 5, 8, 10, 10, 65, 90, 100 ] */

以上这篇几种经典排序算法的JS实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgydxx.html