zoukankan      html  css  js  c++  java
  • 快排的改进

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int selectPartition(vector<int>& arr, int low, int high)
    {
        int mid = low + ((high - low) >> 1);
        if (arr[mid] > arr[high])
        {
            swap(arr[mid], arr[high]);
        }
        if (arr[low] > arr[high])
        {
            swap(arr[low], arr[high]);
        }
        // low的位置上保存三个位置的中间值
        if (arr[mid] > arr[low])
        {
            swap(arr[mid], arr[low]);
        }
    
    }
    
    int partition(vector<int>& a, int low, int high)
    {
        selectPartition(a, low, high);
        int pivot = a[low];
        // int pivot = a[low];
    
        while (low < high)
        {
            while (low < high && a[high] >= pivot) high--;
            a[low] = a[high];
            while (low < high && a[low] <= pivot) low++;
            a[high] = a[low];
        }
        a[low] = pivot;
        return low;
    }
    
    void Qsort(vector<int>& a, int low, int high)
    {
        int pivotloc;
        if (low < high)
        {
            pivotloc = partition(a, low, high);
            Qsort(a, low, pivotloc - 1);
            Qsort(a, pivotloc + 1, high);
        }
    }
    
    void QuickSort(vector<int>& a, int low, int high)
    {
        Qsort(a, low, high - 1);   
    }
    
    int main()
    {
        vector<int> a = {1, -2, 3, 55, -23, 0, -23, 44, 2, 55, 99, 23, -40};
        QuickSort(a, 0, a.size());
        for(auto e : a)
        {
            cout << e << " ";
        }
        cout << endl;
        return 0;
    }
    
    

    https://blog.csdn.net/hacker00011000/article/details/52176100

  • 相关阅读:
    react 学习
    swiper
    日历插件
    插件 uploadify
    前端 websocket用法
    jQuery插件Highcharts
    可用于nodejs的SuperAgent(ajax API)
    git的基本命令
    自己对javascript闭包的了解
    函数中的this的四种绑定形式
  • 原文地址:https://www.cnblogs.com/douzujun/p/15178082.html
Copyright © 2011-2022 走看看