This problem can be easily solved by traversing the array cyclically and finding the maximum and minimum values in the array. The main problem discussed in the book is the method with a small number of comparisons, however, the book has proved that no matter what method is used to compare the minimum number of times, that is, the comparison of loop traversal, the result is O (1.5N). Therefore, this problem can be easily solved.
Method 1:
Function declaration:
void DutFindMaxAndMinInArray_1(int*, int, int&, int&);
The source code is as follows:
/* Basic Solution for finding the maximum and minimum values */bool _ dutfindmaxandmininarray = false; void dutfindmaxandmininarray_1 (int * a, int size, Int & _ max, Int & _ min) {If (! A | size <= 0) {_ dutfindmaxandmininarray = true; _ max =-1; _ min =-1; return;} If (size = 1) {_ max = A [0]; _ min = A [0]; return;} if (a [0]> = A [1]) {_ max = A [0]; _ min = A [1];} else {_ max = A [1]; _ min = A [0];} /* after repeating the loop, perform a comparison at a time. */For (INT I = 2; I <size; ++ I) {if (a [I]> _ max) {_ max = A [I];} else if (a [I] <_ min) {_ min = A [I] ;}}
Then, we can use the sub-governance solution to solve this problem, which means to find the maximum and minimum values in the first half of the array, and find the maximum and minimum values in the last half of the array, then, divide the original problem into smaller problems.
Function declaration:
void DutFindMaxAndMinInArray_2(int*, int, int&, int&);void DutFindMaxAndMinInArray_2(int*, int, int, int&, int&);
The source code is as follows:
/* the maximum and minimum values of the sub-rule method */void dutfindmaxandmininarray_2 (int * a, int size, Int & MAX, Int & min) {If (! A | size <= 0) {_ dutfindmaxandmininarray = true; max =-1; min =-1; return;} dutfindmaxandmininarray_2 (A, 0, size-1, Max, min);} void dutfindmaxandmininarray_2 (int * a, int low, int high, Int & MAX, Int & min) {If (high-low <= 1) {if (a [High]> A [low]) {max = A [High]; min = A [low]; return;} else {max = A [low]; min = A [High]; return ;}int maxl, minl; int maxr, minr;/* Find the maximum and minimum values on the left and right sides, respectively, then compare the values on the two sides to get the most */dutfindmaxandmininarray_2 (A, low, low + (high-low)/2, maxl, minl); dutfindmaxandmininarray_2 (, low + (high-low)/2 + 1, high, maxr, minr); If (maxl> maxr) {max = maxl;} else {max = maxr ;} if (minl> minr) {min = minr;} else {min = minl ;}
Next, let's look at the expansion problem in the next book, that is, how to find the second largest value in the array.
In fact, this problem can also be used in the previous division idea, that is, to find the first half of the array to meet the required value, and then find the second half of the array to meet the required value.
Function declaration:
/* 2.10 extended search for the second largest value */void dutfindmaxandsecondmax (int *, Int, Int &, Int &); void dutfindmaxandsecondmax (int *, Int &, int &);
The source code is as follows:
/* divide the rule to find the maximum and second largest values */bool _ dutfindmaxandsecondmax = false; void dutfindmaxandsecondmax (int * a, int size, Int & MAX, Int & max2) {If (! A | size <= 0) {_ dutfindmaxandsecondmax = true; max =-1; max2 =-1; return;} dutfindmaxandsecondmax (A, 0, size-1, Max, max2);} void dutfindmaxandsecondmax (int * a, int low, int high, Int & MAX, Int & max2) {If (high-low <= 1) {if (a [High]> A [low]) {max = A [High]; max2 = A [low]; return;} else {max = A [low]; max2 = A [High]; return ;}int maxl, maxl2; int maxr, maxr2;/* Find the maximum values and secondary values on both sides, compare the values in the following order: */dutfindmaxandsecondmax (A, low, low + (high-low)/2, maxl, maxl2); dutfindmaxandsecondmax (A, low + (high-low) /2 + 1, high, maxr, maxr2); If (maxl> maxr) {max = maxl; If (maxl2> maxr) {max2 = maxl2 ;} else {max2 = maxr ;}} else {max = maxr; If (maxr2> maxl) {max2 = maxr2 ;}else {max2 = maxl ;}
the beauty of programming 2.10 searches for the maximum and minimum values in the array