快速排序法

2018-10-25  本文已影响0人  石器时代小古董

思路:找到一个基准值,比基准值小的在左边,比基准值大的在右边
1.从前往后对比start和key,如果start比key大,让start和end调换位置
2.从后往前end比key小,start和end调换位置

void sort(int a[],int low,int hight){
   int start=low;
   int end=higth;
   int key=a[low];
   while(end>start){
   //从前往后查找比关键值大的
       while(end>start&&key<=start){
          start++;
        }
      if(key>a[start]){
       int temp=a[start];
        a[start]=a[end];
        a[end]=temp;
       }
  }
  //查找比key小的,如果比key大,忽略,查找下一个
  wihle(key>=a[end]){
   end--;
   }
//让起始位置和最后位置的数据进行交换
   if(a[end]<key){
       int temp=a[end];
       a[end]=a[start];
       a[start]=temp
   }
}
if(start>low){
  sort( a,low,start-1);
}
if(end<height){
  sort(a,end+1,height);
}
上一篇 下一篇

猜你喜欢

热点阅读