Data Structure Multiple Choice Questions on “Fibonacci Search”.
1. Which algorithmic technique does Fibonacci search use? Answer: b 2. Choose the recursive formula for the Fibonacci series.(n>=1) Answer: c 3. Write a function for the Fibonacci search method. b) c) d) Answer: a 4. What is the time complexity of Fibonacci Search? Answer: a 5. Which of the following is not an advantage of Fibonacci Search? 6. What is the length of the step in jump search? Answer: c 7. Select the code snippet for Jump Search. b) c) d) View Answer Answer: b 8. What is the time complexity of Jump Search? Answer: c complete set of 1000+
a) Brute force
b) Divide and Conquer
c) Greedy Technique
d) Backtracking
Clarification: With every iteration, we divide the given array into two sub arrays(not necessarily equal).
a) F(n) = F(n+1) + F(n+2)
b) F(n) = F(n) + F(n+1)
c) F(n) = F(n-1) + F(n-2)
d) F(n) = F(n-1) – F(n-2)
Clarification: None.
a)public static int fibSearch(final int key, final int[] a)
{
int low = 0;
int high = a.length - 1;
int fibCurrent = 1;
int fibPrev = 1;
int N = a.length;
while (low <= high)
{
while(fibCurrent < N)
{
int tmp = fibCurrent + fibPrev;
fibPrev = fibCurrent;
fibCurrent = tmp;
N = N - (fibCurrent - fibPrev);
}
final int mid = low + (high - low) - (fibCurrent + fibPrev);
if (key < a[mid]) high = mid - 1;
else if (key > a[mid]) low = mid + 1;
else return mid;
}
return -1;
}
public static int fibSearch(final int key, final int[] a)
{
int low = 0;
int high = a.length - 1;
int fibCurrent = 1;
int fibPrev = 1;
int N = a.length;
while (low <= high)
{
int tmp = fibCurrent + fibPrev;
fibPrev = fibCurrent;
fibCurrent = tmp;
N = N - (fibCurrent - fibPrev);
final int mid = low + (high - low) - (fibCurrent + fibPrev);
if (key < a[mid]) high = mid - 1;
else if (key > a[mid]) low = mid + 1;
else return mid;
}
return -1;
}
public static int fibSearch(final int key, final int[] a)
{
int low = 0;
int high = a.length - 1;
int fibCurrent = 1;
int fibPrev = 1;
int N = a.length;
while (low <= high)
{
while(fibCurrent < N)
{
int tmp = fibCurrent + fibPrev;
fibPrev = fibCurrent;
fibCurrent = tmp;
N = N - (fibCurrent - fibPrev);
}
final int mid = low + (high - low) - (fibCurrent + fibPrev);
if (key < a[mid]) low = mid + 1;
else if (key > a[mid]) high = mid - 1;
else return mid;
}
}
public static int fibSearch(final int key, final int[] a)
{
int low = 0;
int high = a.length - 1;
int fibCurrent = 1;
int fibPrev = 1;
int N = a.length;
while (low <= high)
{
while(fibCurrent < N)
{
int tmp = fibCurrent + fibPrev;
fibPrev = fibCurrent;
fibCurrent = tmp;
N = N - (fibCurrent - fibPrev);
}
final int mid = low + (high - low) - (fibCurrent + fibPrev);
if (key < a[mid]) low = mid - 1;
else if (key > a[mid]) high = mid - 1;
else return mid;
}
}
Clarification: Here instead of choosing middle of the array as a point of array division, we use Fibonacci numbers, the division index are strictly between two Fibonacci numbers.
a) O(logn)
b) O(n)
c) O(n2)
d) O(nlogn)
Clarification: Since it divides the array into two parts, although not equal, its time complexity is O(logn), it is better than binary search in case of large arrays.
a) When the element being searched for has a non uniform access storage
b) Can be used in magnetic tapes
c) Can be used for large arrays which do not fit in the CPU cache or in the RAM
d) It can be applied efficiently on unsorted arrays
Answer: d
Clarification: When the speed of access depends on the location previously accessed, Fibonacci search is better compared to binary search as it performs well on those locations which have lower dispersion. Fibonacci search won’t work on unsorted arrays. The input should be a sorted array or array should be sorted before Fibonacci search.
a) n
b) n/2
c) sqrt(n)
d) 1
Clarification: If the step size is 1, it becomes a linear search, if it is n, we reach the end of the list in just on step, if it is n/2, it becomes similar to binary search, therefore the most efficient step size is found to be sqrt(n).
a)public int jumpSearch(int arr[], int key)
{
int size = arr.length;
int step = floor(sqrt(size));
int prev = 0;
while (arr[(step > size ? step : size)] < key)
{
prev = step;
step += floor(sqrt(size));
if (step >= size)
{
return -1;
}
}
while (arr[prev] < key)
{
prev++;
if (prev == (step < size ? step : size))
{
return -1;
}
}
if (arr[prev] == key)
{
return prev;
}
return -1;
}
public int jumpSearch(int arr[], int key)
{
int size = arr.length;
int step = floor(sqrt(size));
int prev = 0;
while (arr[(step < size ? step : size)] < key)
{
prev = step;
step += floor(sqrt(size));
if (step >= size)
{
return -1;
}
}
while (arr[prev] < key)
{
prev++;
if (prev == (step < size ? step : size))
{
return -1;
}
}
if (arr[prev] == key)
{
return prev;
}
return -1;
}
public int jumpSearch(int arr[], int key)
{
int size = arr.length;
int step = floor(sqrt(size));
int prev = 0;
while (arr[(step > size ? step : size)] < key)
{
prev = step;
step += floor(sqrt(size));
if (step >= size)
{
return -1;
}
}
while (arr[prev] > key)
{
prev++;
if (prev == (step < size ? step : size))
{
return -1;
}
}
if (arr[prev] == key)
{
return prev;
}
return -1;
}
public int jumpSearch(int arr[], int key)
{
int size = arr.length;
int step = floor(sqrt(size));
int prev = 0;
while (arr[(step > size ? step : size)] < key)
{
prev = step;
step += floor(sqrt(size));
if (step <= size)
{
return -1;
}
}
while (arr[prev] > key)
{
prev++;
if (prev == (step < size ? step : size))
{
return -1;
}
}
if (arr[prev] == key)
{
return prev;
}
return -1;
}
Clarification: After finding the correct block of k elements, a sequential search is performed in this block.
a) O(logn)
b) O(n)
c) O(sqrt(n))
d) O(nlogn)
Clarification: Since the size of the step is sqrt(n), the complexity is also obviously O(sqrt(n)).