我正在尝试使用快速排序对字母数组进行排序。
我基本上已经尝试从主要算法出发并将其转换为与 char 数组一起使用。
我想我快到了,但我似乎无法得到它。
任何帮助深表感谢。
:)
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int qscounter = 0;
int split(char a[], char low, char high)
{
char part_element = a[low];
for (;;) {
while (low < high && part_element <= a[high])
high--;
if (low >= high) break;
a[low++] = a[high];
while (low < high && a[low] <= part_element)
low++;
if (low >= high) break;
a[high--] = a[low];
}
a[high] = part_element;
return high;
}
void quick_sort(char a[], char low, char high)
{
char middle;
if (low >= high) return;
middle = split(a, low, high);
qscounter++;
quick_sort(a, low, middle - 1);
quick_sort(a, middle + 1, high);
printf("Quick Sort: %d\n", qscounter);
for(int i=0;i<26;i++)
printf("%c",a[i]);
printf("\n\n");
}
void main()
{
char unsorted_alphabet[26] = {'A','E','O','D','B','Q','G','V','Y','J','Z','S','M','N','C','P','F','R','L','T','U','H','W','X','I','K'};
quick_sort(unsorted_alphabet,unsorted_alphabet[0],unsorted_alphabet[25]);
fflush(stdin);
getchar();
}