zoukankan      html  css  js  c++  java
  • 快速排序的分治求解方法

    5

    9 1 0 -2 6

    -2 0 1 6 9

    #include <iostream>
    #include <algorithm>
    using  namespace std;
    int a[105];
    int b[105];
    
    int Partition(int data[],int low,int high)
    {
        int temp = data[low];
        int pivotkey=data[low];
        while(low<high)
        {
            while(low<high && data[high]>=pivotkey)
                --high;
            data[low]=data[high];
            while(low<high && data[low]<=pivotkey)
                ++low;
            data[high]=data[low];
        }
        data[low] = temp;
        return low;
    }
    
    void QuickSort(int array[], int first, int last)
    {
        
        if (first < last)
        {
            int i = Partition(array, first, last);
            QuickSort(array, first, i - 1);
            QuickSort(array, i + 1, last);
        }
    }
    
    
    int main()
    {
        int n,x;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        QuickSort(a,1,n);
        for(int i=1;i<=n;i++)
        {
            cout<<a[i]<<" ";
        }
        return 0;
    }
  • 相关阅读:
    常用源代码管理工具与开发工具
    项目发布
    学期总结
    个人博客
    个人博客
    阅读笔记6
    阅读笔记5
    阅读笔记4
    团队代码
    团队代码
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8618525.html
Copyright © 2011-2022 走看看