码迷,mamicode.com
首页 > 编程语言 > 详细

quicksort 快速排序

时间:2018-07-06 17:51:56      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:ast   class   排序   fun   highlight   快速排序   typeof   last   efi   

javascript

function qsort(a, comp) {
	if (typeof comp==="undefined") {
		comp = function(a, b) {
			return a - b < 0;
		}
	}
	function _qsort(a, low, high) {
		if (!comp(low, high)) {
			return;
		}
		var first = low, 
			last = high,
			pivot = a[first];
		while (comp(first, last)) {
			while (comp(first, last) && !comp(a[last], pivot)) {
				--last;
			}
			a[first] = a[last];
			while (comp(first, last) && !comp(pivot, a[first])) {
				++first;
			}
			a[last] = a[first];
		}
		a[first] = pivot;
		_qsort(a, low, first-1);
		_qsort(a, first+1, high);
	}
	
	return _qsort(a, 0, a.length-1);
}

// quicksort
var a = [57, 68, 59, 52, 52, 72, 28, 96, 33, 24];
qsort(a);  
console.log(a); // [24, 28, 33, 52, 52, 57, 59, 68, 72, 96]

  

 

quicksort 快速排序

标签:ast   class   排序   fun   highlight   快速排序   typeof   last   efi   

原文地址:https://www.cnblogs.com/mingzhanghui/p/9274431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!