Given an array arr = {12, 34, 47, 62, 85, 92, 95, 99,105} and key = 34; what are the mid values (corresponding array elements) generated in the first and second iterations?
Answers
Answered by
16
Explanation:
Below is algorithm:
1) Initialize the largest three elements as minus infinite. first = second = third = -∞ 2) Iterate through all elements of array. a) Let current array element be x. b) If (x > first) { // This order of assignment is important third = second second = first first = x } c) Else if (x > second) { third = second second = x } d) Else if (x > third) { third = x } 3) Print first, second and third.
Answered by
17
Answer:
85 and 34
Explanation:
There are 9 elements in the initial array (index 0 to 8).
1) mid = (0+8)/2 = 4 [(low+high)/2]
arr[4] = 85(first iteration mid value);
85 > 34 (key).
Hence, low = 0(no change); high = mid-1 = 4-1 = 3
2) mid = (0+3)/2 = 1.5 = 1(integer)
arr[1] = 34 (second iteration mid value)
Similar questions