/*----- qsorts() ------*/ void qsorts(int *a_p, int low, int high) { int i=low, j=high, tmp; int p = a_p[low]; while (i <= j) { while (a_p[i] < p) { i++; } while (a_p[j] > p) { j--; } if (i<=j) { tmp = a_p[i]; a_p[i] = a_p[j]; a_p[j] = tmp; i++; j--; } } printlist(a_p); if (low < j) { qsorts(a_p, low, j); } if (high > i) { qsorts(a_p, i, high); } }
void qsorts(void *v[],int left ,int right,int (*comp)(void *,void *)){ int i,last; void swap(void *v[],int,int); if(left>=right) return; swap(v,left,(left+right)/2); last=left; for (i = left+1;i<= right;i++) if ((*comp)(v[i],v[left]) < 0) swap(v, ++last, i); swap(v,left,last); qsorts(v,left,last-1,comp); qsorts(v,last+1,right,comp); }
/*----- main() -----*/ int main() { int a[] = {3, 9, 1, 6, 8, 7, 2, 5, 4, 0}; printlist(a); qsorts(a, 0, A_SIZE-1); return EXIT_SUCCESS; }
int main(int argc,char *argv[]){ char *lineptr[MAXLINE]; int nlines; int stal,endl; int i,numeric=1; char c; if((argc>=2||argc<=4)&&(*argv[1])=='-'){ c=*(argv[1]+1); if(argc==3){ stal=atoi(argv[2]); endl=0;} if(argc==4){ stal=atoi(argv[2]); endl=atoi(argv[3]);} switch(c){ case 'n': if(*(argv[1]+2)=='r'){ option=2; break;} option=1; break; case 'r': if(*(argv[1]+2)=='n'){ option=2; break;} option=5; break; case 'f': if(*(argv[1]+2)=='d'){ option=6; break;} option=3; break; case 'd': if(*(argv[1]+2)=='f'){ option=6; break;} option=4; break; default: printf("option error\n"); return 1; break; } }else { if(argc==1) option=0; else { printf("usage error\n"); return 1; } } printf("%d",option); if(endl<=stal){ printf("start line end line illigal"); return 1;} if((nlines = readlines(lineptr,MAXLINE))>0){ if(endl>nlines-1) endl=nlines-1; switch(option){ case 0: qsorts((void **) lineptr,stal,endl,(int (*)(void*,void*))strcmp); break; case 1: qsorts((void **) lineptr,stal,endl,(int (*)(void*,void*))numcmp); break; case 2: qsorts((void **) lineptr,stal,endl,(int (*)(void*,void*))numcmp); break; case 3: qsorts((void **) lineptr,stal,endl,(int (*)(void*,void*))fccmp); break; case 4: qsorts((void **) lineptr,stal,endl,(int (*)(void*,void*))dcmp); break; case 5: qsorts((void **) lineptr,stal,endl,(int (*)(void*,void*))strcmp); break; case 6: qsorts((void **) lineptr,stal,endl,(int (*)(void*,void*))fdcmp); break; default: break; } writelines(lineptr,nlines); }else printf("too big to sort\n"); return 0; }