SteelReed

Search

Search posts, questions…

Collections & DSAQuestion 7 / 25

Junior

How do merge sort and quicksort work, and how do they differ?

Short answer

Both are divide and conquer. Merge sort halves and merges: always O(n log n), stable, using O(n) extra memory. Quicksort splits around a pivot: fast on average but O(n²) in the worst case.

Merge sortQuicksortArray of n itemsSplit in halfsort each halfMerge both halvesneeds O(n) memoryPick pivot, partitionsmaller left, larger rightRecurse both sidesin place

3 points to say:

  1. Merge sort is stable, quicksort is not.
  2. Quicksort sorts in place and reads memory well.
  3. A random pivot avoids O(n²).

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.