The maximum and minimum values of the sequence number (Java implementation) under the premise of least comparison

Source: Internet
Author: User

1 /**2 * In a set of numbers, get the maximum and minimum values in the smallest possible comparison here3  * @authorJimmypc4  *5  */6  Public classProblem_minmax {7     8      Public classNode {9          Public intmin;Ten          Public intMax; One     } A     //Get maximum minimum value -      PublicNode Getminmax (intA[],intN) { -Node Minmax =NewNode (); the         inti; -         if(n%2 ==0){ -             if(A[0] >a[1]){ -Minmax.max = a[0]; +Minmax.min = a[1]; -}Else{ +Minmax.max = a[1]; AMinmax.min = a[0]; at             } -i = 2; -}Else{ -Minmax.max = a[0]; -Minmax.min = a[0]; -i = 1; in         } -          to          while(I < n-1){ +             if(A[i] > a[i+1]){ -                 if(A[i] >Minmax.max) { theMinmax.max =A[i]; *                 } $                 if(A[i+1] <minmax.min) {Panax NotoginsengMinmax.min = a[i+1]; -                 } the}Else{ +                 if(A[i+1] >Minmax.max) { AMinmax.max = a[i+1]; the                 } +                 if(A[i] <minmax.min) { -Minmax.min =A[i]; $                 } $             } -i+= 2; -         } the         returnMinmax; -     }Wuyi  the     /** -      * @paramargs Wu      */ -      Public Static voidMain (string[] args) { About         intArr[] = {1000, 11, 445, 1, 330, 3000}; $         intArr_size = 6; -Node Minmax =NewProblem_minmax (). Getminmax ( - arr, arr_size); -System.out.println ("Minimum element is" +minmax.min); ASystem.out.println ("Maximum element is" +Minmax.max); +          the     } -}

Reference: http://blog.csdn.net/jiyanfeng1/article/details/8541886,http://www.geeksforgeeks.org/maximum-and-minimum-in-an-array/

The maximum and minimum values of the sequence number (Java implementation) under the premise of least comparison

Related Article

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.