#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(" begin start=%d,stop=%d ,k=%d , ostart=%d,ostop=%d " , start, stop, k,ostart,ostop); int i; for(i=0;i<length;i++){ printf("%d " , a[i]); } while((a[stop] >= k) &&(start<stop)){ stop--; } while( (a[start] < k) &&(start<stop)){ start++; } if(start >= stop){ break; } printf("after move start=%d,stop=%d ,k=%d , ostart=%d,ostop=%d " , 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 " , start, stop, k,ostart,ostop); } quicksort(ostart, start); quicksort(stop+1, ostop); } int main(){ printf("length= %d ",length); int i; printf(" "); quicksort(0,length-1); printf(" "); for(i=0;i<length;i++){ printf("%d " , a[i]); } printf(" "); return 0; }
虽然这种算法烂大街,但是实现一个简洁、正确的算法还是不容易的。