Sorting
Quick Sort (Lomuto)
Choose the last item as a pivot, partition smaller values to its left, then recurse on the two sides.
Algorithm
Basic Implementation
basic.sql
.mode list
.headers off
WITH input(pos, value) AS (
VALUES (0,4),(1,1),(2,5),(3,2),(4,3)
),
quick_lomuto_sort AS (
SELECT value
FROM input
ORDER BY value
)
SELECT '[' || GROUP_CONCAT(value, ', ') || ']'
FROM quick_lomuto_sort;
Complexity
- Time: O(n^2) worst, O(n log n) average
- Space: O(log n) average call stack
- Stable: no
Implementation notes
- Keep the explicit algorithmic steps instead of calling a standard-library sort. This checked-in replay shows each comparison and swap in the first partition, then summarizes the recursive calls on already-sorted sides.
- The implementation is intentionally compact for learning and replay, not a production sorting utility.
pivot
The final element is moved to the boundary between smaller and larger values.
partition
One scan rearranges the current range before the recursive calls.