JackJobs JackJobs - 5 months ago 59
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";
quickSortChar(arr, 0, strlen(arr) - 1);

I expecte the output to be: abcdg, but output is: abcgd

Answer Source

You're calling quickSort() recursively (wrong function name) inside quickSortChar().

After fixing this bug, it runs and produces good output on my machine:

$ ./program
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download