Note: those marked with an asterisk I am not 100% confident in my answer/reasoning.
Please feel free to discuss the problems and my proposed solutions below!
1.1
Assuming 0 based counting, 4 will be in position 7.
1.2
I would think it should be , but that’s not an answer choice. Additionally that would be an improvement of the 2-way merge sort, and I’m not so sure that 3-way mergesort should be better. I would think it should perform worse as there are now more comparisons being made at the merge steps. Indeed, if you were to keep making more and more divisions, say make n groups as opposed to 2 or 3 groups, then you’ve obtained a virtual selection sort algorithm. And selection sort runs at .
My guess is going to be b, .
1.3
Taking two sorted arrays of length n, there should be at most n comparisons. If you take that 2n item array and merge with the third n array, then in the worst case 2n comparisons will be made (first half of the 2n array are lesser than the incoming elements), giving a total of 3n comparisons. One would be tempted to stop there and say it should be nk, as 3 was obtained after merging 3 n arrays. But the number of comparisons grows by the triangular numbers: 1,3,6,10,... which I believe indicates some sort of squaring on k is occurring.
So my guess is e, .
1.4
Now that we’re applying more “divide and conquer”, we should have a log factor now. I think it should be logk as we’re dividing the number of arrays each time.
My guess is c, .
1.5
This problem is pretty similar to the classic problem of:
If we arrange a single elimination tournament with n contestants, how many matches will need to be organized?
The intuition I’ve used for that classic problem is: in order for there to be 1 winner, there must be (*)n - 1 losers. In order to have a loser, you have to play a match. Since there’s n - 1 losers, then that’s how many matches are needed to determine a winner.
The same principle can be performed on this unsorted array, similar to merge sort. But for the second largest value (SLV) we’ll need to do some extra steps. Particularly, we’ll need to keep a record of each defeated opponent for each value in our list. Once we determine what the largest value (LV) is, the SLV will be one of the defeated opponents that the LV faced against in the tournament. The number of defeated opponents amounts to the number of rounds in the tournament. Since the number of values is a power of 2, there will be log_2(n) rounds in the tournament. Since that’s the number of rounds, then that’s the number of opponents the LV will face. So to determine the SLV, have a mini tournament of these log_2(n) values. Using the same principal from (*), there must be log_2(n) - 1 matches to determine the SLV. So then the total number of comparisons amounts to: .∎
Programming Challenge
1.6
This one took me longer than I think it should have. It’s super important to plan things out before hand, which I did not do. I will try to do more preliminary planning in future projects.
There’s probably some simplifying I could do, such as eliminate some steps in which I trim leading zeroes. Either way, I’ve designed a successful project which computes the correct answer in only a few milliseconds.