【2020年8月9日17:22:37】感谢凤源blog指正,该代码存在bug,可能这个解题思路是错误的。已删去相关代码。
一、背景
某校考研真题:
2. 快速排序算法中,如何选取一个界值(又称为轴元素),影响着快速排序的效 率,而且界值也并不一定是被排序序列中的一个元素。例如,我们可以用被排序序列中所有元素的平均值作为界值。 用 C 语言编写算法实现以平均值为界值 的快速排序方法(注:待排序数据存储在数组 R[ ]中, 数组最小下标为 S,数组最大下标为 T)。
二、分析
1. 什么是快排(Quicksort)?
引入wiki定义:
Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm. Developed by British computer scientist Tony Hoare in 1959[1] and published in 1961,[2] it is still a commonly used algorithm for sorting. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heapsort.[3][contradictory]
Quicksort is a divide-and-conquer algorithm. It works by selecting a ‘pivot’ element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. This can be done in-place, requiring small additional amounts of memory to perform the sorting.
Quicksort is a comparison sort, meaning that it can sort items of any type for which a “less-than” relation (formally, a total order) is defined. Efficient implementations of Quicksort are not a stable sort, meaning that the relative order of equal sort items is not preserved.
Mathematical analysis of quicksort shows that, on average, the algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare.
简而言之,快排是一个基于分治思想与比较的非稳定排序算法。
2. 快排怎么做?
引入wiki:
Quicksort is a divide and conquer algorithm. It first divides the input array into two smaller sub-arrays: the low elements and the high elements. It then recursively sorts the sub-arrays. The steps for in-place Quicksort are:
Pick an element, called a pivot, from the array.
Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
The base case of the recursion is arrays of size zero or one, which are in order by definition, so they never need to be sorted.
The pivot selection and partitioning steps can be done in several different ways; the choice of specific implementation schemes greatly affects the algorithm’s performance.
三、代码部分
代码有bug!
代码有bug!
代码有bug!
避免误人子弟,已删除相关代码,留下一个思路做供参考。
//这份代码是有bug的,同理,网路上另外一篇思路大同小异的代码也是有bug的,希望大家谨慎参考。
void quickSortByMean(int arr[], int low, int high) {
double mean = getMean(arr, low,high);
int i = low, j = high;
if(low<high)//保证待处理序列>1 {
while (i<j) {
/* code */
while (j>i&&arr[j]>mean) {
/* code */
--j;
}
while (j>i&&arr[i]<mean) {
/* code */
++i;
}
if(i<j) {
swap(&arr[i], &arr[j]);
--j;
++i;
}
}
if(low<j&&i<high) {
quickSortByMean(arr,low,j);
quickSortByMean(arr,i,high);
}
}
printArray(arr, len);
}
四、结果
//有bug
这份代码在VSCode + C++11的标准下运行成功,如果您发现有错,欢迎指正。
答题的时候没必要写这么臃肿,主要是为了跑出来方便分析。
- example 1
- example 2