Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int IsLess(char * x, char * mid)
{
//control the order of alphebetizing here with < or >
if(strcmp( x,mid) < 0)
return -1;
else
return 1;
}
char * GetRecord(char * a[], int x)
{
char * temp = (char*) malloc (sizeof(char) * 20);
temp = a[x];
return temp;
}
void QSort(char * a[], int left, int right)
{
int i = left;
int j = right;
char * temp = NULL;
//frees placeholder during recursive call
free(temp);
//get middle record
temp = GetRecord(a, (i+j) / 2);
do
{
while (IsLess(GetRecord(a,i), temp) < 0 && i < right)
i++;
while (IsLess(temp,GetRecord(a,j)) < 0 && j > left)
j--;
//swap
if( i <= j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}while( i <= j);
//recursive calls
if(left < j)
QSort(a,left,j);
if(i < right)
QSort(a,i,right);
}
int main()
{
int i;
const int size = 5;
//jagged array rather than a smooth array[x][size]
char * array[size] = {"namev","namee","namex","namea","namen" };
QSort(array, 0, size - 1);
printf("Sorted array of strings.\n");
for(i = 0; i < size; i++)
{
printf("%s\n",array[i]);
}
return 0;
}