it does not seem to work even if i make MAXLINE and MAXWORD to be 100. When i look into the program in detail, it seems to me that the problem arises from the quicksort function that i have written. It does not proceed beyond
Code:
strcpy (pivot, data[(low+high)/2]);
the error after trying to run the program was Segmentation fault (core dumped)
here is my program anyway.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAXLINE 100
#define MAXWORD 100
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[100],temp[100];
int 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);
}
thank you for your attention!