Text files, each row represents an integer in the range 0 to 512;
Requirements: Sort files, do not use heap space, only stack space.
Function generator with a certain amount of srand () and rand () Random Number
/*** file.c ***/ #include<stdio.h> #include<string.h> #include<time.h> const int maxn = 100000; int main() { srand((unsigned int)time(NULL)); int i; FILE *p = fopen("./a.txt","w"); for(i = 0; i < maxn; i++) { fprintf(p,"%d\n",(int)rand() % 513); } fclose(p); return 0; }
Then sort by:
/*** sort.c ***/ #include<stdio.h> #include<string.h> #include<time.h> int main() { int i; FILE *p = fopen("./a.txt","r"); int array[513] = {0}; while(!feof(p)) { char buf[100] = {0}; fgets(buf,sizeof(buf),p); if(0 != buf[0]) { int value = atoi(buf); array[value]++; } } fclose(p); p = fopen("./b.txt","w"); int j; for(i = 0; i < 513; i++) { for(j = 0; j < array[i]; j++) { fprintf(p,"%d\n",i); } } fclose(p); return 0; }