Description of the problem: for a given random sequence containing n elements, find the largest and the second largest sequence of two different elements.
Problem solving analysis (divide and conquer): first given sequence disorder array a [low ... high]. The first case is when there is only one element in the array, there is only one at this time is the maximum value in itself, the second largest value is negative infinity, where I set -999999, the second case is only two elements in the array In this case the maximum value and the second largest obviously you can compare the two elements. The third case is greater than two elements in the array, this time with the Method, the elements in the array is cut in half, as we shall binary sausages, be noted that at this time an intermediate point grouped into the first half, then we judgment of the first half of three cases again, and then do the same for the second half of the operation, because every time we are determined to return the maximum value and the second largest part of the current determination, and therefore has a maximum value and the binary sides the second largest value, then both sides of the four values compare to find the maximum value and the second largest value.
code show as below:
Import Classes in java.util *. ; public class the Main { static int A []; // store the array data of static int INF = -999999; // custom minimum public static void main (String args []) { A = new new int [. 5 ]; A [ 0] =. 4; A [. 1] =. 3; A [2] =. 5; A [. 3] =. 9; A [. 4] =. 1; // test data, may be modified to a keyboard input solve m = max (0,4); // call to solve a method of obtaining the maximum value and the second largest System.out.println (m.max1 + "" + m.max2); // output } static classmax // custom class (the largest and the second largest value is stored) { int MAX1; // maximum int MAX2; // second largest value max () {}; // constructor } static max Solve ( int Low, int High ) // Low and high in the initial array subscripts and subscript termination { max mm = new new max (); // declaration, because every time looking for the maximum value and the second largest value returned will be updated IF (Low = High =) // If only one element { mm.max1 = a [Low]; // maximum value itself mm.max2 = INF; //The second largest value INF } the else IF (High-Low ==. 1) // If only two elements { mm.max1 = Math.max (A [Low], A [High]); // maximum of two maximum element mm.max2 Math.min = (a [Low], a [High]); // minimum is the minimum of the two elements } the else // than two elements { int MID = (Low High +) / 2; // disposed intermediate value mID max = Solve M1 (Low, mID); // M1 and the second largest is the maximum value of the front half portion (including a [mID]) max = M2 Solve (mID + . 1, High); // M2 and the second largest is the maximum value of the second half of IF (m1.max1> m2.max1) //If the second half of the first half maximum is larger than the maximum value { mm.max1 = m1.max1; // updates the maximum front half maximum mm.max2 = Math.max (m1.max2, m2.max1); // time the first half of the second largest maximum value is a large value of the maximum value and the posterior } the else { mm.max1 = m2.max1; // update the second half of the maximum value is a maximum value mm.max2 = Math.max (m2.max2 , m1.max1); // maximum value of the second half of the second largest and the second largest portion of the maximum value of the first half } } return mm; // return } }
Note: Each time you solve method is called, must be initialized mm, because in order to make low and high update, mm in each store is low ~ high maximum value and the second largest value;
Empty it wow ~