快速排序

思路分析:數組

一、先從數組裏面拿出一個數據(sample),從數組最右邊下標(end)開始比較,直到遇到比sample小的數(min)就把min放到sample的位置上ui

二、接着從數組最左邊下標(begin)開始比較,直到遇到比sample大的數(max),就把max放到前面min的原來的位置上spa

三、一直重複步驟1和2,直到begin下標和end下標相等code

四、接着遞歸分別排序左右兩邊的數blog

 

代碼:排序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void quick_sort(int *array,int begin,int end)
{
    if(begin < end)
    {
      int i=begin,j=end,x;
      x = array[begin];
      while(i < j)
       {
         while( i < j && x <= array[j])
         j--;
         if(i < j)
         array[i++] = array[j];
         while(i < j && x > array[i])
         i++;
         if(i < j)
         array[j++] = array[i];
      }
      array[i] = x; 
      quick_sort(array,begin,i-1);
      quick_sort(array,i+1,end);
    }
}


int main(int argc,char **argv)
{
    int i = 0;
  int array[10] = {1,2,8,97,45,56,77,11,6,100};
    quick_sort(array,0,9);
    printf("hello\n");
    for(i=0;i<10;i++)
    {
        printf("%d ",array[i]);
    }
    return 0;
}
相關文章
相關標籤/搜索