putting thigs together, now it seems to work. Of course, this is just a simple implementation of Quicksort as advised to do by Deitel:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STRLEN 256
#define STRING_COUNT 5
void quickSort(char * [],int,int);
int partition(char *[],int , int );
void swap (char **,char **);
void printStrings(char * []);
int main()
{
char * strings[STRING_COUNT];
int i;
char buffer[MAX_STRLEN];
char *tmp;
for (i=0;i<STRING_COUNT;i++)
{
printf("Enter string #%d: ",i);
fflush(stdout);
if (!fgets (buffer, sizeof(buffer),stdin))
{
printf("FGETS ERROR! Program terminated.\n");
exit(1);
}
if (tmp=strchr(buffer,'\n'))
{
*tmp='\0';
}
strings[i]=malloc(strlen(buffer)+1);
if (!strings[i])
{
printf("Out of memory\n");
exit(1);
}
strcpy(strings[i],buffer);
}
quickSort(strings,0,STRING_COUNT-1);
printStrings(strings);
}
int partition (char * str[],int lb, int ub)
{
int a=lb,b=ub;
while(a<b)
{
while ((a<b)&&(strcmp(str[a],str[b])<=0))
{
b--;
}
if (a<b)
{
swap(&str[a],&str[b]);
a++;
}
while ((a<b)&&(strcmp(str[a],str[b])<=0))
{
a++;
}
if (a<b)
{
swap(&str[b],&str[a]);
b--;
}
}
return a;
}
void quickSort(char * str[],int lb, int ub)
{
if (lb>=ub) return;
else
{
int newPosition;
newPosition=partition(str,lb,ub);
quickSort(str,lb,newPosition-1);
quickSort(str,newPosition+1,ub);
}
}
void swap (char ** a, char ** b)
{
char * tmp = *a;
*a=*b;
*b=tmp;
}
void printStrings(char * strings[])
{
int i;
for (i=0;i<STRING_COUNT;i++)
{
printf("%d: %s\n",i,strings[i]);
}
}