Algorithm-find out the number of times the array appears more than half

Source: Internet
Author: User

Author: Chen taihan

Algorithm-- Find out the number of times the array appears more than half
Whenever I see a classic algorithm question, I miss my high school. I feel that many algorithm questions are the question of high school. Who is my brother? I have read-only specialists, and I have almost never learned a high number of questions.
If you have time, you need to look at high numbers. I think that's a good math ......

# Include <iostream>
Using namespace STD;
Class findtheone
{
Public:
Method 1
The first way to think of it is to see a two-dimensional array, where the data in the one-dimensional array and the number of times the two-dimensional storage occurs. The number that appears the most is the number that you want to find.
Because a number appears more than half the length of the array, the length of the two-dimensional array is only half of the length of the array.CodeThe implementation is as follows,
Of course, this method is very bad. The time complexity and space complexity are both relatively large. I wrote about it if I want to practice it.

Method 1

Void Search ( Int A [], Int Len, Int & Theone)
{
If (Null = A | Len <= 0 )
{
Return ;
}

Int ( * B )[ 2 ] = New Int [Len / 2 ] [ 2 ];
B [ 0 ] [ 0 ] = A [ 0 ];
B [ 0 ] [ 1 ] = 1 ;

Int T = 0 ;
Bool Notexist = True ;
For ( Int I = 1 ; I < Len; ++ I)
{
For ( Int J = 0 ; J < T; ++ J)
{
If (A [I] = B [J] [ 0 ])
{
B [J] [ 1 ] ++ ;
Notexist = False ;
Break ;
}
}
If (Notexist)
{
B [t ++ ] [ 0 ] = A [I];
}
}

Int Max = 1 ;
Int K = 0 ;
For ( Int I = 0 ; I < Len / 2 ; ++ I)
{
If (B [I] [ 1 ] > Max)
{
Max = B [I] [ 1 ];
K = I;
}
}

Theone=B [k] [0];
}

Method 2
Sort the array. The number in the middle is the number you are looking.
If the maximum number is the smallest, then 1 to (n + 1)/2 are all
If the maximum number is the largest, then (n-1)/2 to N are all
If it is neither the smallest nor the largest, when the number gradually changes from the smallest to the largest, you will find that the value of the number in the middle remains unchanged.
To sum up, the number in the middle is the number you are looking.
Time complexity is the time used for sorting. I really don't want to write about sorting (refer to my other blog). We all know that sorting is quite time-consuming. Of course this method is still not very good.

Method 3
This method borrowed others' ideas.
Here I will perform a simple analysis.
The time complexity of this algorithm is O (n), and two auxiliary variables are used.
K is used to temporarily store the data in the array, and J is used to store the number of occurrences of a certain number.
At the beginning, K stores the first number in the array, and J is 0. If the number in the array is equal to K, J adds 1; otherwise, 1 is subtracted. If J is 0, then, the number in the current array is assigned to K.
Because the number of occurrences of the specified number is more than half the length of the array, After all J ++ and j -- are offset, the final J value is greater than or equal to 1, the number stored in K is the one with the largest number.

The following algorithm is only applicable to an array with more than half the length of an array.

Method 3

Int Search ( Int A [], Int Len)
{
If (Null = A | Len <= 0 )
{
Return - 1 ;
}

Int K, J = 0 ;
For ( Int I = 0 ; I < Len; ++ I)
{
If (J = 0 )
{
K = A [I];
}
If (K = A [I])
{
++ J; // Some people say that ++ J has inherent advantages over J ++. I wonder if you have heard of it. If you have heard of it, have you ever thought of more powerful tive C ++ (C ++ProgramMembers must read books)
} Else
{
-- J;
}
}

ReturnK;
}

};

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.