快速排序算法不能正常工作

Quicksort algorithm not functioning properly

本文关键字:工作 常工作 算法 不能 快速排序      更新时间:2023-10-16
#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的含义,因为它在您的quickSortpivot接口中使用。如果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); } }