快排

来源:互联网 发布:写作文软件 编辑:程序博客网 时间:2024/04/27 19:23
////////////////////////////////////////// description: quick sort algorithm// created by : kangquan2008@scut////////////////////////////////////////#include<stdio.h>#include<stdlib.h>int partition(int data[],int low,int high){int pivot_key = data[low];while(low < high){while(low<high && data[high] >= pivot_key)//find a data which is smaller the the pivot,//and we should begin the high first,//because we store the low data:pivot_key = data[low]high--;data[low] = data[high];while(low<high && data[low] <= pivot_key)// find a bigger datalow++;data[high] = data[low];}// when low == high,it stopsdata[low] = pivot_key; // data[high] = pivot_key; is ok too!return low;}// partitionvoid quick_sort(int data[], int low, int high){if(low < high)// the recursion ends: mid=0 ,mid-1=-1,so the condition should not just be low==high{int mid;mid = partition(data,low,high);quick_sort(data,low,mid-1);quick_sort(data,mid+1,high);}}// quick_sortvoid display(int data[], int size){int i;for( i=0; i<size; i++){printf("%d ",data[i]);}printf("\n");}int main(){int data[]={1,4,6,8,3,657,342,1231,12315,879,9090,23};display(data,sizeof(data)/sizeof(data[0]));quick_sort(data,0,sizeof(data)/sizeof(data[0])-1);display(data,sizeof(data)/sizeof(data[0]));return 0;}

 
原创粉丝点击