Sorting Algorithm Animations

Problem Size:  20 · 30 · 40 · 50     Magnification:  1x · 2x · 3x
Algorithm:  Insertion · Selection · Bubble · Shell · Merge · Heap · Quick · Quick3
Initial Condition:  Random · Nearly Sorted · Reversed · Few Unique

Insertion

Selection

Bubble

Shell

Merge

Heap

Quick

Quick3

Random

Nearly Sorted

Reversed

Few Unique

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.

Directions

  • Click on above to restart the animations in a row, a column, or the entire table.
  • Click directly on an animation image to start or restart it.
  • Click on a problem size number to reset all animations.

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).

References

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

Programming Pearls by Jon Bentley. Addison Wesley, 1986.

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

Comments

To post a comment, you must log in.
Thanks!
— posted by someone on 25-Jul-2010
pls could you add animations for the sorted condition + special worst case animations like quicksort with a sorted list and the first or last element as pivot. thank you
— posted by someone on 8-Jul-2010
nice..but a difficult to understand, hopefully, I can implement it on my application for the present.
— posted by someone on 14-Jun-2010
If the animations are too fast for you, please try a different browser (such as Google Chrome). Different browsers play animated GIFs at different speeds, and there's unfortunately no way to control that from javascript or HTML. Why did I use animated GIFs? Because they are small and lightweight, so it's possible to have many on the same page.
— posted by someone on 2-May-2010
If the animations are too small for you, then please note the magnification links at the top of the page.
— posted by someone on 2-May-2010
Hi there... to site owner, i my self wrote my own sorting algorithm which is totally a new kind of concept and very easy to understand but its very very fast sorting method, faster than quick sort and heap sort. Do you think you can include my sort on this site.
— posted by someone on 27-Apr-2010
The animated diagram is exellent,but its too fast! moreover, it is also small.
— posted by someone on 10-Apr-2010
What you've done here is extremely helpful. My data structures instructor was very impressed and found it useful to show students the differences between algorithms.
— posted by someone on 8-Apr-2010
I frequently get asked to add radix sort to this site. It's deliberately omitted since I wanted to limit the scope of the site to generic comparison-based sorting algorithms. Radix sorting is definitely a large and interesting topic on its own right.
— posted by someone on 31-Mar-2010
Radix sort would be good, should produce O(N) performance, and is stable, but a bit more complex. Can be adapted to floats and speed is proportional to the number of bytes.
— posted by someone on 14-Mar-2010
Nice wensite
— posted by someone on 27-Jan-2010
Hi I've finished the dev off a new sorting algorithme witch I called "A.L.E.X." sins few days , and i wona know how to do to register/protect it under a copyright licence. samir L. 2010
— posted by someone on 13-Jan-2010
Two-way bubble sort (shaker sort) is a variation on bubble sort, and isn't different enough (or useful enough) to merit being included here in my opinion. Shaker sort has all the same analytical properties of bubble sort, and is slightly faster in some instances, but shaker sort does not get used in practice as far as I know.
— posted by someone on 20-Dec-2009
hey, may you add the two-way bubblesorting to the comparsion chart?
— posted by someone on 8-Dec-2009