快速排序算法不能正常工作
Quicksort algorithm not functioning properly
#include <iostream>
#include <string>
using namespace std;
//Iterates over the string array appNames displaying each application
//name in a separate line. There are appCount elements in the array
void displayAllApplicationNames(string appNames[], int appCount);
//Swaps strings in string array appNames between appIndex1 and appIndex2
void swapAppNames(int appIndex1, int appIndex2, string appNames[]);
//Splits string array appNames around a pivot index p (the pivot).
//Elements below index p are less than elements above index p.
//The function returns the pivot p
int pivot(int first, int last, string appNames[]);
//Implements the QuickSort algorithm to sort string array
//appNames between indeces first and last
void quickSort(int first, int last, string appNames[]);
void main()
{
string appNames[] =
{
"4) Pages", "2) Keynote", "3) Numbers",
"8) Word", "5) PowerPoint", "1) Excel",
"0) Documents", "6) Presentation", "7) Sheets"
};
displayAllApplicationNames(appNames, 9);
swapAppNames(3, 6, appNames);
displayAllApplicationNames(appNames, 9);
quickSort(0, 8, appNames);
displayAllApplicationNames(appNames, 9);
getchar();
}
void displayAllApplicationNames(string appNames[], int appCount)
{
for(appCount = 0; appCount <= 8; appCount++)
{
cout << "[" << appCount << "]t"<< appNames[appCount] << endl;
}
if( appCount < 0 || appCount > 8)
{
cout << "_________" <<endl;
}
}
void swapAppNames(int appIndex1, int appIndex2, string appNames[])
{
string temp = appNames[appIndex1];
appNames[appIndex1] = appNames[appIndex2];
appNames[appIndex2] = temp;
}
int pivot(int first, int last, string appNames[])
{
int pivotIndex, mid = (first + last) / 2;
swapAppNames(first, mid, appNames);
pivotIndex = first;
string pivotValue = appNames[first];
for (int i = first + 1; i <= last; i++)
{
if (appNames[i] < pivotValue)
{
pivotIndex++;
swapAppNames(pivotIndex, i, appNames);
}
swapAppNames(first, last, appNames);
return pivotIndex;
}
}
void quickSort(int first, int last, string appNames[])
{
if (first < last)
{
int p = pivot( first, last, appNames);
quickSort( first, p - 1, appNames);
quickSort( p + 1, last, appNames);
}
}
我的目标是对字符串数组"appNames"中的名称进行排序。我将数字添加到名称中以显示它们应该在什么顺序中,但是当我运行程序时,它似乎根本没有正确排序。谁能告诉我我哪里做错了?
我已经看了好几天了,但一无所获。
编辑:这是解决方案。非常感谢每个回复我的人。必须交换几个变量的位置,并阅读快速排序算法。
int pivot(int first, int last, string appNames[])
{
int pivotIndex, mid = (first + last) / 2;
swapAppNames(first, mid, appNames);
pivotIndex = first;
string pivotValue = appNames[first];
for (int i = first + 1; i <= last; i++)
{
if (appNames[i] < pivotValue)
{
pivotIndex++;
swapAppNames(pivotIndex, i, appNames);
}
}
swapAppNames(pivotIndex, first, appNames);
return pivotIndex;
}
您发布的代码仍然不正确。这是一个工作版本。我做了一些改动。
我从枢轴函数中删除了mid
启发式。除非你的任务明确要求你考虑最坏的情况,否则它就是噪音。如果确实如此,那么就有更好的启发式方法可以使用。我还更改了交换工作方式,使其效率降低,但希望更直观。
另一个变化是last
的含义,因为它在您的quickSort
和pivot
接口中使用。如果last
的意思是"一过终点",那就更好了。如果last
实际上是最后一项,那么您就没有办法表示空列表。在你的符号中(0,0)的长度为1,(0,1)的长度为2,等等,长度计算为(last - first) + 1。如果last
是"最后一个",那么空列表是(0,0),(0,1)的长度为1,等等,长度只是(last - first)。如果你继续学习c++,你会发现这就是STL的工作方式,所以现在学习它是很有用的。
#include <iostream>
#include <string>
using namespace std;
//Iterates over the string array appNames displaying each application
//name in a separate line. There are appCount elements in the array
void displayAllApplicationNames(string appNames[], int appCount);
//Swaps strings in string array appNames between appIndex1 and appIndex2
void swapAppNames(int appIndex1, int appIndex2, string appNames[]);
//Splits string array appNames around a pivot index p (the pivot).
//Elements below index p are less than elements above index p.
//The function returns the pivot p
int pivot(int first, int last, string appNames[]);
//Implements the QuickSort algorithm to sort string array
//appNames between indices first and last
void quickSort(int first, int last, string appNames[]);
int main() {
string appNames[] = {
"4) Pages", "2) Keynote", "3) Numbers",
"8) Word", "5) PowerPoint", "1) Excel",
"0) Documents", "6) Presentation", "7) Sheets" };
displayAllApplicationNames(appNames, 9);
swapAppNames(3, 6, appNames);
displayAllApplicationNames(appNames, 9);
quickSort(0, 9, appNames);
displayAllApplicationNames(appNames, 9);
return 0; }
void displayAllApplicationNames(string appNames[], int appCount) {
for (int i = 0; i < appCount; ++i) {
cout << "[" << i << "]t" << appNames[i] << endl; }
cout << "_________" << endl; }
void swapAppNames(int appIndex1, int appIndex2, string appNames[]) {
string temp = appNames[appIndex1];
appNames[appIndex1] = appNames[appIndex2];
appNames[appIndex2] = temp; }
int pivot(int p, int n, string a[]) {
for (int i = p + 1; i < n; ++i) {
if (a[i] < a[p]) {
swapAppNames(i, p + 1, a);
swapAppNames(p, p + 1, a);
++p; } }
return p; }
void quickSort(int first, int last, string a[]) {
if (first < last) {
int p = pivot(first, last, a);
quickSort(first, p, a);
quickSort(p + 1, last, a); } }
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程