Quicksort-1000000-lines problem
hi, i m having some problems with my program. Would appreciate any help. I am supposed to sort over 10000 lines of characters using quicksort. I cannot seem to tell what is wrong with the one i have come up with. Thank you for your attention!
here is my code
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAXLINE 1000000
#define MAXWORD 1000000
void quicksort(char *data[], int low, int high);
int main(void)
{
clock_t start , end;
int i;
char *data[MAXLINE], *tmp, buffer[MAXWORD];
for (i = 0; i < MAXLINE; i++) {
if (gets(buffer) == NULL) break;
if ((tmp = malloc(strlen(buffer) + 1)) == NULL) exit(-1);
strcpy(tmp, buffer);
*(data + i) = tmp;
}
*(data+i) = NULL;
start = clock();
quicksort(data,0,i-1);
end = clock();
for (i = 0; data[i]; i++) {
fprintf(stdout, "%s\n", data[i]);
free(data[i]);
}
fprintf(stderr, "\n****************\n%f sec\n****************\n", (double)(end - start) / CLOCKS_PER_SEC);
return (0);
}
void quicksort(char *data[], int low, int high)
{
char pivot[1000000],temp[1000000],i,j;
i=low;
j=high;
strcpy (pivot, data[(low+high)/2]);
do
{
while ((strcmp(data[i],pivot))<1)
i++;
while ((strcmp(data[j],pivot))>1)
j--;
if (i<=j)
{
strcpy (temp, data[i]);
strcpy (data[i], data[j]);
strcpy (data[j], temp);
i++;
j--;
}
}
while (i<=j);
if (low<j) quicksort(data, low,j);
if (i<high) quicksort(data, i, high);
}