• 排序算法——快速排序


    图示思想:

    int Partition(int* a, int low, int high)
    {
        int pivotkey = a[low];
        while (low < high)
        {
            while (low < high && a[high] >= pivotkey)
                high--;
            a[low] = a[high];
            while (low < high && a[low] <= pivotkey)
                low++;
            a[high] = a[low];
        }
        a[low] = pivotkey;
        return low;
    }
    void QuickSort(int* a, int low, int high)
    {
        if (low < high)
        {
            int pivotloc = Partition(a, low, high);
            QuickSort(a, low, pivotloc-1);
            QuickSort(a, pivotloc+1, high);
        }
    }
    
    int main()
    {
        int arr[] = { 9,2,3,1,5,4,7,8,6 };
        int n = sizeof(arr) / sizeof(int);
    
        int low = 0, high = n - 1;
        QuickSort(arr, low,high);
    
    
        for (int i = 0; i < n; i++)
        {
            cout << arr[i] << " ";
        }
        cout << endl;
        return 0;
    }
  • 相关阅读:
    内置对象
    js作用域对象与运用技巧
    js流程控制与函数
    JavaScript基础
    CSS3新增属性2
    CSS3新增
    前端实践
    浮动与定位
    页面布局
    表格表单视频音频
  • 原文地址:https://www.cnblogs.com/single-dont/p/11354670.html
Copyright © 2020-2023  润新知