← Back to archive

Data Structure & Algorithm

How quicksort divides and conquers

Follow pivot selection, partitioning, and recursion, and compare average and worst-case complexity.

Read the Korean original on Velog ↗English translation

Data Structure & Algorithm archive cover

The interesting part of quicksort is how one pivot turns a large problem into smaller ones. Each partition follows the same rule until the array is sorted.

Quicksort

Quicksort partitions an array around a pivot and recursively sorts the resulting parts. Its average time complexity is O(n log n), while the worst case is O(n^2). Its strong average performance makes it a widely used sorting approach.

Divide and conquer

Divide a problem into smaller subproblems, solve them recursively, then use those results to solve the original problem.

Recursion A recursive function calls itself to solve a smaller version of the same problem.

The process

  1. Choose an element as the pivot.
  2. Partition the array so smaller elements are on one side and larger elements on the other.
  3. Recursively sort the left and right partitions.
  4. Stop when a partition contains at most one element.
  5. The sorted left partition, pivot, and sorted right partition form the final order.

← Back to archive