私のオンライン コースの課題の 1 つで、単語のリストを並べ替えるためにクイック ソート アルゴリズムを使用する必要があります。数字のリストはソートできましたが、単語はソートできませんでした。関数 IthChar は、文字列と文字列のインデックスを表す整数の 2 つの引数を取り、インデックスの場所にある文字を返します。
たとえば、IthChar("Paul", 0) --> P
クイックソート、スワップ、および swapPivot 関数は次のとおりです。
void quickSort(string array[], int left, int right)
{
int I, J, pivot;
char chI, chJ, chPivot;
if(left<right)
{
pivot=left;
I=left;
J=right;
while(I<J)
{
chI=IthChar(array[I], 0);
chJ=IthChar(array[J], 0);
chPivot=IthChar(array[I], 0);
while(chI<=chPivot&&I<right)
I++;
while(chJ>chPivot)
J--;
if(I<=J)
{
swap(array, I, J);
}
}
swapPivot(array, pivot, J);
quickSort(array, left, J-1);
quickSort(array, J+1, right);
}
}
void swap(string array[], int loc, int loc1)
{
int temp;
temp=array[loc];
array[loc]=array[loc1];
array[loc1]=temp;
}
void swapPivot(string array[], int pivot, int J)
{
int temp;
temp=array[pivot];
array[pivot]=array[J];
array[J]=temp;
}
ありがとう