码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序

时间:2015-03-28 21:41:36      阅读:144      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!