码迷,mamicode.com
首页 > 其他好文 > 详细

Qsort

时间:2015-06-28 00:09:30      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

/**快速排序*/
#include<cstdio>
#include<algorithm>
using namespace std;
int a[]={5,2,1,3,4,6,8,9,10};
int f(int l,int h){
    int p=a[l],x=a[l];
    while(l<h){
        while(l<h&&a[h]>=p) --h;
        a[l]=a[h];
        while(l<h&&a[l]<=p) ++l;
        a[h]=a[l];
    }
    a[l]=x;
    return l;
}
void Qs(int l,int h){
    if(l < h){
        int p=f(l,h);
        Qs(l,p-1);
        Qs(p+1,h);
    }
}
int main()
{
    Qs(0,5);
    for(int i=0;i<5;i++) printf("%d ",a[i]);
    return 0;
}

Qsort

标签:

原文地址:http://blog.csdn.net/a197p/article/details/46664873

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!