Skip to content
UPEEC

Sorting Algorithms

Arrange data in order — and why the choice of algorithm matters.

Easy
Comments 0
AlgorithmBestAverageWorstStableExtra memory
Bubblennn2n^2n2n^2yesO(1)O(1)
Insertionnnn2n^2n2n^2yesO(1)O(1)
Mergenlog⁡nn\log nnlog⁡nn\log nnlog⁡nn\log nyesO(n)O(n)
Quicknlog⁡nn\log nnlog⁡nn\log nn2n^2noO(log⁡n)O(\log n)
Heapnlog⁡nn\log nnlog⁡nn\log nnlog⁡nn\log nnoO(1)O(1)

A stable sort keeps equal items in their original order. Real libraries use hybrids like Timsort (Python, Java objects) and introsort (C++).

Sub-topics (3)

Bubble Sort

Repeatedly swap adjacent out-of-order elements until the list is sorted.

Beginner♥ 0🔖 0💬 0

Merge Sort

Divide the list in half, sort each half, then merge — always O(n log n).

Intermediate♥ 0🔖 0💬 0

Quick Sort

Pick a pivot, partition smaller and larger elements around it, and recurse.

Intermediate♥ 0🔖 0💬 0

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.