Browse Curriculum

Quick Sort

Learn Quick Sort, an efficient, in-place sorting algorithm that is the standard for most real-world sorting applications.

Pivot
Compare
Sorted
10
80
30
90
40
50
70

Ready to sort

1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.
Unlock VisualizerPREMIUM FEATURE

Intuition

Pick a "leader" (pivot). Ask everyone smaller than the leader to stand on the left, and everyone taller to stand on the right. The leader is now in their exact final spot (sorted). Repeat for the groups on the left and right.

Concept

  • Partitioning: The key operation. Reorders array so elements < pivot are left, elements > pivot are right.
  • Divide & Conquer: Sorts the two partitions recursively.
  • In-Place: Requires minimal extra memory (O(log N) stack space) compared to Merge Sort.

How it Works

Algorithm:

  1. Choose a Pivot (e.g., last element).
  2. Traverse array: if element < Pivot, swap it to the front.
  3. Place Pivot after the last 'smaller' element.
  4. Recursively apply to left and right sub-arrays.

Step-by-Step Breakdown

Watch the visualization:

  • Blue Bar (Pivot): The element used for partitioning.
  • Orange Bar: Element being compared.
  • Red Bar: Swap occurring to move smaller element to left.
  • Green: Sorted partition.

When to Use

  • General Purpose: Fastest sorting algorithm for randomized data on average.
  • Memory Constraints: Preferred over Merge Sort when O(N) extra space is not available.
  • Arrays: Better locality of reference (cache-friendly) than Heap Sort.

When NOT to Use

  • Worst Case O(N²): Not suitable if data is already sorted and bad pivot choice is made (can be mitigated by randomizing pivot).
  • Stable Sort Required: Quick Sort is unstable.

How to Identify

"In-place sort", "Kth Largest Element (QuickSelect)".

Stop Guessing, Start Mastering.

Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.

Start Your Premium Prep