We assume that we're sorting a total of
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
of the indices
and
. Recall that in big-Θ notation, we indicate constant time by
.
- The conquer step, where we recursively sort two subarrays of approximately
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
elements, taking
time.
If we think about the divide and combine steps together, the
running time for the divide step is a low-order term when compared with the
running time of the combine step. So let's think of the divide and combine steps together as taking
time. To make things more concrete, let's say that the divide and combine steps together take
time for some constant
.Let's draw out the merging times in a "tree":
Computer scientists draw trees upside-down from how actual trees grow. Atree is a graph with no cycles (paths that start and end at the same place). Convention is to call the vertices in a tree its nodes. The root node is on top; here, the root is labeled with the
subarray size for the original
-element array. Below the root are two child nodes, each labeled
to represent the subarray sizes for the two subproblems of size
.What do you think would happen for the subproblems of size
? There will be eight of them, and the merging time for each will be
, for a total merging time of
:As the subproblems get smaller, the number of subproblems doubles at each "level" of the recursion, but the merging time halves. The doubling and halving cancel each other out, and so the total merging time is
at each level of recursion. Eventually, we get down to subproblems of size 1: the base case. We have to spend
time to sort subarrays of size 1, because we have to test whether
, and this test takes time. How many subarrays of size 1 are there? Since we started with
elements, there must be
of them. Since each base case takes
time, let's say that altogether, the base cases take
time:When we use big-Θ notation to describe this running time, we can discard the low-order term (
) and the constant coefficient (
), giving us a running time of
, as promised.Read more from Source: https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/analysis-of-merge-sort
0 comments:
Post a Comment
Let us know your responses and feedback