quick sort去除无用判断
#include <stdio.h> #include <stdlib.h> //int a[]={1000,10000,9,10,30,20,50,23,90,100,10}; int a[]={10,9,8,7,6,5}; int length=sizeof(a)/sizeof(int); int swap(int start, int stop){ int temp; temp=a[start]; a[start]=a[stop]; a[stop]=temp; return 0; } int quicksort(int start, int stop) { int i; int ostart=start; int ostop=stop; int k=a[start]; int middle; if(start >= stop){ return 0; } while( start < stop ){ //printf("\n begin start=%d,stop=%d ,k=%d , ostart=%d,ostop=%d\n" , start, stop, k,ostart,ostop);while((a[stop] >= k) &&(start<stop)){ stop--; } while( (a[start] < k) &&(start<stop)){ start++; } //printf("after move start=%d,stop=%d ,k=%d , ostart=%d,ostop=%d\n" , start, stop, k,ostart,ostop); if( start < stop ){ swap(start,stop); } //printf("may after swap start=%d,stop=%d ,k=%d , ostart=%d,ostop=%d\n" , start, stop, k,ostart,ostop); } quicksort(ostart, start); quicksort(start+1, ostop); } int main(){ printf("length= %d\n",length); int i; printf("\n"); quicksort(0,length-1); printf("\n\n\n"); for(i=0;i<length;i++){ printf("%d\n" , a[i]); } printf("\n"); return 0; }
MySQL限时解答,24小时内友哥专业解答
http://www.yougemysqldba.com
如有进一步需要请联系微信onesoft007
微博账号@友哥一指
http://www.yougemysqldba.com
如有进一步需要请联系微信onesoft007
微博账号@友哥一指