打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c++快速排序
userphoto

2015.12.31

关注
#include<iostream>  
using namespace std;   

//从小到大排序,即升序
int partion(int a[], int low, int high)
{
    int tag=a[low];
    while(low<high){
        while(low<high&&tag<=a[high])
            high--;
        if(low<high){
            a[low]=a[high];
            low++;
        }
        
        while(low<high&&a[low]<=tag)
            low++;
        if(low<high){
            a[high]=a[low];
            high--;
        }
    }
    
    a[low]=tag;
    return low;
}
  
void quickSort(int a[], int low, int high)  
{  
    int position = partion(a, low, high); //划分  
    if(low < high)  
    {  
        quickSort(a, low, position - 1);  
        quickSort(a, position + 1, high);  
    }  
}  
  
int main()  
{  
    int a[] = {4, 5, 1, 3, 2, 0, -3 ,-20, 100, 50};  
    quickSort(a, 0, 10 - 1);  
    int i;  
    for(i = 0; i < 10; i++)  
        cout << a[i] << " ";  
    cout << endl;  
    return 0;  
}  


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
快速排序
插入,冒泡,选择,快速排序,二分查找
C#快速排序类
用VC++类实现快速排序(并输出过程)
几种排序算法
持续输出Java面试题之交换排序
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服