• 快速排序2


    #include <iostream>
    
    using namespace std;
    const int N = 10;
    int data[]= {6,1,2,7,9,3,4,5,10,8};
    void quickSort(int* data, int low,int high);
    void printArray(int* data, int n);
    int partition(int* data,int low, int high);
    int main()
    {
        quickSort(data,0,N-1);
        printArray(data,N);
        return 0;
    }
    
    void quickSort(int* data, int low, int high)
    {
        if(low>=high) return;
        int key = partition(data,low,high);
        quickSort(data,low,key-1);
        quickSort(data,key+1,high);
    
    }
    void printArray(int* data, int n)
    {
        for(int i=0;i<n;++i)
        {
    
            cout<<data[i]<<"	";
        }
        cout<<endl;
    }
    int partition(int* data,int l, int h)
    {
        int low=l,high=h;
        int temp = data[low];
        while(low<high)
        {
            while(low<high && data[high]>=temp) --high;
            while(low<high && data[low]<=temp) ++low;
    
            if(low<high)
            {
                data[low] ^= data[high];
                data[high] ^= data[low];
                data[low] ^= data[high];
            }
        }
    
        data[l] = data[low];
        data[low] = temp;
        return low;
    
    
    }
    
  • 相关阅读:
    Python自动化运维答疑解惑
    MySQL基础
    Centos下常用MySQL语法
    PDO
    生成静态页面的好处
    页面纯静态
    源码安装LNMP
    Nginx URL重写(rewrite)
    防盗链
    自定义菜单
  • 原文地址:https://www.cnblogs.com/yldf/p/6249896.html
Copyright © 2020-2023  润新知