Evaluate the maximum value of the array, secondary limit

Source: Internet
Author: User

At the end of the Microcomputer principles examination, the teacher gave us a question: calculate the maximum number of segments, the next answer. At that time, the question was required to be written in assembly language, here, I use C ++ to present relevant algorithms.

It is very easy to calculate the maximum value of the array. Basically, everyone who will program will ask for the maximum value, but the second seek will be a little bent ......

My thought was like this:

Because these numbers are all positive integers, we can achieve this through two cyclic comparisons. The first traversal is used to obtain the maximum value, then the number at the position corresponding to the maximum value is set to zero, and the second traversal is used to obtain the secondary value.

Two traversal methods:
# Include
 
  
Using namespace std; int fun_max (int * a, int n) {int max = a [0], k = 0; for (int I = 1; I
  
   
The result is correct, but the original data in the array is undoubtedly damaged (although the problem does not say that the data in the array cannot be modified ), the method is not very good after two traversal.
   

After the examination, I learned a better way to get the result through a traversal without modifying the data in the array:

Compare each number in the array with the maximum value:

If a [I]> m1, it indicates that the current "Maximum m1" is not the maximum value, but a [I]. Therefore, the current maximum value is assigned to the secondary worker, then assign a [I] to m1;

If a [I] <= m1, and then judge whether ai]> m2 is true, if a [I] is smaller than the maximum value and greater than the number of "Times, that a [I] is the new secondary node.

One Traversal method:
# Include
    
     
Using namespace std; int main () {int a [5] = {1, 40, 6, 7, 10}; int m1 = a [0], m2 = a [0]; for (int I = 1; I <5; I ++) {if (m1
     

This method is obviously much better ......


Of course, sorting is also a method. One idea is to write the Sorting Algorithm by yourself. However, we do not need to sort these five numbers. We only need to find the first two to obtain the sorting result; another method is to use the Sorting Algorithm in stl. This method is easy to use, but it is useless.

Sorting method: select a sorting method:
# Include
      
       
Using namespace std; int main () {int a [5] = {1, 40, 6, 7, 10}; int k, temp; for (int I = 0; I <2; I ++) // a [0] And a [1] are the largest and the second largest {k = I; for (int j = I; j <5; j ++) {if (a [j]> a [k]) k = j;} temp = a [k]; a [k] = a [I]; a [I] = temp;} cout <"m1:" <
       
Use stl's quick sorting algorithm:

# Include
        
         
# Include
         
          
Using namespace std; int com (const void * a, const void * B) {return * (int *) a <* (int *) B;} int main () {int a [5] = {1, 40, 6, 7, 10}; qsort (a, 5, sizeof (a [0]), com); cout <"m1:" <
          

Output result:

M1: 40

M2: 10





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.