Question 11
Consider the following program that attempts to locate an element x in a sorted array a[ ] using binary search. Assume N>1. The program is erroneous. Under what conditions does the program fail?
var i,j,k: integer; x: integer; a: array; [1....N] of integer; begin i:= 1; j:= N; repeat k:(i+j) div 2; if a[k] < x then i:= k else j:= k until (a[k] = x) or (i >= j); if (a[k] = x) then writeln (\'x is in the array\') else writeln (\'x is not in the array\') end;
Question 12
The recurrence relation that arises in relation with the complexity of binary search is:
Question 13
Suppose there are 11 items in sorted order in an array. How many searches are required on the average, if binary search is employed and all searches are successful in finding the item?
Question 15
The time taken by binary search algorithm to search a key in a sorted array of n elements is
Question 16
Match the following:
List - I | List - II |
(a) Sequential Search | (i) Dynamic programming principle |
(b)Branch - and - bound | (ii) repeatedly double index |
(c) Exponential Search | (iii) O(LogN) |
(d) Binary Search | (iv)O(N) |
codes:
(a) | (b) | (c) | (d) | |
(1) | (i) | (iv) | (iii) | (ii) |
(2) | (iv) | (i) | (ii) | (iii) |
(3) | (i) | (iv) | (ii) | (iii) |
(4) | (iv) | (ii) | (i) | (iii) |
Question 17
Question 19
Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous. (GATE CS 2008)
1. f(int Y[10], int x) {
2. int i, j, k;
3. i = 0; j = 9;
4. do {
5. k = (i + j) /2;
6. if( Y[k] < x) i = k; else j = k;
7. } while(Y[k] != x && i < j);
8. if(Y[k] == x) printf (\"x is in the array \") ;
9. else printf (\" x is not in the array \") ;
10. }
On which of the following contents of Y and x does the program fail?
Question 20
what is the name of the below searching Algorithm?
int function(vector<int> arr,int x){
int n = arr.size();
if(n == 0)
return -1;
// Find range for binary search by repeatedly doubling i
int i = 1;
while(i < n and arr[i] < x)
i *= 2;
// Perform binary search on the range [i/2, min(i, n-1)]
int left = i /2;
int right = min(i, n-1);
while(left <= right){
int mid = (left + right)/2;
if(arr[mid] == x) return mid;
else if(arr[mid] < x) left = mid + 1;
else right = mid - 1;
}
return -1;
}
There are 28 questions to complete.