Algo-Sim › Sorting & Divide and Conquer

Quick Sort (Hoare)

Medium · Sorting & Divide and Conquer · step through it in Java, Python and C++ on your own input

Partition with two pointers moving inward from both ends, swapping pairs on the wrong side of the middle value. Fewer swaps than Lomuto, and fine on sorted input.

Problem

Sort nums into ascending order with quicksort, using the Hoare partition: two indices move inward from both ends and swap pairs that sit on the wrong side of the pivot value, then each part is sorted recursively.

Constraints

Edge cases to test

Follow-up questions

Why does Hoare do fewer swaps than Lomuto?
It swaps only pairs that are both on the wrong side, about three times fewer swaps on average.
Where does the pivot end up?
Not necessarily at the split index; recurse on [lo, j] and [j + 1, hi].

Solution

Java

public int[] sortArray(int[] nums) {
    quickSort(nums, 0, nums.length - 1);
    return nums;
}

private void quickSort(int[] nums, int lo, int hi) {
    if (lo >= hi) return;
    int p = partition(nums, lo, hi);
    quickSort(nums, lo, p);
    quickSort(nums, p + 1, hi);
}

// Hoare: two pointers walk inward and swap pairs on the wrong side of the pivot value.
private int partition(int[] nums, int lo, int hi) {
    int pivot = nums[(lo + hi) / 2], i = lo - 1, j = hi + 1;
    while (true) {
        do i++; while (nums[i] < pivot);
        do j--; while (nums[j] > pivot);
        if (i >= j) return j;
        int t = nums[i]; nums[i] = nums[j]; nums[j] = t;
    }
}

Python

def sort_array(nums):
    quick_sort(nums, 0, len(nums) - 1)
    return nums


def quick_sort(nums, lo, hi):
    if lo >= hi:
        return
    p = partition(nums, lo, hi)
    quick_sort(nums, lo, p)
    quick_sort(nums, p + 1, hi)


# Hoare: two pointers walk inward and swap pairs on the wrong side of the pivot value.
def partition(nums, lo, hi):
    pivot, i, j = nums[(lo + hi) // 2], lo - 1, hi + 1
    while True:
        i += 1
        while nums[i] < pivot:
            i += 1
        j -= 1
        while nums[j] > pivot:
            j -= 1
        if i >= j:
            return j
        nums[i], nums[j] = nums[j], nums[i]

C++

#include <bits/stdc++.h>
using namespace std;

class Solution {
  public:
    vector<int> sortArray(vector<int> &nums) {
        quickSort(nums, 0, int(nums.size()) - 1);
        return nums;
    }

    void quickSort(vector<int> &nums, int lo, int hi) {
        if (lo >= hi)
            return;
        int p = partition(nums, lo, hi);
        quickSort(nums, lo, p);
        quickSort(nums, p + 1, hi);
    }

    // Hoare: two pointers walk inward and swap pairs on the wrong side of the pivot value.
    int partition(vector<int> &nums, int lo, int hi) {
        int pivot = nums[(lo + hi) / 2], i = lo - 1, j = hi + 1;
        while (true) {
            do
                i++;
            while (nums[i] < pivot);
            do
                j--;
            while (nums[j] > pivot);
            if (i >= j)
                return j;
            int t = nums[i];
            nums[i] = nums[j];
            nums[j] = t;
        }
    }
};

More Sorting & Divide and Conquer problems