My code doesn't sort more than ten words and I need it to sort about 2000 strings of alfabet. and string length is less than 20 characters. I made sure that the file read and write is correct. the file have a list of words less than 20 characters and each line contains one word. the problem is that the quick sort function is not sorting for large amount of strings.
#include <stdio.h>
#include <string.h>
void quickSortMain(char items[][20], int count);
void quickSort(char items[][20], int left, int right);
int main(void)
{
int i=0, n=0;
char str[2000][20];
memset(str, 0 , sizeof(str[0][0])*20*1000);
FILE *fil;
fil=fopen("text.txt","r");
if (fil!=NULL)
{
while (!feof(fil))
{
fgets(str[i], 20, fil);
i++;
}
fclose(fil);
}
else
{
printf("can not open");
scanf("\n");
}
fil=fopen("text.txt","w");
if (fil!=NULL)
{
fclose(fil);
}
else
{
printf("can not open");
scanf("\n");
}
quickSortMain(str, i);
fil=fopen("text.txt","a");
if (fil!=NULL)
{
while(n<i)
{
fprintf(fil,"%s", str[n]);
n++;
}
fclose(fil);
}
else
{
printf("can not open");
scanf("\n");
}
return 0;
}
void quickSortMain(char items[][20], int count)
{
quickSort(items, 0, count-1);
}
void quickSort(char items[][20], int left, int right)
{
int i=left;
int j=right;
char *x=items[(left+right)/2];
char temp[right+1];
while(i <= j)
{
while((strcmp(items[i],x) < 0) && (i < right)) {
i++;
}
while((strcmp(items[j],x) > 0) && (j > left)) {
j--;
}
if(i <= j) {
strcpy(temp, items[i]);
strcpy(items[i], items[j]);
strcpy(items[j], temp);
i++;
j--;
}
}
if(left < j) {
quickSort(items, left, j);
}
if(i < right) {
quickSort(items, i, right);
}
}