Skip to content

Tournament Merge Sort

Merge-based sorting algorithm that uses a tournament tree to repeatedly select the next smallest element.

Tournament merge sort uses a tournament tree to merge sorted sequences. Each element competes in a structured tree, and the winner at the root is the smallest element. After removing the winner, the tree is updated to find the next smallest.

It is conceptually similar to multiway merge, but uses a tournament structure instead of a heap.

Problem

Given an array AA of length nn, reorder it such that:

A[0]A[1]A[n1] A[0] \le A[1] \le \dots \le A[n-1]

Idea

Build a tournament tree:

  • leaves represent elements or input streams
  • internal nodes store the winner of comparisons
  • the root stores the global minimum

After extracting the minimum:

  1. replace it with the next element from the same source
  2. update the path to the root

This maintains the tournament property.

Algorithm

tournament_merge_sort(A):
    split A into k sorted runs

    build tournament tree over runs

    result = []

    while tree not empty:
        min_element = root value
        append to result

        replace leaf with next element from that run
        update tree

    return result

Tree update:

update_tree(node):
    while node has parent:
        recompute winner at parent
        move upward

Example

Let sorted runs:

  • [1, 4, 7]
  • [2, 5, 8]
  • [3, 6, 9]

Initial tournament:

matchwinner
1 vs 21
3 vs winner1

Extract 1, replace with 4, update tree.

Repeat:

Final result:

[1,2,3,4,5,6,7,8,9] [1,2,3,4,5,6,7,8,9]

Correctness

The tournament tree ensures that the smallest element among all current candidates is at the root. Each update maintains this invariant. Extracting elements in this order produces a sorted sequence.

Complexity

metricvalue
build treeO(k)O(k)
each extractionO(logk)O(\log k)
total mergeO(nlogk)O(n \log k)

Total sort:

O(nlogn) O(n \log n)

Space:

O(k) O(k)

Properties

propertyvalue
stableyes with careful handling
in-placeno
merge structuretournament tree
comparison reuseefficient

Notes

Tournament merge sort reduces repeated comparisons by reusing previous comparison results stored in the tree.

It is closely related to:

  • winner trees
  • loser trees

These structures are widely used in external sorting and database systems.

Implementation

def tournament_merge_sort(runs):
    import math

    k = len(runs)
    indices = [0] * k

    # build tree as array
    size = 1
    while size < k:
        size *= 2

    tree = [None] * (2 * size)

    def get_value(i):
        if i >= k or indices[i] >= len(runs[i]):
            return float("inf")
        return runs[i][indices[i]]

    # initialize leaves
    for i in range(size):
        tree[size + i] = i

    # build tree
    for i in range(size - 1, 0, -1):
        left = tree[2 * i]
        right = tree[2 * i + 1]

        if get_value(left) <= get_value(right):
            tree[i] = left
        else:
            tree[i] = right

    result = []

    while True:
        winner = tree[1]
        val = get_value(winner)

        if val == float("inf"):
            break

        result.append(val)
        indices[winner] += 1

        # update path
        i = size + winner
        while i > 1:
            i //= 2
            left = tree[2 * i]
            right = tree[2 * i + 1]

            if get_value(left) <= get_value(right):
                tree[i] = left
            else:
                tree[i] = right

    return result