Algo-Sim › Sorting & Divide and Conquer

Quick Sort (Lomuto)

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

Partition around the last element so the pivot lands in its final place, then recurse on both sides. Simple, but slow on sorted input and swaps a lot.

Problem

Sort nums into ascending order with quicksort, using the Lomuto partition: the last element is the pivot, smaller values are moved to its left, and the pivot then lands in its final position before both sides are sorted recursively.

Constraints

Edge cases to test

Follow-up questions

How do you avoid the O(n²) case?
Pick a random pivot (or median of three) and use three-way partitioning for many duplicates: O(n log n) expected.
How do you bound the recursion depth?
Recurse on the smaller side and loop on the larger: O(log n) stack. Introsort switches to heap sort if depth grows too large.

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 - 1);
    quickSort(nums, p + 1, hi);
}

// Lomuto: pivot = last element; nums[lo..i-1] holds the values below it.
private int partition(int[] nums, int lo, int hi) {
    int pivot = nums[hi], i = lo;
    for (int j = lo; j < hi; j++) {
        if (nums[j] < pivot) {
            int t = nums[i]; nums[i] = nums[j]; nums[j] = t;
            i++;
        }
    }
    int t = nums[i]; nums[i] = nums[hi]; nums[hi] = t;
    return i;
}

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 - 1)
    quick_sort(nums, p + 1, hi)


# Lomuto: pivot = last element; nums[lo..i-1] holds the values below it.
def partition(nums, lo, hi):
    pivot, i = nums[hi], lo
    for j in range(lo, hi):
        if nums[j] < pivot:
            nums[i], nums[j] = nums[j], nums[i]
            i += 1
    nums[i], nums[hi] = nums[hi], nums[i]
    return 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 - 1);
        quickSort(nums, p + 1, hi);
    }

    // Lomuto: pivot = last element; nums[lo..i-1] holds the values below it.
    int partition(vector<int> &nums, int lo, int hi) {
        int pivot = nums[hi], i = lo;
        for (int j = lo; j < hi; j++) {
            if (nums[j] < pivot) {
                int t = nums[i];
                nums[i] = nums[j];
                nums[j] = t;
                i++;
            }
        }
        int t = nums[i];
        nums[i] = nums[hi];
        nums[hi] = t;
        return i;
    }
};

More Sorting & Divide and Conquer problems