Question: Give an iterative version of Algorithm majority MAJORITY Input: An array >A[l..n] of n elements. Output: The majority element if it exists; otherwise none. C

Give an iterative version of Algorithm majority

Give an iterative version of Algorithm majority MAJORITY Input: An array >A[l..n]

MAJORITY Input: An array >A[l..n] of n elements. Output: The majority element if it exists; otherwise none. C left arrow candidate(l) count left arrow 0 for j left arrow 1 to n if.A[j] = c then count left arrow- count + 1 end for if count > [n/2] then return c else return none Procedure candidate(m) j left arrow m; c left arrow A[m]; count left arrow 1 while j 0 j left arrow j + 1 if.A[j] = c then counts left arrow count + 1 else count left arrow count - 1 end while if j = n then return c {See Exercises 5.31 and 5.32.} else return candidate(j + 1) MAJORITY Input: An array >A[l..n] of n elements. Output: The majority element if it exists; otherwise none. C left arrow candidate(l) count left arrow 0 for j left arrow 1 to n if.A[j] = c then count left arrow- count + 1 end for if count > [n/2] then return c else return none Procedure candidate(m) j left arrow m; c left arrow A[m]; count left arrow 1 while j 0 j left arrow j + 1 if.A[j] = c then counts left arrow count + 1 else count left arrow count - 1 end while if j = n then return c {See Exercises 5.31 and 5.32.} else return candidate(j + 1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!