您的位置:首页 > 其它

快速排序

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: