Question 1
What is the worst case time complexity of insertion sort where position of the data to be inserted is calculated using binary search?
Question 2
Given an array arr = {12, 13, 35, 78, 56} and key = 78; How many iterations are done until the element is found using Binary search?
Question 5
A binary search is to be performed on the list:
3 5 7 10 23
How many comparisons would it take to find number 7?
Question 6
What are the mid values (corresponding array items) produced in the first and second iterations for an array arr = [23, 45, 67, 89, 90,46 ]and key = 90?
Question 7
Identify what the below function is doing.
int fun(int arr[], int size, int key, int k)
{
int s = 0;
int e = size - 1;
int mid = s + (e - s) / 2;
int ans = -1;
while (s <= e) {
if (arr[mid] == key) {
if (arr[mid - k] == arr[mid])
e = mid - 1;
else if (arr[mid - (k - 1)] == arr[mid]) {
ans = mid;
break;
}
else {
s = mid + 1;
}
}
else if (key < arr[mid]) {
e = mid - 1;
}
else if (key > arr[mid]) {
s = mid + 1;
}
mid = s + (e - s) / 2;
}
return ans;
}
Question 9
Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous.
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 10
The time taken by binary search algorithm to search a key in a sorted array of n elements is
There are 12 questions to complete.