Hire The World’s
Top Talent On Demand

Toptal connects the top 3% of freelance developers all over the world.

Sorting Algorithms Animations

The following animations illustrate how effectively data sets from different starting points can be sorted using different algorithms.

Share

How to use: Press “Play all”, or choose the button for the individual row/column to animate.

InsertionSelectionBubbleShellMergeHeapQuickQuick3
Random
Insertion on Random
Selection on Random
Bubble on Random
Shell on Random
Merge on Random
Heap on Random
Quick on Random
Quick3 on Random
Nearly Sorted
Insertion on Nearly Sorted
Selection on Nearly Sorted
Bubble on Nearly Sorted
Shell on Nearly Sorted
Merge on Nearly Sorted
Heap on Nearly Sorted
Quick on Nearly Sorted
Quick3 on Nearly Sorted
Reversed
Insertion on Reversed
Selection on Reversed
Bubble on Reversed
Shell on Reversed
Merge on Reversed
Heap on Reversed
Quick on Reversed
Quick3 on Reversed
Few Unique
Insertion on Few Unique
Selection on Few Unique
Bubble on Few Unique
Shell on Few Unique
Merge on Few Unique
Heap on Few Unique
Quick on Few Unique
Quick3 on Few Unique
RandomNearly SortedReversedFew Unique
Insertion
Insertion on Random
Insertion on Nearly Sorted
Insertion on Reversed
Insertion on Few Unique
Selection
Selection on Random
Selection on Nearly Sorted
Selection on Reversed
Selection on Few Unique
Bubble
Bubble on Random
Bubble on Nearly Sorted
Bubble on Reversed
Bubble on Few Unique
Shell
Shell on Random
Shell on Nearly Sorted
Shell on Reversed
Shell on Few Unique
Merge
Merge on Random
Merge on Nearly Sorted
Merge on Reversed
Merge on Few Unique
Heap
Heap on Random
Heap on Nearly Sorted
Heap on Reversed
Heap on Few Unique
Quick
Quick on Random
Quick on Nearly Sorted
Quick on Reversed
Quick on Few Unique
Quick3
Quick3 on Random
Quick3 on Nearly Sorted
Quick3 on Reversed
Quick3 on Few Unique

KEY

  • Black values are sorted.
  • Gray values are unsorted.
  • A red triangle marks the algorithm position.
  • Dark gray values denote the current interval (shell, merge, quick).
  • A pair of red triangles marks the left and right pointers (quick).

DISCUSSION

These pages show 8 different sorting algorithms on 4 different initial conditions. These visualizations are intended to:

  • Show how each algorithm operates.
  • Show that there is no best sorting algorithm.
  • Show the advantages and disadvantages of each algorithm.
  • Show that worse-case asymptotic behavior is not always the deciding factor in choosing an algorithm.
  • Show that the initial condition (input order and key distribution) affects performance as much as the algorithm choice.

The ideal sorting algorithm would have the following properties:

  • Stable: Equal keys aren’t reordered.
  • Operates in place, requiring O(1) extra space.
  • Worst-case O(n·lg(n)) key comparisons.
  • Worst-case O(n) swaps.
  • Adaptive: Speeds up to O(n) when data is nearly sorted or when there are few unique keys.

There is no algorithm that has all of these properties, and so the choice of sorting algorithm depends on the application.

Sorting is a vast topic; this site explores the topic of in-memory generic algorithms for arrays. External sorting, radix sorting, string sorting, and linked list sorting—all wonderful and interesting topics—are deliberately omitted to limit the scope of discussion.

Preparing for a technical interview? Check out our interview guides.

References

Algorithms in Java, Parts 1-4, 3rd edition by Robert Sedgewick. Addison Wesley, 2003.

Quicksort is Optimal by Robert Sedgewick and Jon Bentley, Knuthfest, Stanford University, January, 2002.

Dual Pivot Quicksort: Code by Discussion.

Bubble-sort with Hungarian (“Csángó”) folk dance YouTube video, created at Sapientia University, Tirgu Mures (Marosvásárhely), Romania.

Select-sort with Gypsy folk dance YouTube video, created at Sapientia University, Tirgu Mures (Marosvásárhely), Romania.

Sorting Out Sorting, Ronald M. Baecker with the assistance of David Sherman, 30 minute color sound film, Dynamic Graphics Project, University of Toronto, 1981. Excerpted and reprinted in SIGGRAPH Video Review 7, 1983. Distributed by Morgan Kaufmann, Publishers. Excerpt.