Ryan is implementing a merge sort algorithm that he heard about in computers class. However, he wasn't paying attention, and ended up implementing the merge sort in a very unusual way.
The standard merge sort takes a list, and recursively splits it in half, until there is only one element left. It then uses the idea that two sorted lists can be easily merged in
time using "two pointer technique" (this step is usually called
merge
).
Ryan doesn't know about two pointer technique, however, so he decided to replace
merge
with a bubble sort! The bubble sort runs in
time.
What is the runtime of this merge sort implementation?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
We analyze this recurrence using the master method.
Note that the recurrence T ( n ) can be written as follows: T ( n ) = 2 T ( 2 n ) + O ( n 2 ) This follows from the fact that we split the list in two, and recurse on each half. The O ( n 2 ) accounts for the worse runtime of the bubble sort than the merge function.
Now we note that a = 2 , b = 2 , c = 2 , by the master method. Since c > lo g b a , this falls under case 3. Therefore, T ( n ) ∈ O ( n c ) , T ( n ) ∈ O ( n 2 ) .