Data Structures and Algorithms

    Analysing MergeSort

    If we are just interested in the running time, we can think of MergeSort as operating like this:

    Mergesort(input of size n)
    {
    Mergesort(input of size n/2)
    Mergesort(input of size n/2)
    Do O(n) work merging the two sorted lists
    }

    ...where the "input of size n/2" is actually either floor(n/2) or ceiling(n/2). Since Big Oh doesn't care which, we will ignore the floors and ceilings and act as if the n is always a power of two.

    In this case, it is easy to see that the recurrence relation

    T(n) = 2T(n/2) + Theta(n)

    describes the running time of the algorithm. You can now use the Master Theorem to analyze this recurrence.



    This document is licensed under Creative Commons copyright "by-nc": anyone may use it for non-commercial purposes with attribution; derived works allowed, with attribution. Original author David Wessels, derivation by Gara Pruesse.