...

/

Analysis of Merge Sort

Analysis of Merge Sort

We'll cover the following...

Given that the merge function runs in Θ(n)\Theta(n) time when merging nn elements, how do we get to showing that mergeSort() runs in Θ(nlogn)\Theta(n \log n) time? We start by thinking about the three parts of divide-and-conquer and how to account for their running times. We assume that we're sorting a total of nn elements in the entire array.

The divide step takes constant time, regardless of the subarray size. After all, the divide step just computes the midpoint qq of the indices pp and rr. Recall that in big–Θ\Theta notation, we indicate constant time by Θ(1)\Theta(1).

The conquer step, where we recursively sort two subarrays of approximately n/2n/2 elements each, takes some amount of time, but we'll account for that time when we consider the subproblems.

The combine step merges a total of nn elements, taking Θ(n)\Theta(n) time.

If we ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy