快速排序算法

#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;
}

  

posted on 2021-01-08 14:46  wlqsmiling  阅读(65)  评论(0编辑  收藏  举报

导航