To solve the issue in the shortest amount of time
- Divide the array into two parts of size .
- Name the first array and the second array.
- Take this same majority component from. and call it , then take the majority component from .
- Determine if the number for or is more than the array role="math" localid="1659779390830" size.
A majority of both the element is found using the following algorithm:
function
Input: an array of numbers
Output: majority value of array
return
if:
return
if:
return
else if:
return
else
return
Algorithm to find the frequency of the algorithm:
function
Input: an array of numbers
Output: count of elementof the array
for:
if
return
Explanation:
• "majority" is indeed a divide-and-conquer method which it accepts this same array "" as an input argument and employs the divide-and-conquer approach.
• It returnsafter checking if the index of the final member of the array is equal to 1.
• Divide the size of the index by two to get the middle member in the array, and use the ceilings procedure to have the centre element & allocate this to the variables ""
• Have the overwhelming element of both the left and right arrays by executing the same majority method with half of the array as a parameter.
• Next, make sure that both components are equal.
If yes, return the majority element of the left array.
• Using the function frequency, determine the frequency of the majority element in the left and right arrays.
• Return the element if the count of the element in the left array is larger than half of the items in the array "".
• Otherwise, return the element if the count of the right array element is larger than half of the items in the array "".
• Alternatively, no-majority-elements will be returned.
As a result, in each function call, it splits an array split two as well as searches each array through constant division of a array until the element is located with a consistent amount of time..
It takestime to search each array and a linear time ofto count the majority element.
The recurrence relation is shown below:
…… (1)
Consider the master theorem for the following recurrence equation,
…… (2)
By comparing equation (1) and (2), the value of “” is “”, “” is, “” is “” and “” is.
By master’s theorem, if the value of, then the running time is,
…… (3)
Check if :
…… (4)
Substitute the value of “ ” as “2 ” and “” as “” in equation (4).
Thus, the value ofis equal to “c”.
Substitute the value of “c” as 1 in equation (3). The running time of algorithm is shown below:
Therefore, the algorithm takes a run time of .