快排

// // // 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值