zoukankan      html  css  js  c++  java
  • 分治法之快速排序

    //program 3-3
    #include <iostream>
    using namespace std;
    int Partition(int r[],int low,int high)//划分函数
    {
        int i=low,j=high,pivot=r[low];//基准元素
        while(i<j)
        {
            while(i<j&&r[j]>pivot) j--;//向左扫描
            if(i<j)
            {
                swap(r[i++],r[j]);//r[i]和r[j]交换后i+1右移一位
            }
            while(i<j&&r[i]<=pivot) i++;//向右扫描
            if(i<j)
            {
                swap(r[i],r[j--]);//r[i]和r[j]交换 后j-1左移一位
            }
        }
        return i;//返回最终划分完成后基准元素所在的位置
    }
    
    int Partition2(int r[],int low,int high)//划分函数
    {
        int i=low,j=high,pivot=r[low];//基准元素
        while(i<j)
        {
            while(i<j&&r[j]>pivot) j--;//向左扫描
            while(i<j&&r[i]<=pivot) i++;//向右扫描
            if(i<j)
            {
                swap(r[i++],r[j--]);//r[i]和r[j]交换
            }
        }
        if(r[i]>pivot)
        {
            swap(r[i-1],r[low]);//r[i-1]和r[low]交换
            return i-1;//返回最终划分完成后基准元素所在的位置
        }
        swap(r[i],r[low]);//r[i]和r[low]交换
        return i;//返回最终划分完成后基准元素所在的位置
    }
    
    void QuickSort(int R[],int low,int high)//实现快排算法
    {
        int mid;
        if(low<high)
        {
            mid=Partition(R,low,high); //基准位置
            for(int i=low;i<=high;i++)
              cout<<R[i]<<" ";
            cout<<endl;
            QuickSort(R,low,mid-1);//左区间递归快排
            QuickSort(R,mid+1,high);//右区间递归快排
        }
    }
    int main()
    {
        int a[100];
        int i,N;
        cout<<"请先输入要排序的数据的个数:";
        cin>>N;
        cout<<"请输入要排序的数据:";
        for(i=0;i<N;i++)
            cin>>a[i];
        cout<<endl;
        QuickSort(a,0,N-1);
        cout<<"排序后的序列为:"<<endl;
        for(i=0;i<N;i++)
            cout<<a[i]<<" " ;
        cout<<endl;
        return 0;
    }
    无欲则刚 关心则乱
  • 相关阅读:
    Python 三级菜单
    linux 下按文件类型删除
    linux 做内网端口映射
    ss
    fio
    libXtst.so.6 is needed by teamviewer-12.0.76279-0.i686
    copy 浅复制 与深复制
    Git 使用方法
    关于 爬虫使用 urllib.urlopen 提交默认 User-Agent值
    Python 官方模块文档
  • 原文地址:https://www.cnblogs.com/xjyxp/p/11315898.html
Copyright © 2011-2022 走看看