The minimum number of comparisons required to find the minimum and the...
Steps to find minimum and maximum element out of n numbers:
1. Pick 2 elements(a, b), compare them. (say a > b)
2. Update min by comparing (min, b)
3. Update max by comparing (max, a)
Therefore, we need 3 comparisons for each 2 elements, so total number of required comparisons will be (3n)/2 - 2, because we do not need to update min or max in the very first step. Recurrence relation will be:
T(n) = T(⌈n/2⌉)+T(⌊n/2⌋)+2 = 2T(n/2)+2 = ⌈3n/2⌉-2
By putting the value n=100, (3*100/2)-2 = 148 which is answer.
View all questions of this test
The minimum number of comparisons required to find the minimum and the...
Finding minimum and maximum of 100 numbers
To find the minimum and the maximum of 100 numbers, we can use the following algorithm:
1. Initialize the minimum and maximum variables with the first number in the list.
2. Loop through the remaining numbers in the list.
3. For each number, compare it with the current minimum and maximum.
4. Update the minimum and maximum variables accordingly.
5. After the loop, the minimum and maximum variables will contain the minimum and maximum numbers in the list.
Calculating minimum number of comparisons
To calculate the minimum number of comparisons required by the algorithm, we need to consider two cases:
1. Even number of elements: In this case, we can pair the elements and compare them pairwise to find the minimum and maximum of each pair. Then, we compare the minimums and maximums to find the overall minimum and maximum. This requires 3(n/2) - 2 comparisons.
2. Odd number of elements: In this case, we can treat the last element separately and compare it with the current minimum and maximum. This requires 3(n-1)/2 comparisons.
Since we have 100 elements, which is an even number, we can use the first case. Substituting n = 100 in the formula, we get:
3(100/2) - 2 = 150 - 2 = 148
Therefore, the minimum number of comparisons required to find the minimum and the maximum of 100 numbers is 148.
To make sure you are not studying endlessly, EduRev has designed Computer Science Engineering (CSE) study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Computer Science Engineering (CSE).