快速排序 - 三个中位数枢轴选择 - 某些元素顺序不正确
Quick Sort - Median-of-Three pivot selection - Some elements out of order
我已经尽我所能实现了我的教科书中的快速排序算法,其中包含三中位数枢轴选择函数 - 数据抽象和问题解决,墙壁和镜子第 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);
}
}
相关文章:
- 如何使用默认参数等选择模板专业化
- CMake-按正确顺序将项目与C运行时对象文件链接
- 函数调用中参数的顺序重要吗
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 为什么不;名字在地图上是按顺序排列的吗
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 数到第n个楼梯的路(顺序无关紧要)
- 选择要调用的构造函数
- C++选择排序算法中的逻辑错误
- QTreeView幻灯片多选后无法使用单击选择
- 优先顺序:智能指针和类析构函数
- 无法获取菜单选择以运行函数.C++
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 在循环中按顺序遍历成员变量
- 独立读取-修改-写入顺序
- QML按钮点击功能执行顺序
- 快速排序 - 三个中位数枢轴选择 - 某些元素顺序不正确
- C :使用声明和指令的顺序会影响选择
- 顺序正向选择(SFS)算法
- EM_EXGETSEL与文本选择顺序无关.如何确定一段选定文本中的插入符号位置