C++ 快速排序算法无法正常运行 #包括 #包括 使用名称空间std; //迭代显示每个应用程序的字符串数组AppName //在单独的行中输入名称。数组中有appCount元素 void displayAllApplicationNames(字符串appNames[],int-appCount); //在appIndex1和appIndex2之间交换字符串数组AppName中的字符串 void swapAppNames(int-appIndex1,int-appIndex2,string-appNames[]); //围绕轴索引p(轴)拆分字符串数组AppName。 //指数p以下的元素小于指数p以上的元素。 //函数返回pivot p int pivot(int first,int last,string appNames[]); //实现对字符串数组进行排序的快速排序算法 //第一个索引和最后一个索引之间的AppName void快速排序(int first,int last,string appNames[]); void main() { 字符串appNames[]= { “4)页数”、“2)基调”、“3)编号”, “8)Word”、“5)PowerPoint”、“1)Excel”, “0)文件”、“6)演示文稿”、“7)表格” }; 显示所有应用程序名称(appNames,9); swapAppNames(3、6、AppName); 显示所有应用程序名称(appNames,9); 快速排序(0、8、AppName); 显示所有应用程序名称(appNames,9); getchar(); } void displayAllApplicationNames(字符串appNames[],int-appCount) { 对于(appCount=0;appCount
您发布的代码仍然不正确。这是一个工作版本。我做了一些更改C++ 快速排序算法无法正常运行 #包括 #包括 使用名称空间std; //迭代显示每个应用程序的字符串数组AppName //在单独的行中输入名称。数组中有appCount元素 void displayAllApplicationNames(字符串appNames[],int-appCount); //在appIndex1和appIndex2之间交换字符串数组AppName中的字符串 void swapAppNames(int-appIndex1,int-appIndex2,string-appNames[]); //围绕轴索引p(轴)拆分字符串数组AppName。 //指数p以下的元素小于指数p以上的元素。 //函数返回pivot p int pivot(int first,int last,string appNames[]); //实现对字符串数组进行排序的快速排序算法 //第一个索引和最后一个索引之间的AppName void快速排序(int first,int last,string appNames[]); void main() { 字符串appNames[]= { “4)页数”、“2)基调”、“3)编号”, “8)Word”、“5)PowerPoint”、“1)Excel”, “0)文件”、“6)演示文稿”、“7)表格” }; 显示所有应用程序名称(appNames,9); swapAppNames(3、6、AppName); 显示所有应用程序名称(appNames,9); 快速排序(0、8、AppName); 显示所有应用程序名称(appNames,9); getchar(); } void displayAllApplicationNames(字符串appNames[],int-appCount) { 对于(appCount=0;appCount,c++,quicksort,C++,Quicksort,您发布的代码仍然不正确。这是一个工作版本。我做了一些更改 我从你的枢轴函数中删除了 是“一个结束”,则空列表为(0,0),(0,1)有长度1等,长度是简单的(最后一个)。如果你继续使用C++,你会发现这是STL的工作方式,所以现在学习它是有用的。 int pivot(int first, int last, string appNames[]) { int pivotIndex, mid = (first + last) / 2; swapAppNames(first, mid
我从你的枢轴函数中删除了
的含义,因为它在last
和quickSort
界面中使用。最好是pivot
表示“一个过了终点”。如果last
实际上是最后一项,则无法表示空列表。在您的符号中,(0,0)的长度为1,(0,1)长度为2等,长度为(最后一次)+1。如果<>最后> <代码>是“一个结束”,则空列表为(0,0),(0,1)有长度1等,长度是简单的(最后一个)。如果你继续使用C++,你会发现这是STL的工作方式,所以现在学习它是有用的。
last
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;
#包括
#包括
使用名称空间std;
//迭代显示每个应用程序的字符串数组AppName
//名称在单独的行中。数组中有appCount元素
void displayAllApplicationNames(字符串appNames[],int-appCount);
//在appIndex1和appIndex2之间交换字符串数组AppName中的字符串
void swapAppNames(int-appIndex1,int-appIndex2,string-appNames[]);
//围绕轴索引p(轴)拆分字符串数组AppName。
//指数p以下的元素小于指数p以上的元素。
//函数返回pivot p
int pivot(int first,int last,string appNames[]);
//实现对字符串数组进行排序的快速排序算法
//第一个索引和最后一个索引之间的AppName
void快速排序(int first,int last,string appNames[]);
int main(){
字符串appNames[]={
“4)页数”、“2)基调”、“3)编号”,
“8)Word”、“5)PowerPoint”、“1)Excel”,
“0)文件”、“6)演示”、“7)表格”};
显示所有应用程序名称(appNames,9);
swapAppNames(3、6、AppName);
显示所有应用程序名称(appNames,9);
快速排序(0、9、AppName);
显示所有应用程序名称(appNames,9);
返回0;}
void displayAllApplicationNames(字符串appNames[],int-appCount){
对于(int i=0;i
-那里有很多奇怪的事情。我将重新编写pivot函数,但与此同时,你能给我任何其他建议吗?我现在真的没有任何线索。如果不清楚,我是一个初学者,这是一个家庭作业。我能看到的第一件事是最后一次交换和返回应该是结束的第二,最后一次交换应该有pivot
和索引,索引应该是您在第一个位置存储枢轴的位置,而不是poivotindex
和first
。最后,我认为您应该在开始循环之前将枢轴存储在最后一个位置,而不是第一个位置(由于您对数组进行排序的方式,如果pivot是最大的元素,它将保持在第一位,而不会像它应该的那样移动到最后)。检查文章中的伪代码。非常感谢您,先生。这两个更改完全修复了它,您的简短解释为我阐明了一些事情。
last
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;
#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); } }