Algorithm exercises---Array main elements of linear table search

Source: Internet
Author: User

One: Topic
The main element is that a number appears in the array more than half the length of the array, so this number is the main element of the array element.
For example: {0,5,5,3,4,5,5,5,5,9} This element 5 has 6 more than half, so that is the main element
Use an efficient method to find the main element of array a, and if so, output the element, otherwise return1
Second: Ideas
Here we use the method of the counter to mark the data element, the first element is set to the alternate main element, the count is 1, if the next element or the same number, Count +1, if different, count-1, when the count is 0, then replace an element to do the alternate main element, The count becomes 1 with this loop.
If there is a real master element in the array, then the count of that element must be a number greater than 0.

First step: Choose 2 as the alternate main element with a count of 1

The second step: move backwards, find that the data is different, Count minus one, is 0, the change alternates the main element is 3, the count becomes 1

Step three: Move backwards, find the same data, Count plus one, 2

Fourth step: Move backwards, find the same data, Count plus one, 3

Fifth step: Move backwards, find that the data and the alternate main elements are different, count minus one, 2

Sixth step: Move backwards, find data and alternate main elements the same, Count plus one, 3

Seventh step: Move backwards, find that the data and the alternate main elements are different, count minus one, 2

Eighth step: Move backwards, find data and alternate main elements the same, Count plus one, 3

Nineth step: Move backwards, find data and alternate main elements the same, Count plus one, 4

Tenth step: Move backwards, find that the data and the alternate main elements are different, count minus one, 3

Traverse the end! At this point the alternate main element is 3, the count is positive, but we can not determine whether it must be the main element, so we should loop over the element to compare the count, if the count is more than half, then this is the main element! Three: Algorithm implementation
#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>intMajorityintA[],intN) {    intI=1, count=1;////For counting    intMaj = a[0];  for(i =0; I < n;i++)    {        if(A[i] = =Maj) Count++; Else        {            if(Count >0) Count--; Else{maj=A[i]; Count=1; }        }    }    if(count>0)//find a value that is most likely the primary element    {        //Cyclic count VerificationCount =0;  for(i =0; I < n; i++)            if(A[i] = =Maj) Count++; }    if(Count > N/2)        returnMaj; return-1;}intMain () {inta[Ten] = {2,3,3,3,9,3,7,3,3,4 }; intret; RET= Majority (A,Ten); printf ("%d", ret); System ("Pause"); return 0;}

Algorithm exercises---Array main elements of linear table search

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.