Several methods are used to find the maximum and minimum values in an integer array.

Source: Internet
Author: User

Several methods are used to find the maximum and minimum values in an integer array.

In an integer array, we need to obtain the maximum and minimum values of array elements:

Method 1: first use Array for sorting, and then from the sorted Array, the most element is the smallest, and the last element is the largest.

 

Public static int FindMaxNumber (params int [] stringValue) {Array. sort (stringValue); return stringValue [stringValue. length-1];} public static int FindMinNumber (params int [] stringValue) {Array. sort (stringValue); return stringValue [0];}Source Code


Method 2: This method declares a variable whose value is the first element value in the array. The second element starts the loop and compares it with the variable.
Evaluate the maximum element. If the Compare value is smaller than the variable, the element value is assigned to the variable.
The minimum element is the opposite of the maximum value. If the compared value is smaller than the variable, the comparative value is assigned to the variable.



Public static int FindMaxNumber (params int [] intArray) {int v = intArray [0]; if (intArray. length> 1) {for (int I = 2; I <intArray. length; I ++) {if (intArray [I]> v) v = intArray [I];} return v;} public static int FindMinNumber (params int [] intArray) {int v = intArray [0]; if (intArray. length> 1) {for (int I = 2; I <intArray. length; I ++) {if (intArray [I] <v) v = intArray [I] ;}} return v ;}Source Code

 

Method 3:

 

Public static int FindMaxNumber (params int [] intArray) {return intArray. Max ();} public static int FindMinNumber (params int [] intArray) {return intArray. Min ();}Source Code


Three methods are demonstrated:

 

Static void Main (string [] args) {int [] value = {15, 23, 41, 97,100, 27, 67, 51}; Console. writeLine ("Class3:"); Console. writeLine ("Max: {0}", Class3.FindMaxNumber (value); Console. writeLine ("Min: {0}", Class3.FindMinNumber (value); Console. writeLine ("Class4:"); Console. writeLine ("Max: {0}", Class4.FindMaxNumber (value); Console. writeLine ("Min: {0}", Class4.FindMinNumber (value); Console. writeLine ("Class5:"); Console. writeLine ("Max: {0}", Class5.FindMaxNumber (value); Console. writeLine ("Min: {0}", Class5.FindMinNumber (value ));}Source Code


Execution result:


 

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.