Chat with us, powered by LiveChat What is the worst case space complexity for this algorithm | Writedemy

What is the worst case space complexity for this algorithm

What is the worst case space complexity for this algorithm

Question

A community of N pirates has recently conducted an election to choose their new leader. All pirates

vote, and any pirate may run as a candidate. There is no preferential system, each pirate simply

writes the number of their preered leader on the ballot paper. If a single pirate gets more than

50% of the votes, then that pirate is declared the new leader. If no pirate gets more than 50% of the

votes, then the old leader is retained.

N pirates have voted, and their choices have been collected in an array A. Your task is to determine

whether there will be a new leader and, if there will, who the new leader will be. For example, given

the array

4; 4; 7; 1; 7; 7; 2; 7; 7

pirate 7 has 5 votes out of 9, and becomes the new leader, whereas given the array

4; 4; 7; 1; 4; 7; 2; 4

pirate 4 has 4 votes out of 8 but doesn’t manage to get over 50% so the old leader 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 candidate that could pos-

sibly have more than 50% of the votes. To nd a possible winner in the array, A, create a second

array, 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 nd a possible winner on the

array B, stopping when the array has less than 2 elements. If a possible winner has been found, scan

through the array and count the number of votes received by the possible winner. If the possible

winner has more than N/2 votes, print the possible winner out as the winner. If no possible winner

is found, or the possible winner does not have enough votes, print that the old leader is retained.

(a) What is the worst case space complexity for this algorithm (consider the array(s) B only)?

Explain your reasoning.

(b) Give the O,

and, if possible, time complexities for this algorithm. Explain your reasoning.

(c) Why does this algorithm work? You may assume the array size is even for all function calls.

(d) What problem occurs when the array size is odd? Propose a x for this problem, or describe

an alternative algorithm.

(e) What are the (Big-Oh) time and space1 complexities of your new algorithm? Compare this to

the previous algorithm and explain under what circumstances would you use one algorithm or

the other?

Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.

Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.

Do you need an answer to this or any other questions?

About Writedemy

We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.

How It Works

To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.

Are there Discounts?

All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.

Hire a tutor today CLICK HERE to make your first order