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?
N
N*log(N)
N2
N*log(N)2
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?
3
2
1
5
Question 3
Select the best description to explain what a binary search algorithm is.
Put the elements in order, check each item in turn.
Elements do not need to be in order, compare to the middle value, split the list in order and repeat
Elements do not need to be in order, check each item in turn.
Put the elements in order, compare with the middle value, split the list in order and repeat.
Question 4
What is the best-case TIme complexity of Binary search to find an element?
O(LogN)
O(N)
O(N^2)
O(1)
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?
0-1
3-4
2-3
can't find
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?
67 and 89
67 and 46
67 and 90
None
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;
}
Performing Binary search
Finding Kth occurrence of the element in O(N)
Finding the kth occurrence of the element in O(logN)
None
Question 8
Which of the following is correct recurrence for worst case of Binary Search?
T(n) = 2T(n/2) + O(1) and T(1) = T(0) = O(1)
T(n) = T(n-1) + O(1) and T(1) = T(0) = O(1)
T(n) = T(n/2) + O(1) and T(1) = T(0) = O(1)
T(n) = T(n-2) + O(1) and T(1) = T(0) = O(1)
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?
Y is [2 4 6 8 10 12 14 16 18 20] and 2 < x < 20 and x is even
Y is [2 2 2 2 2 2 2 2 2 2] and x > 2
Y is [1 3 5 7 9 11 13 15 17 19] and x < 1
Y is [1 2 3 4 5 6 7 8 9 10] and x < 10
Question 10
The time taken by binary search algorithm to search a key in a sorted array of n elements is
O ( n log2n )
O ( n )
O ( n^2)
O(log2n)
There are 12 questions to complete.