Ruby Program for Quick Sort Algorithm Example
quick_sort(array) calling itself on subarrays.Examples
How to Think About It
Algorithm
Code
def quick_sort(arr) return arr if arr.length <= 1 pivot = arr.pop left = arr.select { |x| x < pivot } right = arr.select { |x| x >= pivot } quick_sort(left) + [pivot] + quick_sort(right) end puts quick_sort([3, 1, 4, 1, 5]).inspect
Dry Run
Let's trace quick_sort([3, 1, 4, 1, 5]) through the code
Initial call
Array: [3, 1, 4, 1, 5], pivot = 5, left = [3, 1, 4, 1], right = []
Sort left side
Call quick_sort([3, 1, 4, 1])
Left side pivot
Array: [3, 1, 4, 1], pivot = 1, left = [], right = [3, 4, 1]
Sort right side of left
Call quick_sort([3, 4, 1])
Right side pivot
Array: [3, 4, 1], pivot = 1, left = [], right = [3, 4]
Sort right side of right
Call quick_sort([3, 4])
Right side pivot
Array: [3, 4], pivot = 4, left = [3], right = []
Sort left side of right side
Call quick_sort([3]) returns [3]
Combine
Combine [3] + [4] + [] = [3, 4]
Combine
Combine [] + [1] + [3, 4] = [1, 3, 4]
Combine
Combine [] + [1] + [1, 3, 4] = [1, 1, 3, 4]
Combine final
Combine [1, 1, 3, 4] + [5] + [] = [1, 1, 3, 4, 5]
| Call | Pivot | Left | Right | Result |
|---|---|---|---|---|
| [3,1,4,1,5] | 5 | [3,1,4,1] | [] | [1,1,3,4,5] |
| [3,1,4,1] | 1 | [] | [3,4,1] | [1,1,3,4] |
| [3,4,1] | 1 | [] | [3,4] | [1,3,4] |
| [3,4] | 4 | [3] | [] | [3,4] |
| [3] | - | - | - | [3] |
Why This Works
Step 1: Choosing a pivot
The pivot divides the array into smaller and larger parts, helping to sort by comparison.
Step 2: Splitting the array
Elements less than the pivot go left, others go right, so sorting is easier on smaller parts.
Step 3: Recursion
Calling quick_sort on smaller parts repeats the process until arrays are tiny and sorted.
Step 4: Combining results
Joining sorted left, pivot, and sorted right gives the fully sorted array.
Alternative Approaches
def quick_sort_inplace(arr, low=0, high=nil) high ||= arr.length - 1 if low < high p = partition(arr, low, high) quick_sort_inplace(arr, low, p - 1) quick_sort_inplace(arr, p + 1, high) end arr end def partition(arr, low, high) pivot = arr[high] i = low - 1 (low...high).each do |j| if arr[j] < pivot i += 1 arr[i], arr[j] = arr[j], arr[i] end end arr[i + 1], arr[high] = arr[high], arr[i + 1] i + 1 end puts quick_sort_inplace([3,1,4,1,5]).inspect
arr = [3, 1, 4, 1, 5] puts arr.sort.inspect
Complexity: O(n log n) average, O(n^2) worst time, O(n) space
Time Complexity
Quick sort divides the array and sorts parts recursively, averaging O(n log n) but can degrade to O(n^2) if pivot choices are poor.
Space Complexity
This implementation uses extra arrays for left and right parts, so it needs O(n) space; in-place versions use O(log n) space.
Which Approach is Fastest?
In-place quick sort is faster and uses less memory but is harder to implement; Ruby's built-in sort is optimized and usually fastest in practice.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Recursive quick sort with extra arrays | O(n log n) average | O(n) | Learning and clarity |
| In-place quick sort | O(n log n) average | O(log n) | Performance and memory efficiency |
| Ruby built-in sort | O(n log n) | O(n) | Practical use and simplicity |