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
- 1 ≤ nums.length ≤ 5 · 10⁴ in the LeetCode version (Sort an Array)
- -5 · 10⁴ ≤ nums[i] ≤ 5 · 10⁴
- O(n log n) on average, O(n²) on already-sorted input with this pivot choice
Edge cases to test
- Sorted input with the last element as pivot: O(n²)
- All equal values
- Two elements
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 iC++
#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
- Bubble Sort (Easy)
- Insertion Sort (Easy)
- Selection Sort (Easy)
- Merge Sort (Medium)
- Quick Sort (Hoare) (Medium)
- Counting Sort (Easy)
- Radix Sort (LSD) (Medium)
- Bucket Sort (Medium)
- Kth Largest Element (Quickselect) (Medium)
- Count Inversions (Hard)
- Largest Number (Medium)