

快速排序(Quicksort)的Javascript实现
source link: https://blogread.cn/it/article/3867?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

快速排序(Quicksort)的Javascript实现
日本程序员norahiko,写了一个排序算法的动画演示,非常有趣。
这个周末,我就用它当做教材,好好学习了一下各种排序算法。
排序算法(Sorting algorithm)是计算机科学最古老、最基本的课题之一。要想成为合格的程序员,就必须理解和掌握各种排序算法。
目前,最常见的排序算法大概有七八种,其中"快速排序"(Quicksort)使用得最广泛,速度也较快。它是图灵奖得主C. A. R. Hoare(1934--)于1960时提出来的。
"快速排序"的思想很简单,整个排序过程只需要三步:
(1)在数据集之中,选择一个元素作为"基准"(pivot)。
(2)所有小于"基准"的元素,都移到"基准"的左边;所有大于"基准"的元素,都移到"基准"的右边。
(3)对"基准"左边和右边的两个子集,不断重复第一步和第二步,直到所有子集只剩下一个元素为止。
举例来说,现在有一个数据集{85, 24, 63, 45, 17, 31, 96, 50},怎么对其排序呢?
第一步,选择中间的元素45作为"基准"。(基准值可以任意选择,但是选择中间的值比较容易理解。)
第二步,按照顺序,将每个元素与"基准"进行比较,形成两个子集,一个"小于45",另一个"大于等于45"。
第三步,对两个子集不断重复第一步和第二步,直到所有子集只剩下一个元素为止。
下面参照网上的资料(这里和这里),用Javascript语言实现上面的算法。
首先,定义一个quickSort函数,它的参数是一个数组。
var quickSort = function(arr) {
然后,检查数组的元素个数,如果小于等于1,就返回。
var quickSort = function(arr) {
if (arr.length <= 1) { return arr; }
接着,选择"基准"(pivot),并将其与原数组分离,再定义两个空数组,用来存放一左一右的两个子集。
var quickSort = function(arr) {
if (arr.length <= 1) { return arr; }
var pivotIndex = Math.floor(arr.length / 2) ;
var pivot = arr.splice(pivotIndex, 1)[0];
var left = [];
var right = [];
然后,开始遍历数组,大于"基准"的元素放入左边的子集,小于基准的元素放入右边的子集。
var quickSort = function(arr) {
if (arr.length <= 1) { return arr; }
var pivotIndex = Math.floor(arr.length / 2) ;
var pivot = arr.splice(pivotIndex, 1)[0];
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++){
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
最后,使用递归不断重复这个过程,就可以得到排序后的数组。
var quickSort = function(arr) {
if (arr.length <= 1) { return arr; }
var pivotIndex = Math.floor(arr.length / 2);
var pivot = arr.splice(pivotIndex, 1)[0];
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++){
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
return quickSort(left).concat([pivot], quickSort(right));
使用的时候,直接调用quickSort()就行了。
建议继续学习:
扫一扫订阅我的微信号:IT技术博客大学习
Recommend
-
48
The quicksort algorithm has the best case complexity of O(n log n) when each pivot in the sort divides the list into two equal uniform pieces [1]. The worst case occurs when the pivot always divides the list into one lis...
-
83
go-pdqsort is my implementation of pattern defeating sort in golang. I knew about pattern defeating sort from rust’s standard library documentation. I’ve never heard abou...
-
21
Quicksort is a sorting algorithm. It works in three main steps: Pick an element from the array (Called the pivot) Rearrange the array so that all the elements with values less than the pivot come...
-
8
Yet another explanation of the Quicksort algorithmLoading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.jsYet another explanation of the Quicksort algorithm For those who have a hard time understanding it. Many...
-
5
Quicksort optimizations explained [complete code] yourbasic.org/golang Most Quickso...
-
13
Armin's BlogPOJ2299 Ultra-QuickSort(离散化 树状数组)February 14, 2016题目链接 题意:求 n 个数的逆序对数。 思路:首先 n 只有 500000...
-
2
Files Permalink Latest commit message Commit time
-
5
Go ile Algoritmalar — QuickSortBu gün Go ile QuickSort algoritmasını yazacağız. Bunun için harici bir kütüphaneye ihtiyacımız yok. Kısaca QuickSort algoritmasına değinmek gerekirse...
-
9
-
11
POSTED ON JULY 20, 2022 TO Open Source, Security U...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK