Question
A community of N pirates has recently conducted an election to choose their newleader. All piratesvote, and any pirate may run as a candidate. There
A community of N pirates has recently conducted an election to choose their newleader. All piratesvote, and any pirate may run as a candidate. There is no preferential system,each pirate simplywrites the number of their preffered leader on the ballot paper. If a singlepirate gets more than50% of the votes, then that pirate is declared the new leader. If no pirate getsmore than 50% of thevotes, then the old leader is retained.N pirates have voted, and their choices have been collected in an array A. Yourtask is to determinewhether there will be a new leader and, if there will, who the new leader willbe. For example, giventhe array4, 4, 7, 1, 7, 7, 2, 7, 7pirate 7 has 5 votes out of 9, and becomes the new leader, whereas given thearray4, 4, 7, 1, 4, 7, 2, 4pirate 4 has 4 votes out of 8 but doesn t manage to get over 50% so the oldleader is retained.Your team has proposed the following algorithm to determine which, if any,candidate wins:First, a possible winner must be found. This possible winner the only candidatethat could possiblyhave more than 50% of the votes. To find a possible winner in the array, A,create a secondarray, B. Compare A[0] and A[1]. If they are equal, put one of them in B;otherwise do nothing.Then compare A[2] and A[3]. Again if they are equal, add one of these to B;otherwise do nothing.Continue like this until the entire array A has been used. Recursively find apossible winner on thearray B, stopping when the array has less than 2 elements. If a possible winnerhas been found, scanthrough the array and count the number of votes received by the possible winner.If the possiblewinner has more than N/2 votes, print the possible winner out as the winner. Ifno possible winneris found, or the possible winner does not have enough votes, print that the oldleader is retained.(a) What is the worst case space complexity for this algorithm (consider thearray(s) B only)?Explain your reasoning.(b) Give the O, ? and, if possible, T time complexities for this algorithm.Explain your reasoning.(c) Why does this algorithm work? You may assume the array size is even for allfunction calls.(d) What problem occurs when the array size is odd? Propose a fix for thisproblem, or describean alternative algorithm.(e) What are the (Big-Oh) time and space1complexities of your new algorithm? Compare this tothe previous algorithm and explain under what circumstances would you use onealgorithm orthe other?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started