/* 一百万的随机数排序,费时1秒 ! */ /* c */ #include "stdio.h" #include "time.h" int shu[1000000]; int i,j,k,len; void quick_sort(int left,int right); void swap(int left,int right); void read_file(); void main() { clock_t start, finish; double duration; read_file(); printf ("%d\n",len); start = clock(); quick_sort(0,len-1); finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf ("%.6f\n",duration); for (i=0;i<len-1;i++) if (shu[i]>shu[i+1]) printf("%d %d\n",shu[i],shu[i+1]); } void read_file() { FILE * fp; char c; fp = fopen("e:/test/sort/100w.txt","r"); len=0; k=0; c = fgetc(fp); while (c!=EOF) { if (c == 32) { shu[len]=k; k=0; len++; } else k = k*10+c-48; c = fgetc(fp); } fclose(fp); } void quick_sort(int left,int right) { int m; if (left>=right) return; m = left; for (i=left+1;i<=right;i++) if (shu[i]<shu[left]) swap(++m,i); swap(left,m); quick_sort(left,m-1); quick_sort(m+1,right); } void swap(int left,int right) { int temp; temp = shu[left]; shu[left] = shu[right]; shu[right] = temp; }