首页 > 编程语言 > 详细

快速排序算法

时间:2021-01-08 16:51:58      阅读:33      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Solution
{
private:
public:
    int partition(int a[], int left, int right)
    {
        int temp = a[left];

        while (left < right)
        {

            while (left < right && a[right] >= temp)
            {
                right--;
            }

            a[left] = a[right];

            while (left < right && a[left] <= temp)
            {
                left++;
            }

            a[right] = a[left];
        }

        a[left] = temp;

        return left;
    }

    void quickSort(int a[], int left, int right)
    {
        if (left < right)
        {
            int low = left;
            int high = right;
            int temp = a[low];

            while (low < high)
            {

                while (low < high && a[high] >= temp)
                {
                    high--;
                }

                a[low] = a[high];

                while (low < high && a[low] <= temp)
                {
                    low++;
                }

                a[high] = a[low];
            }

            a[low] = temp;
            int pivot = low;

            quickSort(a, left, pivot - 1);
            quickSort(a, pivot + 1, right);
        }
    }
};

int main()
{
    Solution sol = Solution();

    int a[] = {4, 3, 1, 5, 3};

    sol.quickSort(a, 0, 5);

    cout << "hehe" << endl;
}

  

快速排序算法

原文:https://www.cnblogs.com/wlqsmiling/p/14251348.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!