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;
    }
  • 相关阅读:
    C语言I博客作业08
    作业7
    作业6
    作业5
    作业--4
    java基础学习--I/O流
    刷题记录--[CISCN2019 华北赛区 Day2 Web1]Hack World
    ADB测试Android真机
    sqli-labs通关笔记
    Tensorflow入门
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8618525.html
Copyright © 2011-2022 走看看