Returns the maximum value of an array using the sub-governance method and the Array Using the sub-governance method.

Source: Internet
Author: User

Returns the maximum value of an array using the sub-governance method and the Array Using the sub-governance method.

Now we provide a book group with n elements. The number of elements is n, and the maximum and minimum values need to be obtained.

Method 1.

Use max and min to record the maximum and minimum values of the array, scan the array sequentially, and constantly replace and update max, min, (the initial values of max and min are all the first elements in the array)

Method 2.

1. If there is only one element in the array, It is the maximum and minimum values.

2. otherwise, you can find the maximum and minimum values on the left and on the right of the array. then the maximum value of this interval is max (lmax, rmax), and the minimum value is min (lmin, rmin)

The details are as follows (n numbers are randomly generated ).

#include <stdio.h>#include <time.h>int getmax(int a,int b){        return a>b?a:b;}int getmin(int a,int b){        return a<b?a:b;}void print(int a[],int n){        int i;        for(i=0;i<20;i++)                printf("%d ",a[i]);        printf("\n");}void maxmin(int a[],int l,int r,int * _max,int * _min){        if(r>l){               int m=(l+r)/2;               int _lmin,_rmin,_lmax,_rmax;                maxmin(a,l,m,&_lmax,&_lmin);                maxmin(a,m+1,r,&_rmax,&_rmin);               *_max=getmax(_lmax,_rmax);               *_min=getmin(_lmin,_rmin);        }else{        *_max=*_min=a[l];        }}int creat(int a[],int n,int m){        int i;        srand(time(NULL));        for(i=0;i<n;i++)                a[i]=rand()%m-m/2;        return n;     }int main(){        int n,i;        int a[100];        int _min,_max;        creat(a,20,100);        print(a,20);        maxmin(a,0,19,&_max,&_min);        printf("max:%d min:%d\n",_max,_min);        return 0;}
The following are the running results.



How to use c ++ to divide and conquer the maximum and minimum values of an array

Www.cppblog.com/..5.html
Algorithm idea: First two adjacent comparisons, larger values are placed into the array max [], smaller values are placed into the array min [], and then the maximum value is obtained from the max [] array, min [] array to obtain the minimum value. It can be proved that this is the most efficient algorithm and cannot be further improved.

# Include <iostream>
# Define n 11
# Define m (n + 1)/2)
Using namespace std;

Void main (void)
{
Int num [] = {11, 2, 3, 4, 6, 5, 7, 8, 9, 10, 20 };
// Int n = sizeof (num)/sizeof (num [0]);
// Int m = (n + 1)/2;
Int max [m], min [m];
Int k = 0, j = 0;

If (n/2! = 0) max [M-1] = min [M-1] = num [n-1];

For (int I = 0; I <n-1; I = I + 2)
{
If (num [I]> = num [I + 1])
{
Max [j ++] = num [I];
Min [k ++] = num [I + 1];
}
Else
{
Max [j ++] = num [I + 1];
Min [k ++] = num [I];
}

}

For (I = 0; I <m; I ++)
{
Cout <"max [" <I <"] =" <max [I] <"\ t ";
Cout <"min [" <I <"] =" <min [I] <endl;
}

Int MAX = max [0];
Int MIN = min [0];

For (j = 1; j <m; j ++)
{
If (max [j]> MAX) MAX = max [j];
If (min [j] <MIN) MIN = min [j];
}

Cout <"MAX =" <MAX <", MIN =" <MIN <endl;
}

C/C ++ division and Control Method: two maximum values and two minimum values in an array: Yes.

Int max = 0;
Int min = 0;
Void max_num (int * number, int first, int last)
{
If (first + 1 <last)
{
Int I = (first + last)/2;
Max_num (number, first, I );
Max_num (number, I + 1, last );
}
Else if (first + 1 = last)
{
If (number [first]> number [last])
{
Max = max> number [first]? Max: number [first];
Min = min <number [last]? Min: number [last];
}
Else if (number [first] <number [last])
{
Max = max> number [last]? Max: number [last];
Min = min <number [first]? Min: number [first];
}
}
Else if (first = last)
{
Max = max> number [first]? Max: number [first];
Min = min <number [last]? Min: number [last];
}
};

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.