首页 > 编程语言 > 详细

快速排序

时间:2015-03-28 21:38:18      阅读:284      评论:0      收藏:0      [点我收藏+]
#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;
}

快速排序

原文:http://www.cnblogs.com/chunbiao/p/4374812.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!