JackJobs - 3 years ago 376
C Question

quick sorting a char array in C

I copy an

`int`
array in order to try the quick sort algorithm from here.

Then I change the type of the array to
`char`
. Here is the edited code:

``````extern void quickSortChar(char a[], int left, int right)
{
int j;
if (left < right)
{
j = partition(a, left, right);
quickSort(a, left, j - 1);
quickSort(a, j + 1, right);
}
}

static int partition(char a[], int left, int right)
{
int pivot, i, j, t;
pivot = a[left];
i = left; j = right + 1;

while (1)
{
do i++; while (a[i] <= pivot && i <= right);
do j--; while (a[j] > pivot);
if (i >= j) break;
t = a[i]; a[i] = a[j]; a[j] = t;
}
t = a[left]; a[left] = a[j]; a[j] = t;
return j;
}

void main()
{
char arr[] = "bacgd";
puts(arr);
quickSortChar(arr, 0, strlen(arr) - 1);
puts(arr);
system("pause");
}
``````

I expect the output to be:
`abcdg`
, but output is:
`abcgd`

You're calling `quickSort()` recursively (wrong function name) inside `quickSortChar()`.
``````\$ ./program