Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A majority element in an array, A, of size N is an element that appears more than N/2 times(thus, there is at most one). If

A majority element in an array, A, of size N is an element that appears more than N/2 times(thus, there is at most one). If there is no majority element, your program should indicate this. Design three different algorithms and write them in Java.

And then run these algorithms with four given sample sets(Majex1, 2, 3, 4) of input files and measure execution time for each case.

a) Method 1 (O(N2) algorithm: Write a program to have two loops and keep track of maximum count for all different elements. If maximum count becomes greater than N/2 then break the loops and return the element having maximum count. If maximum count doesnt become more than N/2 then majority element doesnt exist

b) Method 2 (O(NlogN) algorithm using a divided-and-conquer method. The algorithm begins by splitting the array in half repeatedly and calling itself on each half. When we get down to single elements, that single element is returned as the majority of its (1-element) array. At every other level, it will get return values from its two recursive calls.

The pseudo-code of the algorithm is as follows:

procedure GetMajorityElement(a[1...n])

Input: Array a of objects

Output: Majority element of a

if n = 1: return the element

else

k = n/2

elemlsub = GetMajorityElement(a[1...k])

elemrsub = GetMajorityElement(a[k+1...n]

if elemlsub = = elemrsub:

return elemlsub

lcount = GetFrequency(a[1...n],elemlsub)

rcount = GetFrequency(a[1...n],elemrsub)

if lcount > k+1:

return elemlsub

else if rcount > k+1:

return elemrsub

else return NO-MAJORITY-ELEMENT

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions

Question

d. Would you implement the new control algorithm? Pg45

Answered: 1 week ago

Question

Identify the cause of a performance problem. page 363

Answered: 1 week ago