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