/* ---------------------------------------------------------------------------- By Bob Jenkins, July 8 1995. Sort any length file by line. Public Domain This would be 4x faster if it merged 16 subfiles at a time rather than just two. ---------------------------------------------------------------------------- */ #include #include #define FNAMLEN 16 #define LINELEN 128 #define LSORTMAX 255 #define MAXDEP 31 void merge(a,b,c) FILE *a; /* first input file */ FILE *b; /* second input file */ FILE *c; /* third input file */ { char ax[LINELEN],bx[LINELEN]; register char *ay,*by; ay = fgets(ax,LINELEN,a); by = fgets(bx,LINELEN,b); while (ay && by) { if (strcmp(ay,by) < 0) { fprintf(c,"%s",ay); if (!fgets(ay,LINELEN,a)) ay = (char *)0; } else { fprintf(c,"%s",by); if (!fgets(by,LINELEN,b)) by = (char *)0; } } if (!ay) { do {fprintf(c,"%s",by);} while (fgets(by,LINELEN,b)); } else { do {fprintf(c,"%s",ay);} while (fgets(ay,LINELEN,a)); } } /* read LSORTMAX lines and write them, sorted, to another file. */ /* return TRUE if the end of the input is reached. */ int littlesort(inp,oup) FILE *inp; /* opened file to read unsorted lines in from */ FILE *oup; /* opened file to write sorted lines out to */ { register int i,j,k,lines; char lbuf[LSORTMAX][LINELEN],*last; char *l[LSORTMAX]; for (lines=0; (lines>1; i; i>>=1) { for (j=0; j>=1, ++mergepairs) ; /* merge all the pairs we have */ for (j=0; j