Boyer moore voting algorithim
public class boyer_moore {
public static void main(String[] args) {
//input array
int nums[]={6,5,5};
int majority=nums[0];
int lead=1;
for(int i=1;i<nums.length;i++){
if(nums[i]==majority){
lead++;
}
else if(lead>0){
lead--;
}
else{
majority=nums[i];
lead=1;
}
}
System.out.println("majority is "+majority);
}
}
Comments
Post a Comment