Interactive demo

Tournament Sort Visualization

Tournament sort lets the elements play a knockout tournament in which the smaller one always wins. The overall winner is the smallest element. After taking it out, only the matches on its way up are played again.

Your browser does not support the HTML5 canvas tag.

Watch the colored path: after a winner leaves, only the matches on its way to the top are replayed.

Playback

Comparisons
0
Writes
0

Array

Legend

  • Element in the array
  • Player in the tournament
  • Replayed match
  • Path of the winner
  • Final position

Runtime and properties

Best case

O(n log n)

Building the bracket and replaying it always cost about log n comparisons per element.

Average case

O(n log n)

n − 1 matches build the bracket, then every winner replays only the log n matches on its way.

Worst case

O(n log n)

The bracket is balanced, whatever the order of the input.

Extra memory

O(n)

The bracket needs a node for every match.

Stable

Yes

On ties the element from the left wins, which came first.

In-place

No

The bracket is built outside the array.

n is the number of elements. O(n log n) is the best a sorting algorithm based on comparisons can achieve.

Compared with the other sorting algorithms

AlgorithmBest caseAverage caseWorst caseExtra memoryStableIn-place
Bubble sortO(n)O(n²)O(n²)O(1)YesYes
Selection sortO(n²)O(n²)O(n²)O(1)NoYes
Insertion sortO(n)O(n²)O(n²)O(1)YesYes
Shell sortO(n log n)≈ O(n1.25)O(n1.5)O(1)NoYes
Tree sortO(n log n)O(n log n)O(n²)O(n)YesNo
Tournament sort this pageO(n log n)O(n log n)O(n log n)O(n)YesNo
HeapsortO(n log n)O(n log n)O(n log n)O(1)NoYes
SmoothsortO(n)O(n log n)O(n log n)O(1)NoYes
Merge sortO(n log n)O(n log n)O(n log n)O(n)YesNo
Patience sortO(n)O(n log n)O(n log n)O(n)YesNo
TimsortO(n)O(n log n)O(n log n)O(n)YesNo
Block sortO(n)O(n log n)O(n log n)O(1)YesYes
QuicksortO(n log n)O(n log n)O(n²)O(log n)NoYes
IntrosortO(n log n)O(n log n)O(n log n)O(log n)NoYes
FluxsortO(n)O(n log n)O(n log n)O(n)YesNo
CrumsortO(n)O(n log n)O(n log n)O(log n)NoYes

1. The bracket

Every element enters as a leaf. In each match the smaller of two elements moves up, so after n − 1 matches the smallest element stands at the top.

2. Take out the winner

The winner goes to the next position of the array. Its leaf is now empty, so every match it took part in has to be decided again.

3. Replay only one path

All other matches keep their result, so only about log n matches are replayed per element. Heapsort uses the same idea, but stores its tree inside the array.