快速排序 - 三个中位数枢轴选择 - 某些元素顺序不正确

Quick Sort - Median-of-Three pivot selection - Some elements out of order

本文关键字:选择 顺序 不正确 元素 中位数 快速排序 三个      更新时间:2023-10-16

我已经尽我所能实现了我的教科书中的快速排序算法,其中包含三中位数枢轴选择函数 - 数据抽象和问题解决,墙壁和镜子第 7 版。它是部分工作的 - 除了数组中元素的结果并非全部按正确的升序排列。出于某种原因,在某个数字序列以正确的顺序放置后,突然一些整数似乎会在自己的间隔中失序,然后它会恢复到以后以正确的升序继续,然后重复一些整数再次乱序。我坚持使用教科书,并搜索了许多其他解决方案,并注意到一些细微的差异(例如,我的枢轴选择函数需要 3 个参数,而我见过的所有其他函数都需要两个参数(。我试图弄清楚它无济于事,我认为这是逻辑上次要的事情,我似乎无法从文本中的伪代码中推断出来。

template<typename ItemType>
void sortFirstMiddleLast(ItemType theArray[], int first, int middle, int last) {
ItemType temp, temp2, temp3;
if (theArray[first] > theArray[middle]) {
temp = theArray[first];
theArray[first] = theArray[middle];
theArray[middle] = temp;
}
if (theArray[middle] > theArray[last]) {
temp2 = theArray[last];
theArray[last] = theArray[middle];
theArray[middle] = temp2;
}
if (theArray[first] > theArray[middle]) {
temp3 = theArray[first];
theArray[first] = theArray[middle];
theArray[middle] = temp3;
}
}
template<typename ItemType>
int partition(ItemType theArray[], int first, int last)
{
ItemType temp;
//Choose pivot and reposition it
int mid = first + (last - first) / 2;
sortFirstMiddleLast(theArray, first, mid, last);
//Interchange
temp = theArray[last - 1];
theArray[last - 1] = theArray[mid];
theArray[mid] = temp;
int pivotIndex = last - 1;
ItemType pivot = theArray[pivotIndex];
//Determine the regions S sub 1 and S sub 2
int indexFromLeft = first + 1;
int indexFromRight = last - 2;

bool done = false;
while (!done) {
//locate first entry on left that is >= pivot
while (theArray[indexFromLeft] < pivot)
indexFromLeft = indexFromLeft + 1;
//locate first entry on right that is <= pivot 
while (theArray[indexFromRight] > pivot)
indexFromRight = indexFromRight - 1;
//now indexFromLeft has a new index subscript and indexFromRight has a new index subscript
//compare the two indexes 
if (indexFromLeft < indexFromRight) {
ItemType temp2 = theArray[indexFromRight];
theArray[indexFromRight] = theArray[indexFromLeft];
theArray[indexFromLeft] = temp2;
indexFromLeft = indexFromLeft + 1;
indexFromRight = indexFromRight - 1;
}
else
done = true;
}
//Place pivot in proper position between Ssub1 and Ssub2 and mark its new location
pivot = theArray[pivotIndex];
theArray[pivotIndex] = theArray[indexFromLeft];
theArray[indexFromLeft] = pivot;
pivotIndex = indexFromLeft;
return pivotIndex;
}
template<typename ItemType>
void quickSort(ItemType theArray[], int first, int last) {
//sift out small arrays 
int n = last - first + 1;
if ( n < MIN_SIZE){//array is of size < 10 so use insertion sort
insertionSort(theArray, n);
}
else {
//Make the partition : S1 | Pivot | S2
int pivotIndex = partition(theArray, first, last);
//Sort subarrays S1 and S2
quickSort(theArray, first, pivotIndex - 1);
quickSort(theArray, pivotIndex + 1, last);
}
}
const int RAND_NUMSIZE = 51; // for quick sort array size (random number gen 1-50)
const int MIN_SIZE = 10;//specify size of smallest array to use quick sort
int main()
{
int array5[RAND_NUMSIZE] = { 50, 41, 45, 43, 48, 40, 47, 42, 46, 49, 44, 39, 31, 37, 35, 33, 32, 38, 33, 34, 30, 36, 21, 29, 20, 22, 28, 23, 27, 24, 26, 25, 19, 13, 18, 14, 17, 15, 16, 12, 10, 11, 7, 8, 1, 4, 2, 6, 3, 9, 5 }
std::cout << "nThe quick sort array before sorting: n";
for (int i = 0; i < RAND_NUMSIZE; i++) {
std::cout << array5[i] << ' ';
}
//call quick sort
quickSort(array5, 0, RAND_NUMSIZE - 1);
std::cout << "nThe quick sort array after sorting: n";
for (int i = 0; i < RAND_NUMSIZE; i++) {
std::cout << array5[i] << ' ';
}

显示我正在谈论的结果的图像链接:

快速排序控制台输出

我认为您在这里的quickSort()中有问题:

if (n < MIN_SIZE) { //array is of size < 10 so use insertion sort
insertionSort(theArray, n);
}

当您有一个小分区时,您总是插入排序theArray的前n元素。

您实际上希望将范围从first排序为last。您可以通过传递指向theArray[first]和小分区大小的指针来做到这一点,如下所示:

if (n < MIN_SIZE) { //array is of size < 10 so use insertion sort
insertionSort(theArray + first, n);
}

当然,您还希望确保您的插入顺序正确...

一旦我在快速排序函数中更正了基本情况的实现,而不是使用插入排序,我在将 MIN_SIZE 的常量值设置为 4 后调用了 sortFirstMiddleLast,从而使用透视选择函数对 3 个条目进行排序,而不是通过尝试对小子数组使用分区和递归来导致逻辑错误。

template<typename ItemType>
void quickSort(ItemType theArray[], int first, int last) {
//relegate out small arrays 
int n = last - first + 1;//index range of elements to consider
int middle = first + (last - first) / 2;
if ( n < MIN_SIZE){//array is of size < 4 so no partition or quick sort
sortFirstMiddleLast(theArray, first, middle, last);//base case
}
else {
//Make the partition : S1 | Pivot | S2
int pivotIndex = partition(theArray, first, last);
//Sort subarrays S1 and S2
quickSort(theArray, first, pivotIndex - 1);
quickSort(theArray, pivotIndex + 1, last);
}

}