Quicksort
2015-11-17 22:48
288 查看
#include<stdio.h> #include<stdlib.h> #include<iostream> #include<algorithm> #define N 1000000 using namespace std; int a ; void quicksort(int x[], int left, int right) { if(left >= right)return; int i = left; int j = right; swap(x[i], x[(rand() % (right - left + 1)) + left]); int key = x[i]; while(i < j){ while(i < j && x[j] >= key)j--; if(i < j)swap(x[i++], x[j]); while(i < j && x[i] <= key)i++; if(i < j)swap(x[i], x[j--]); } quicksort(x, left, i - 1); quicksort(x, i + 1, right); } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0; i < n; i++){ cin >> a[i]; } quicksort(a, 0, n - 1); for(int i = 0; i < n; i++){ cout << a[i] << ' '; } cout << endl; } return 0; }
相关文章推荐
- duilib 如果获取listui 当前选择的项GetCursel
- 线段树(updata+query)
- LXC+PBS+集群调度 msg: 'Bad UID for job execution MSG=connection to mom timed out'
- UI Button
- UI_UITextField_UIButton
- Android源码阅读与理解(二):Android系统Build流程详解
- UI基础__ NSMutableAttributedString
- UIAlertController UIAlertAction (UIAlertView, UIActionSheet)(最好)
- 利用UISlider和 UIImageView实现跑图
- UI导航栏(UINavigation)相关属性left\rightButton,导航栏title,系统时间颜色的设置
- Lingo 做线性规划 - Revenue Management
- Gson-----5、Gson注解和GsonBuilder
- 20.UISlider(滑动按钮)
- UISegmentedControl
- String、StringBuilder、StringBuffer
- valueForkeyPath方法使用
- UITableView的使用
- 19.UISegmentedControl(分段按钮)
- 爬爬爬之路:UI(六) UISegmentedControl UISlider NSTimer的简单应用
- Implement Stack using Queues leetcode oj java