Вы находитесь на странице: 1из 20

Finding Minimum & Maximum

Dr. Bibhudatta Sahoo


Communication & Computing Group
Department of CSE, NIT Rourkela
Email: bdsahu@nitrkl.ac.in, 9937324437, 2462358
Moodle server : http://moodle.nitrkl.ac.in
The MinMax Problem
 Given a sequence of integers, find the minimum and the
maximum values.
 The straightforward solution to this problem is to scan
the elements searching for the minimum, which would
require n−1 comparisons, then scan the elements one more
time to search for the maximum.
 Thus in total, there will be 2n−2 comparisons.

2 Maximum and Minimum


Algorithm Straightforward maximum and minimum

This is an O(N) algorithm

3 Maximum and Minimum


Algorithm Straightforward maximum and minimum

This algorithm is to scan the elements searching for the


minimum, which would require n−1 comparisons, then scan the
elements one more time to search for the maximum.
Thus in total, there will be 2n−2 comparisons [best, average and
worst case].
4 Maximum and Minimum
An immediate improvement

Replacing the line number 7 and 8 with the following

5 Maximum and Minimum


Count of comparisons performed by the algorithm

 Now the best case occurs when the elements are in


increasing order. The number of element comparisons is
n-1.
 The worst case occurs when the elements are in
decreasing order. In this case the number of element
comparisons is 2(n-1).
 The average number of element comparison is less than
2(n - 1).
 On the average, a[i] is greater than max half the time,
and so the average number of comparisons is 3n/2 - 1

6 Maximum and Minimum


A divide-and-conquer algorithm for the MinMax problem

7 Maximum and Minimum


8 Maximum and Minimum
The tree of recursive calls for computing MinMax(A,1,16).

9 Maximum and Minimum


Analysis

T ( 2)  1 (assume N  2 ) k

T ( N )  2T ( N / 2)  2

T ( N )  2[ 2T ( N / 4)  2]  2
T ( N )  2 T ( N / 2 )  ( 2  2)
2 2 2

r 1
T (N )  2 T (N / 2 )   2
r r i

i 1

10 Maximum and Minimum


Analysis…

k 2
T (N )  2 k 1
T (N / 2 k 1
)  2 2 i

i 0

T ( N )  2 k 1  2( 2 k 1  1)
N N
T (N )   2(  1) N

2 2 Note :  2
i 0
i
 2 N 1
1
3N
T (N )  2
2
Note, although this is still O(N), it is better than the
T(N)=2N found earlier.

11 Maximum and Minimum


Thanks for Your Attention!

12 Maximum and Minimum


APPENDIX
C Program to find maximum/minimum element in array

13 Maximum and Minimum


C Program to find maximum/minimum element in array
 #include <stdio.h>
 int main()
 {
 int arr[100];
 int i, max, min, size;
 /* Reads size array and elements in the array */
 printf("Enter size of the array: ");
 scanf("%d", &size);
 printf("Enter elements in the array: ");
 for(i=0; i<size; i++)
 { scanf("%d", &arr[i]); }

14 Maximum and Minimum


C Program to find maximum/minimum element in array

 /* Supposes the first element as maximum and minimum */


 max = arr[0];
 min = arr[0];
 /*
 * Finds maximum and minimum in all array elements.
 */
 for(i=1; i<size; i++)
 {
 /* If current element of array is greater than max */
 if(arr[i]>max)
 {
 max = arr[i];
 }

15 Maximum and Minimum


C Program to find maximum/minimum element in array
 /* If current element of array is smaller than min */
 if(arr[i]<min)
 {
 min = arr[i];
 }
 }
 /*
 * Prints the maximum and minimum element
 */
 printf("Maximum element = %d\n", max);
 printf("Minimum element = %d", min);
 return 0;
 }

16 Maximum and Minimum


Exercises
Exercises
 How do I find the max and min value of an array in 3n/2−2
comparisons?
 Consider a divide and conquer approach for finding minimum and
maximum elements in an array. Calculate the number of
operations in a divide and conquer approach when the input is not
an exact power of 2.
 Write an algorithm to find minimum and maximum value using
divide and conquer and also drive its complexity.

18 Maximum and Minimum


Finding the Majority Element
 In an election involving n voters and k candidates, each voter casts his
vote to one of the k candidates. Thus, the outcome from voting can be
represented as an n-element sequence where an element is an integer in
[1,k]. There are then various criteria to determine the winner. One
criterion could be to declare the candidate who scores more than
50% of the votes as the winner. Such element is known as the
majority element. Since the majority element is not always assured, an
alternative criterion is that the winner be the candidate that scores
most votes. Such element is known as the mode element. Still
another possibility is to have a rerun election limited to the
candidates who score above certain threshold.

19 Maximum and Minimum


Finding the Majority Element
 Problem: Given a sequence of n elements where each
element is an integer in [1,k], return the majority element
(an element that appears more than n/2 times) or zero if
no majority element is found. A sequence of size 2 has a
majority only if both elements are equal. For an input of
size n=8 (and n=9), the majority element must appear at
least 5 times. For example, for the sequence
1,2,1,3,1,1,4,1 the majority element is 1.

20 Maximum and Minimum

Вам также может понравиться