Quick Sort

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Quick Sort

Approach

Choose the last element as pivot. Partition the array so all elements less than the pivot are on the left and all greater are on the right. The pivot lands in its correct final position. Recursively sort the two partitions. The partition function is the key operation.

Complexity Analysis

Time
O(n log n) average, O(n²) worst
Space
O(log n)

Pattern

Partition Sort

Why It Works

Partition places the pivot in its correct position in O(n) time. On average, the pivot splits the array roughly in half, giving O(log n) recursive levels. Worst case occurs when the pivot is always the min or max (sorted input), giving O(n) levels.

Updated Feb 2026