Question: *************JAVA************* Assigement 1.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
*************JAVA*************
Assigement
1.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 JAVA program should indicate it. And then run these algorithms with four given sample sets(Majex1, 2, 3, 4) of input files and measure execution time for each case. Method 1 (O(N)) algorithm. This algorithm is a two step process.
1.Get an element occurring most of the time in the array. This phase will make sure that if there is a majority element then it will return that only.
2. Check if the element obtained from above step is majority element.
The following is pseudo code of step 1.
// Initialize index and count of majority element
Maj_index = 0, count = 1
Loop for i = 1 to N 1
{
if a[Maj_index] = = a[i]
count++;
else
count - -;
if count = = 0
{
Maj_index = i
count = 1;
}
}
return a[Maj_index]
it should be Java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
