Solution One:
The maximum and minimum values in the array are evaluated separately. Scan through the array first to find the maximum number and the smallest number. Need to compare 2*n times.
Solution Two:
In general, the maximum and minimum values are not the same. So divide the array into two parts, and then find the maximum and minimum values from the two parts respectively.
Finally, the max=9,min=3 are obtained from the odd and even digits, each need to compare N/2 times, the whole algorithm needs to compare 1.5*n times.
Solution Three:
The solution two destroys the array, and if it is compared during the traversal without exchanging elements, it is not possible to break the array. First, the contiguous two numbers in the array are still sorted in the same group (conceptually grouped), and then two variables Max, and Min are used to store the maximum and minimum values, then the two numbers of the same group are compared, but not the order, but the min and the smaller values are compared. if it is smaller than min, then update min is the value. Max and the larger value comparison,
Finally, max=9,min=3. However, the complexity of time is not reduced, and the number of comparisons in the process is still 1.5*n times.
Solution Four:
Using the idea of divide and conquer, in the N number to find the maximum max and Min min, only need to find out the number of Min and Max before and after N/2, and then take the smaller min, and the larger Max can be. (Only a larger number and a larger number comparison, a smaller number and a smaller number can be compared two times).
Suppose we ask for the maximum and minimum values of the ARR[1,2,3.....N] array, the pseudo-code is as follows:
(Max,min)Search(Arr,b, e) {if(b-e<=1){if(arr[b]<arr[e]) {return(arr[e],arr[b]);}Elsereturn(arr[b],arr[e]);(maxl,minl) =Search(Arr,b,b+ (E-b)/2);(Maxr,minr) =Search(Arr,b+ (E-b)/2+1, e);if(MAXL>MAXR) Maxv=minl;ElseMaxv=minr;if(MINL<MINR) Minv=minl;ElseMinv=minr;return(MAXV,MINV);}
Finding the minimum value of the maximum value in an array