Find the Moore voting algorithm

Source: Internet
Author: User

For an array of one array[1,5,1,3,2,3,3,3,4,9,6,4,3,3,3,3], find out how many more than half the length of the array. Today I learned one of the Moore voting algorithms. One of the other algorithms is to sort the array first, from small to large, and more than half, if the number exists, it must be in the middle of the arrays. This is the final check of the number, because it is possible that it is exactly equal to half the number.

The idea of the Moore poll algorithm is to first define a majority and a count,majority to hold the most current number of occurrences. If majority equals the first number, count=1; then majority is compared with the second number, if equal, then count+1, otherwise count-1, if it is reduced to count=0, then that number in the current sequence is not the most occurrences of the occurrence, Therefore, the current number is replaced. And then go on to compare. Finally, the number to be tested.

  

1        Public Static intGetValue (int[] Gifts,intN) {3           intHalf=n/2;5           intMajority=0,count=0;6            for(inti:gifts) {7               if(count==0){8majority=i;9count++;Ten               } One               Else{ A                   if(majority!=i) { //When the majority is not equal to the current number I,  -count--; then the Count count-1, if Count minus one is 0, then replace.  -                   } the                   Else{ -count++; -                   } -               } +               if(count>half) { -                   returnmajority; +               } A           } atCount=0; -            for(inti:gifts) { -               if(majority==i) { -count++; -               } in           } -           if(count>half) { to               returnmajority; +           } -           Else return0; the}

From the found array of more than 1/2 of the number transformation to find 1/3 of the number, you can know that the array of more than 1/2 of the number of one, 1/3 of the maximum two.

  

1        Public StaticString Gettwomax (int[] A,intN) {2           intM1=0,m2=0;3           intC1=0,c2=0;4String maxtwotimesnumber= "";5            for(inti:a) {6               if(m1==i) {7c1++;8               }9               Else if(m2==i) {Tenc2++; One               } A               Else if(c1==0){ -m1=i; -c1++; the               } -               Else if(c2==0){ -M2=i; -c2++; +               } -               Else{ +c1--; Ac2--; at               } -           } -C1=0; -C2=0; -            for(inti:a) { -               if(i==M1) { inc1++; -               } to               if(i==m2) { +c2++; -               } the           } *           if(C1>N/3){ $maxtwotimesnumber+=M1;Panax Notoginseng           } -           if(C2>N/3){ themaxtwotimesnumber+=m2; +           } A           returnMaxtwotimesnumber; the            +}

Also for numbers larger than N/3, we need two numbers to record. For example, the array [A,b,a,b,c], the first of their respective m1,m2, the count is 0, when a condition to compare, m1=a,c1=1,m2=b,c2=2, and then I equals A,I==M1, so C1 plus one, then I=B,C2 plus one, then i=c, conditions are not met, then C1, C2 are each reduced by one. If the subsequent count is 0 o'clock, the current number is replaced with M1 or M2.

Find the Moore voting algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.