So i tried solving this with master theorem but unsure if my solution is correct:
function MixedSort(A[1..n]){
if (n==1) {
return A[1];
}
m = n/2;
B[1 ... m] = bubbleSort(A[1...m]) //normal Bubblesort function O(m^2)
B[m+1 ... n] = MixedSort(A[m+1 ... n])
return Merge(B[1 ... m], B[m+1 .. n]) //merges 2 sorted arrays in O(n)
}
- T(1) = 1
- T(n) = T(n/2)+n^2+n = T(n/2)+n^2
then used mastertheorem and got O(n^2) because 1<2^2=4
It is
O(n^2)
, yes. You don't even need the master theorem to see this. The recurrence relation is the following: (TheO((n/2)^2)
andO(n)
terms are from the bubble sort and the merge.)But
O((n/2)^2)
is equivalent toO(n^2)
andO(n^2)
dwarfsO(n)
such that theO(n)
term can just be omitted yieldingThe recurrence
T(n) = T(n/2) + n^2
can be solved using the recursive tree method. At each level of the recursion, the problem size is halved (n/2), and it takesO(n^2)
time to process each level.The total time complexity is the sum of the work done at each level, which forms the series:
Since all the terms of that series are constants times n^2 we can rearrange as
That series converges to a constant so the running time is O(n^2).