快速排序
2016-02-26 12:28
239 查看
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; int a[100]; void quicksort(int l,int r) { int i,j,t,x; if(l>r) return ; i=l,j=r; t=a[l];//t中存基准数 while(i!=j) { while(a[j]>=t&&i<j) j--;// >= while(a[i]<=t&&i<j) i++; if(i!=j)//当哨兵i和哨兵j没有相遇时 { x=a[i]; a[i]=a[j]; a[j]=x; } } a[l]=a[i];//最终将基准数归位 a[i]=t; quicksort(l,i-1); quicksort(i+1,r); } int main() { int i,n; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); quicksort(0,n-1); for(i=0;i<n;i++) printf("%d ",a[i]); }
相关文章推荐
- Leet Code OJ 292. Nim Game [Difficulty: Easy]
- 【设计模式】10、外观模式
- Linux 下的(防火墙)iptables
- UVa Dragon of Loowater 11292 (贪心)
- Xcode 报错 ERROR ITMS-90096,启动图黑屏
- MongoDB 博客截图之一
- js获取页面及个元素高度、宽度
- 《Effective C++》考虑virtual函数以外的其他选择
- 用实例分析listener filter serlvet
- c#版三层登录
- BUG归因
- 前台获取信息
- Switch between Server Core mode and Server GUI mode in Windows Server 2012 R2
- bzoj 1003: [ZJOI2006]物流运输trans 最短路+dp
- Sublime Text3安装emmet插件
- iOS UIAlertController弹框(OS9.0 后替代UIAlertView弹框和UIActionSheet下弹框)
- Android:自定义View(随机生成验证码)
- peration not supported. Unknown error: 0x8973190e
- Linux内核分析——计算机是如何工作的
- Java 位运算和运算符总结