64. ii
jj
KK
Merging lists takes as long as the list (order(N))
You do this log(N) times
Making algorithm Order (N log(N))
65. ii
jj
KK
Merging lists takes as long as the list (order(N))
You do this log(N) times
Making algorithm Order (N log(N))
Merging lists takes as long as the list (order(N))
You do this log(N) times
Making algorithm Order (N log(N))
Pity you need extra space to ‘merge’ into otherwise
it would be pretty cool
Editor's Notes
Lets begin with a list to sort
We split the array in two and look at the first half.
We split that in two and look at the first half.
We split that in two and look at the first half.
We split that in two and look at the first half. We reach a single item so it must be sorted.
We split that in two and look at the first half. We reach a single item so it must be sorted.
We look at the other half. Its also a single array so it’s also sorted.
We now have two sorted arrays which we need to merge . Using two pointers we move down them picking the smallest one each time. The final purple k pointer shows where the item will go.
When both pointer reach the end of their respective lists we have all the items merged.
So this sub list is sorted. We can move on to the other half.
We sub divide again to get another single item. Its sorted.
the other half of this pair is signle so its sorted.
We now have to merge this pair.
With these two eights done we can merge the first quarter.
With the first quarter done we can move to the second quarter.
With the first half done. I am going to speed up the process for the second half.
We split
And split
And split until we have single items.
then we merge halves together.
and merge again.
and again, until we are ready to merge with first half.