快速排序
2015-03-28 21:02
225 查看
#include <iostream>
using namespace std;
int n;
int a[200];
int Partition(int a[],int startSort,int endSort)
{
int x = a[startSort];
int i = startSort;
int j = endSort + 1;
while(true){
while(a[++i] < x && i < endSort);
while(a[--j] > x);
if(i >= j) break;
swap(a[i],a[j]);
}
a[startSort] = a[j];
a[j] = x;
return j;
}
void QuickSort(int a[],int startSort,int endSort)
{
if(startSort < endSort){
int p = Partition(a,startSort,endSort);
QuickSort(a,startSort,p-1);
QuickSort(a,p+1,endSort);
}
}
int main()
{
cin >> 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] << " ";
}
return 0;
}
using namespace std;
int n;
int a[200];
int Partition(int a[],int startSort,int endSort)
{
int x = a[startSort];
int i = startSort;
int j = endSort + 1;
while(true){
while(a[++i] < x && i < endSort);
while(a[--j] > x);
if(i >= j) break;
swap(a[i],a[j]);
}
a[startSort] = a[j];
a[j] = x;
return j;
}
void QuickSort(int a[],int startSort,int endSort)
{
if(startSort < endSort){
int p = Partition(a,startSort,endSort);
QuickSort(a,startSort,p-1);
QuickSort(a,p+1,endSort);
}
}
int main()
{
cin >> 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] << " ";
}
return 0;
}
相关文章推荐
- 快速排序的算法思想
- MOOC清华《程序设计基础》第5章:快速排序(不分配动态空间-算法一)
- 重温排序之快速排序(qsort)
- 排序算法之快速排序
- 快速排序
- 快速排序及其改进
- 排序---快速排序
- 十大基础排序 · 四 --- 快速排序(不稳定)挖坑填补法+区间分割法+3种优化方法
- C/C++实现快速排序
- 分治法中的合并排序和快速排序
- 唉,用C写一个快速排序 都费劲死了。
- 2017多校第二场 HDU 6040 Hints of sd0061 快速排序
- 一步一步写算法(之快速排序)
- C语言数据结构 快速排序实例详解
- C++随机化快速排序
- javascript之快速排序
- std 中的快速排序
- 链表的快速排序 - 区间分割法
- 递归分治解决快速排序和限行时间选择
- 【转】快速排序