JackJobs - 3 years ago 345
C Question

# quic sort char array in c

I copy a int array quick sort algorithmn from http://www.comp.dit.ie/rlawlor/Alg_DS/sorting/quickSort.c
Then change it to char array quick sort algorithmn.

Here is the 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 expecte the output to be: abcdg, but output is: abcgd

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