Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

快速排序 #3

Open
RicoLiu opened this issue Feb 11, 2018 · 0 comments
Open

快速排序 #3

RicoLiu opened this issue Feb 11, 2018 · 0 comments

Comments

@RicoLiu
Copy link
Owner

RicoLiu commented Feb 11, 2018

快速排序,又称划分交换排序。在平均状况下,排序 n 个项目要 O(n·logn) 次比较。在最坏状况下则需要 O(nˆ2) 次比较,但这种状况并不常见。事实上,快速排序通常明显比其他 O(n·logn) 算法更快,因为它的内部循环可以在大部分的架构上很有效率地被实现出来。

Array.prototype.quick_sort = function() {
	var len = this.length;
	if (len <= 1) {
                return this.slice(0);
        }
	var left = [];
	var right = [];
	var mid = [this[0]];
	for (var i = 1; i < len; i++) {
                if (this[i] < mid[0]) {
                    left.push(this[i]);
                } else {
                    right.push(this[i]);
                }	
        }
	return left.quick_sort().concat(mid.concat(right.quick_sort()));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant