//////////////////////////////////////
//
// 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 data
low++;
data[high] = data[low];
}// when low == high,it stops
data[low] = pivot_key; // data[high] = pivot_key; is ok too!
return low;
}// partition
void 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_sort
void 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;
}
转载于:https://www.cnblogs.com/huangkq1989/archive/2011/08/28/2522643.html