Returns the maximum and minimum values of an array.

Source: Internet
Author: User

Returns the maximum and minimum values of an array.

Calculate the maximum and minimum values of the array. You can traverse the array and record the maximum and minimum values respectively. This method requires 2N comparisons. If you want to reduce the number of comparisons, You can traverse the array, compare Adjacent Elements, and put the greater margin of the adjacent elements behind the smaller ones in front. Select the maximum value from the greater limit to the maximum value of the entire array, and select the minimum value from the smaller value to the minimum value of the entire array. In this case, the number of comparisons is 1.5 * N.

void findmaxmin(int a[],int n){if(a==NULL||n<0)return ;int i=0;int maxe=0x80000000;int mine=0x7fffffff;for(i=0;i<n;i+=2){if(i+1<n&&a[i]>a[i+1])std::swap(a[i],a[i+1]);}for(i=0;i<n;i+=2){if(i+1==n){if(a[i]>maxe)maxe=a[i];if(a[i]<mine)mine=a[i];}if(a[i]<mine)mine=a[i];if(a[i+1]>maxe)maxe=a[i+1];}cout<<maxe<<" "<<mine<<endl;}

You can also use the divide and conquer method. First, find the maximum and minimum values in the left half array, then find the maximum and minimum values in the right half array, and then compare the two maximum values to find the maximum value of the entire array, compare the two minimum values to obtain the minimum values of the entire array. Set the number of comparisons to f (n), with f (2) = 1; f (n) = 2 * f (n/2) + 2;

We can find that the number of comparisons is 1.5 * N-2, the number of comparisons is not reduced.

struct res{int min;int max;};res findmaxmin2(int a[],int start,int end){   res r;if(start>=end-1){if(a[start]<a[end]){r.min=a[start];r.max=a[end];}else{r.min=a[end];r.max=a[start];}return r;}int mid=(end-start)/2+start;res left=findmaxmin2(a,start,mid);res right=findmaxmin2(a,mid+1,end);r.max=left.max>right.max?left.max:right.max;r.min=left.min<right.min?left.min:right.min;return r;}


C language, maximum and minimum values of Arrays

This program has many problems.
The initial value of min is 0. Unless you enter a negative number, min is always zero.
The minimum value comparison after the else statement is also problematic.

So it will be OK.
Main ()
{
Int a [SIZE] = {0 };
Int I = 0, max = 0, min = 0;

For (I = 0; I <SIZE; I ++)
{
Scanf ("% d", & a [I]);
}
For (max = a [0], min = a [0], I = 0; I <SIZE; I ++)
{
If (max <a [I])
Max = a [I];
If (min> a [I])
Min = a [I];
}
Printf ("max = % d \ n \ nmin = % d", max, min );

}

Maximum and minimum values in two-dimensional array

# Include <conio. h>
# Include <stdio. h>
Void main ()
{Int a [3] [3] = {, 37, 5}, I, j, max, min;
Max = min = a [0] [0];
For (I = 0; I <3; I ++)
/************ Found ************/
For (j = 0; j <3; j ++) // here j starts from 0
{If (max <a [I] [j])
Max = a [I] [j];
/************ Found ************/
If (min> a [I] [j]) // here min <a [I] [j] should be min> a [I] [j]
Min = a [I] [j];
}
Printf ("The max is: % d \ n", max );
Printf ("The min is: % d \ n", min );
}

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.