Data Structure Multiple Choice Questions on “Uniform Binary Search”.
1. In which of the cases uniform binary search fails compared to binary search? Answer: d 2. Which of the following is a suitable lookup table that can be used in the uniform binary search?(N is the number of elements in the array and the delta array is global) b) c) d) Answer: a 3. Given delta[4] is a global array and number of elements in the sorted array is 10, what are the values in the delta array? Answer: b 4. Choose the appropriate code snippet that performs uniform binary search. b) c) d) Answer: c 5. What is the time complexity of uniform binary search? Answer: b 6. Given, arr = {1,3,5,6,7,9,14,15,17,19} key = 17 and delta = {5,3,1,0} 7. How can Jump Search be improved? 8. Which of the following false about Jump Search? Answer: d
a) A table lookup is generally faster than an addition and a shift
b) Many searches will be performed on the same array
c) Many searches will be performed on several arrays of the same length
d) Complexity of code
Clarification: Uniform binary search code is more complex to implement than binary search as it involves mid points to be computed in hand before search.
a)public static void make_delta(int N)
{
int power = 1;
int i = 0;
do
{
int half = power;
power <<= 1;
delta[i] = (N + half) / power;
}
while (delta[i++] != 0);
}
public static void make_delta(int N)
{
int power = 0;
int i = 0;
do
{
int half = power;
power <<= 1;
delta[i] = (N + half) / power;
}
while (delta[i++] != 0);
}
public static void make_delta(int N)
{
int power = 1;
int i = 0;
do
{
int half = power;
power >>= 1;
delta[i] = (N + half) / power;
}
while (delta[i++] != 0);
}
public static void make_delta(int N)
{
int power = 1;
int i = 0;
do
{
int half = power;
power <<= 1;
delta[i] = (N - half) / power;
}
while (delta[i++] != 0);
}
Clarification: This provides a single lookup index and the values are dependent on the the number of elements(N) in the array.
a) 4, 3, 1, 0
b) 5, 3, 1, 0
c) 4, 2, 1, 1
d) 5, 2, 1, 1
Clarification: Trace with respect to the make_delta function, always note that the last element is always 0.
a)public static int unisearch(int key)
{
int i = delta[0] - 1;
int j = 0;
while (true)
{
if (key == arr[i])
return i;
else if (delta[j] == 0)
return -1;
else
{
if (key < arr[i])
i += delta[++j];
else
i -= delta[++j];
}
}
}
public static int unisearch(int key)
{
int i = delta[1] - 1;
int j = 0;
while (true)
{
if (key == arr[i])
return i;
else if (delta[j] == 0)
return -1;
else
{
if (key < arr[i])
i -= delta[++j];
else
i += delta[++j];
}
}
}
public static int unisearch(int key)
{
int i = delta[0] - 1;
int j = 0;
while (true)
{
if (key == arr[i])
return i;
else if (delta[j] == 0)
return -1;
else
{
if (key < arr[i])
i -= delta[++j];
else
i += delta[++j];
}
}
}
public static int unisearch(int key)
{
int i = delta[0] - 1;
int j = 0;
while (true)
{
if (key == arr[i])
return i;
else if (delta[j] == 0)
return -1;
else
{
if (key < arr[i])
i += delta[++j];
else
i += delta[++j];
}
}
}
Clarification: Unlike the usual binary search which a low, high and a mid variable and every time comparing the key with the mid value, the comparing index is obtained from the lookup delta table, choosing the left or right side of the array is same as with the normal binary search.
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Clarification: With every iteration we are dividing the array into two parts(though not equal halves), the complexity remains same as the normal binary search.
How many key comparisons are made?(exclude the comparison used to decide the left or right sub array)
a) 4
b) 3
c) 5
d) 6
Answer: b
Clarification: Tracing with the above code, comparison #1: i=4, comparison #2: i=7, comparison #3: i=8
a) Start searching from the end
b) Begin from the kth item, where k is the step size
c) Cannot be improved
d) Step size should be other than sqrt(n)
Answer: b
Clarification: This gives a very slight improvement as you are skipping the first k elements.
a) Jump Search is better than Linear Search
b) Useful when jumping back is more costly than jumping forward
c) Jump Search is worse than Binary Search
d) Jump search starts from the index 0 even though specified index is k
Clarification: Linear search has O(n) complexity and Binary search has O(logn) complexity, in Jump search you have to jump backwards only once, hence it is preferable if jumping backwards is costly. Jump search starts from index k (specified index) and searches for the element. It won’t start searching from index 0.