开发者

Simultaneous maximum & minimum from a set of elements

In Book "Intro to algorithm -- by CLRS" mentioned in Sec-9.1 that Simultaneous Maximum & minimum finding can be done in the running time 3*floor(n/2). Same as for lower-bound also.

I have written the following algorithm (taking help from CLRS) whose lower-bound is 2*floor(n/2). [ This is wrong --- thanks to Ricky Bobby ]

I m using the variables: 'max' & 'min' to denote maximum & minimum elements, 'i' as indexing variable.

Simultaneous_Maximum_&_Minimum (A, n)  // Array A[1...n]
{
  1. If n is even, compare the first two elements and assign the larger to max and
      smaller to min. Set StartIndex = 3.

  2. If n is odd, set both max & min to the first element. Set StartIndex = 2.

  3. for (i = StartIndex; i <= n; i = i+2 ) {  // Processing elements in pair

      开发者_如何学编程   if (max < A[i]) {          // Compare max with A[i]
               if (A[i] < A[i+1])   // Compare max with A[i+1]
                     max = A[i+1];  // we sure that A[i] or A[i+1] can't be min

               else {                 // When max less than A[i] but A[i] not less than A[i+1]
                     max = A[i];        // Set A[i] as max
                     if (min > A[i+1])  // Possible that A[i+1] can be min
                          min = A[i+1]; // if yes, set A[i+1] as min
               }
         }

            // When max is not less than A[i]
         else if (min > A[i]) {        // Compare min with A[i]
                     if (A[i] > A[i+1])  // Compare min with A[i+1]
                           min = A[i+1]; // we sure that A[i] or A[i+1] can't be max

                     else {             // When min is greater than A[i] but A[i] not greater than A[i+1]
                           min = A[i];       // Set A[i] as min
                           if (max < A[i+1]) // Possible that A[i+1] can be max
                                max = A[i+1] // if yes, set A[i+1] as max
                     }
                }                    
         }

         else {  // max > A[i] and min < A[i] -- so A[i] can't be max or min

              if (max < A[i+1])
                  max = A[i+1]

              if (min > A[i+1])
                  min = A[i+1] 
         }
     }
}

Thanks to Ricky Bobby for pointing out my mistakes -- we can't done find simultaneous maximum & minimum in 2*floor(n/2) running time.


Tell me if i'm wrong, but you're not look at the case : max>A[i]>min

if max>A[i]>min :
   if A[i+1] > max 
       max =A[i+1]
   if A[i+1] <min 
       min = A[i+1]

So your algorithm is wrong in general case.

In decreasing order or increasing order it might works, but it's pretty useless, since for a sorted list you can find min and max with this espression :

(min,max) = (A[0],A[n]).sort()

so in O(get(A[0]) + get(A[n]))


Methinks that if the array is sorted, the minimum can be found at array[0] and the maximun at array [n-1]. Or the other way round for a descending ordering.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜