#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"에서 이름을 정렬하는 것입니다. 이름에 숫자를 추가하여 순서가 틀림 없음을 보여 주지만 프로그램을 실행하면 올바르게 정렬되지 않는 것 같습니다. 아무도 내가 잘못 가고 있다고 말할 수 있습니까?Quicksort 알고리즘이 제대로 작동하지 않습니다.
저는 며칠 동안 이것을 보지 않고 있습니다.
편집 : 해결책은 다음과 같습니다. 답장을 한 모든 사람들에게 큰 감사를드립니다. 몇 가지 변수의 위치를 교환하고 퀵 소트 알고리즘을 읽어야했습니다. 당신이 그것을 게시 된
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;
}
'피벗'을 확인하십시오. 거기에 많은 이상한 점이 있습니다. –
피벗 기능을 재 작업 하겠지만, 그 동안에는 다른 조언을 해줄 수 있습니까? 나는이 시점에서 실마리가 없다. 그것이 분명하지 않은 경우, 나는 초보자이며 이것은 숙제 임무입니다. – user3835257
내가 처음 볼 수있는 것은 마지막 스왑과 리턴이 루프 (피벗 내부)에서 벗어나야한다는 것입니다. 둘째, 마지막 스왑에는 '첫 번째'와 '마지막'이 아닌 'poivotindex'와 피벗을 저장 한 위치의 인덱스가 있어야합니다. 마지막으로 피벗을 먼저 저장하는 대신 루프를 시작하기 전에 마지막 위치에 저장해야한다고 생각합니다. 피벗이 가장 크고 요소 인 경우 배열을 정렬하는 방식으로 인해 첫 번째 위치에 머무르고 이동하지 않습니다. 마지막으로해야한다). –