会员
周边
众包
新闻
博问
闪存
赞助商
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
1989huangkq
huangkq1989
博客园
|
首页
|
新随笔
|
新文章
|
联系
|
订阅
|
管理
快排
////////////////////////////////////// // // 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; }
发表于
2011-08-28 14:30
huangkq1989
阅读(
117
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告