0% found this document useful (0 votes)
193 views17 pages

DS On Search

Linear search is used for single searches in unordered lists or small lists. It has a worst case time complexity of O(n). Recursive linear search uses recursion to iteratively search through an array, returning the index if found or -1 if not found. Uniform binary search improves on standard binary search by using a lookup table to estimate the mid-point, reducing comparisons. It has a time complexity of O(logn). Jump search is an alternative to linear search that works by jumping ahead by fixed intervals instead of iterating one-by-one, making it faster than linear search when elements are uniformly distributed.

Uploaded by

Arockiaraj J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views17 pages

DS On Search

Linear search is used for single searches in unordered lists or small lists. It has a worst case time complexity of O(n). Recursive linear search uses recursion to iteratively search through an array, returning the index if found or -1 if not found. Uniform binary search improves on standard binary search by using a lookup table to estimate the mid-point, reducing comparisons. It has a time complexity of O(logn). Jump search is an alternative to linear search that works by jumping ahead by fixed intervals instead of iterating one-by-one, making it faster than linear search when elements are uniformly distributed.

Uploaded by

Arockiaraj J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

VIVEKANANDHA COLLEGE OF ENGINEERING FOR WOMEN

TRAINING AND PLACEMENT

Data Structure - Search

1. Where is linear searching used?


a) When the list has only a few elements
b) When performing a single search in an unordered list
c) Used all the time
d) When the list has only a few elements and When performing a single search in
an unordered list
larger elements the complexity becomes larger and it makes sense to sort the list and
employ binary search or hashing.

2. Select the code snippet which performs unordered linear search iteratively?

int unorderedLinearSearch(int arr[], int size, int data){


int index;
for(int i = 0; i < size; i++)
{ if(arr[i] == data) {
index = i; break; } } return index; }

3. What is the best case for linear search and worst case ?
a) O(nlogn)
b) O(logn)
c) O(n) (worst)
d) O(1) (best)

4. Select the code snippet which performs ordered linear search iteratively?
public int linearSearch(int arr[],int key,int size) {
int index = -1; int i = 0;
while(size > 0) {
if(data[i] == key) {
index = i; }
if(data[i] > key)) { break; }
i++; } return index;}
 
5. Choose the code snippet which uses recursion for linear search.
public void linSearch(int[] arr, int first, int last, int key){
if(first == last)
{ System.out.print("-1");}
else
{ if(arr[first] == key) {
System.out.print(first);
}
else
{
linSearch(arr, first+1, last, key);
}
}}

 
6. What does the following piece of code do?

for (int i = 0; i < arr.length-1; i++){


for (int j = i+1; j < arr.length; j++)
{
if( (arr[i].equals(arr[j])) && (i != j) )
{
System.out.println(arr[i]);
}
}}
a) Print the duplicate elements in the array
b) Print the element with maximum frequency
c) Print the unique elements in the array
d) Prints the element with minimum frequnecy

1. Is there any difference in the speed of execution between linear search(recursive) vs


linear search (lterative)?
a) Both execute at same speed
b) Linear search(recursive) is faster
c) Linear search(Iterative) is faster
d) Cant be said
2. Is the space consumed by the linear search(recursive) and linear search(iterative)
same?
a) No, recursive algorithm consumes more space
b) No, recursive algorithm consumes less space
c) Yes
d) Nothing can be said

3. What is the worst case runtime of linear search(recursive) algorithm?


a) O(n)
b) O(logn)
c) O(n2)
d) O(nx)

4. Linear search(recursive) algorithm used in _____________


a) When the size of the dataset is low O(n) than binary search O(logn)
b) When the size of the dataset is large
c) When the dataset is unordered
d) Never used

5. Which of the following code snippet performs linear search recursively?

LinearSearch(int[] a, n,key)
{
if(n<1)
return False
if(a[n]==key)
return True
else
LinearSearch(a,n-1,key)
}

7. Can linear search recursive algorithm and binary search recursive algorithm be
performed on an unordered list?
a) Binary search can’t be used, requires comparison and be ordered
b) Linear search can’t be used
c) Both cannot be used
d) Both can be used

7. What is the recurrence relation for the linear search recursive algorithm?
a) T(n-2)+c
b) 2T(n-1)+c
c) T(n-1)+c
d) T(n+1)+c
.

1. What is the advantage of recursive approach than an iterative approach?


a) Consumes less memory
b) Less code and easy to implement
c) Consumes more memory
d) More code has to be written

2. . Choose the appropriate code that does binary search using recursion.
public static int recursive(int arr[], int low, int high, int key){
int mid = low + (high - low)/2;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
return recursive(arr,mid+1,high,key);
}
else
{
return recursive(arr,low,mid-1,key);
}}

3. Given an input arr = {2,5,7,99,899}; key = 899; What is the level of recursion?
a) 5
b) 2
c) 3
d) 4
Explanation: level 1: mid = 7
level 2: mid = 99
level 3: mid = 899(this is the key).

4. Given an array arr = {45,77,89,90,94,99,100} and key = 99; what are the mid
values(corresponding array elements) in the first and second levels of recursion?
a) 90 and 99
b) 90 and 94
c) 89 and 99
d) 89 and 94

5. What is the worst and average case complexity of binary search using recursion?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Explanation: T(n) = T(n/2) + 1, Using the divide and conquer master theorem.

6. Which of the following is not an application of binary search?


a) To find the lower/upper bound in an ordered sequence
b) Union of intervals
c) Debugging
d) To search in unordered list

7. Choose among the following code for an iterative binary search.


public static int iterative(int arr[], int key){
int low = 0;
int mid = 0;
int high = arr.length-1;
while(low <= high)
{
mid = low + (high - low)/2;
if(arr[mid] == key)
{
return mid;
}
else if(arr[mid] < key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;}

 
8. Binary Search can be categorized into which of the following?
a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming

9. What is the time complexity of binary search with iteration?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Explanation: T(n) = T(n/2) + theta(1)
Using the divide and conquer master theorem, we get the time complexity as O(logn).

1. In which of the cases uniform binary search fails compared to binary search?
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

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)
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);}

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?
a) 4, 3, 1, 0
b) 5, 3, 1, 0
c) 4, 2, 1, 1
d) 5, 2, 1, 1
4. Choose the appropriate code snippet that performs uniform binary search.

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];
}
}}
 
5. What is the time complexity of uniform binary search?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
6. Given, arr = {1,3,5,6,7,9,14,15,17,19} key = 17 and delta = {5,3,1,0} 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
Initialize 'low' = 0 and 'high' = 9 (index of the last element).

Calculate the estimated position using interpolation:


delta[0] = 5 (interpolation step)
mid = low + ((key - arr[low]) / (arr[high] - arr[low])) * (high - low)
mid = 0 + ((17 - 1) / (19 - 1)) * (9 - 0) = 0 + (16 / 18) * 9 = 0 + (8/9) * 9 = 8
Compare the estimated element arr[8] = 17 with the target element 17.
The elements are equal, so the target element is found. The search ends.
During the interpolation search, the algorithm makes a total of 1 key
comparison.

Therefore, the correct answer is option b) 3 (since the algorithm only makes one key
comparison excluding the comparison used to decide the left or right subarray).

7. Which of the following false about Jump Search?


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

Explanation: 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.

1. Jump search algorithm requires which of the following condition to be true?


a) array should be sorted
b) array should have not be sorted
c) array should have a less than 64 elements
d) array should be partially sorted

2. Jumps are made in the jump search algorithm until ___________


a) element having value less than that of the required element is found
b) element having value equal to the median of values of the array is found
c) element having value greater than that of the required element is found
d) middle element is found equal to the element being searched

3. Which of the following step is taken after finding an element having value greater
than the element being searched?
a) linear search takes place in the forward direction
b) linear search takes place in the backward direction
c) binary search takes place in the forward direction
d) binary search takes place in a backward direction

4. How many jumps will be made in the worst case of jump search(let block jumped =k)?
a) n*k
b) n/k
c) k/n
d) n+k

5. What will be the maximum number of comparisons that can be made in jump search
algorithm (assuming k to be blocks jumped)?
a) k
b) n/k
c) k-1
d) k-1

6. What is the value of jump taken for maximum efficiency while implementing jump
search? Total comparisons n/k+k-1
a) n/2
b) n2
c) sqrt(n)
d) log n

7. What is the auxiliary space requirement of the jump search?


a) O(n)
b) O(log n)
c) O(n1/2)
d) O(1)

8. Which of the following searching algorithm is fastest?


a) jump search
b) binary search
c) linear search
d) all are equally fast

9. In which of the following case jump search will be preferred over binary search?
a) jumping backwards takes significantly more time than jumping forward
b) jumping forward takes significantly more time than jumping backwards
c) when the given array is very large in size
d) when the given array is very small in size
10. Which of the following code correctly represent jump search?

int jumpSearch(int arr[], int x, int n) {


  int step = sqrt(n);
int prev = 0;
while (arr[min(step, n)-1] < x)
{
prev = step;
step += sqrt(n);
if (prev >= n)
return -1;
}
   while (arr[prev] < x)
{
prev++;
  if (prev == min(step, n))
return -1;
}
 
if (arr[prev] == x)
return prev;
  return -1; }

11. Jump search is worse than linear search in terms of time complexity.
a) True
b) False
Explanation: Linear search has a time complexity of O(n) and the time complexity of
jump search is O(n1/2). So jump search is better than linear search in terms of time
complexity.

1. Which algorithmic technique does Fibonacci search use?


a) Brute force
b) Divide and Conquer
c) Greedy Technique
d) Backtracking

2. Choose the recursive formula for the Fibonacci series.(n>=1)


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)
3. Write a function for the Fibonacci search method.

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;}

4. What is the time complexity of Fibonacci Search?


a) O(logn)
b) O(n)
c) O(n2)
d) O(nlogn)

5. Which of the following is not an advantage of Fibonacci Search?


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

6. Select the code snippet for Jump Search.


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;}

7. What is the time complexity of Jump Search?


a) O(logn)
b) O(n)
c) O(sqrt(n))
d) O(nlogn)

1. Exponential search algorithm requires which of the following condition to be true?


a) array should be sorted
b) array should have not be sorted
c) array should have a less than 128 elements
d) array should be partially sorted

2. Which of the following searching algorithm is used with exponential sort after finding
the appropriate range?
a) Linear search
b) Binary search
c) Jump search
d) Fibonacci Search

3. Exponential search has ____________


a) neither an exponential space complexity nor exponential time complexity
b) exponential time complexity but a linear space complexity
c) exponential space complexity but a linear time complexity
d) both exponential time and space complexity

4. Choose the correct while loop statement from the following that finds the range
where are the element being search is present (x is the element being searched in an
array arr of size n)?
while (i < n && arr[i] <= x)
i = i*2;
Explanation: In exponential search we first find the range where the element being
searched can be present before applying binary search. We do this by comparing the
value of element under search with the array elements present at the positions
1,2,4,8….n.

 
 
5. What is the time complexity of exponential sort?
a) O(n)
b) O(2n)
c) O(n log n)
d) O(log n)

6. What is the auxiliary space requirement of the exponential sort when used with
recursive binary search?
a) O(n)
b) O(2n)
c) O(1)
d) O(log n)

7. Which of the following searching algorithm is fastest?


a) jump search
b) exponential search
c) linear search
d) all are equally fast
Explanation: Exponential search has the least time complexity (equal to log n) out of
the given searching algorithms. This makes exponential search preferable in most cases.

8. In which of the following case jump search will be preferred over exponential search?
a) jumping backwards takes significantly more time than jumping forward
b) jumping forward takes significantly more time than jumping backwards
c) when the given array is very large in size
d) when the given array is very small in size
Explanation: Jump search only needs to jump backwards once, while an exponential
search can jump backwards up to log n times. Thus jump search will be preferred if
jumping backwards is expensive.

9. Which of the following code correctly represent exponential search?


int expSearch(int arr[], int n, int x) {

if (arr[0] == x)

return 0;
int i = 1;

while (i < n && arr[i] <= x)

i = i*2;

 return binarySearch(arr, i/2, min(i, n-1), x);//applies binary search in the calculated
range}

 
 
10. Jump search has a better time complexity than the exponential search.
a) True
b) False
Answer: b
Explanation: The worst case time complexity of jump search and exponential searches
are O(n1/2) and O(log n) respectively. So exponential search is better in terms of time
complexity.

11. Exponential search performs better than binary search when the element being
searched is present near the starting point of the array.
a) True
b) False
Explanation: Exponential search first finds the range where binary search needs to be
applied. So when the element is present near the starting point of the array then
exponential search performs better than standard binary search.

14. Choose the incorrect statement about exponential search from the following.
a) Exponential search is an in place algorithm
b) Exponential search has a greater time complexity than binary search
c) Exponential search performs better than binary search when the element being
searched is present near the starting point of the array
d) Jump search has a greater time complexity than an exponential search
Explanation: Time complexity of exponential search and binary search are the
same. But exponential search performs better than binary search when the
element being searched is present near the starting point of the array.

15. Which of the following is not an alternate name of exponential search?


a) Logarithmic search
b) Doubling search
c) Galloping search
d) Struzik search

1. Which of the following is the most desirable condition for interpolation search?
a) array should be sorted
b) array should not be sorted but the values should be uniformly distributed
c) array should have a less than 64 elements
d) array should be sorted and the values should be uniformly distributed

2. Interpolation search is a variation of?


a) Linear search
b) Binary search
c) Jump search
d) Exponential search
Explanation: Interpolation search is a variation of binary search which gives the best
result when the array has uniformly distributed values. Interpolation search goes to
different positions depending on the value being searched whereas binary search
always goes to the middle element.

4. In which of the following case jump search performs better than interpolation search?
a) When array has uniformly distributed values but is not sorted
b) when array is sorted and has uniform distribution of values
c) when array is sorted but the values increases exponentially
d) when array is not sorted
Answer: c
Explanation: In case of non uniform distribution of values the time complexity of
interpolation search is O(n) whereas the average time complexity of jump search is
O(n1/2). So in such a case jump search has a better performance.

5. What is the time complexity of interpolation search when the input array has
uniformly distributed values and is sorted?
a) O(n)
b) O(log log n)
c) O(n log n)
d) O(log n)

6. What is the time complexity of exponential search when the input array is sorted but
the values are not uniformly distributed?
a) O(n1/2)
b) O(log log n)
c) O(n)
d) O(log n)

7. Which of the following searching algorithm is fastest when the input array is sorted
but has non uniformly distributed values?
a) jump search
b) linear search
c) binary search
d) interpolation search

8. Which of the following searching algorithm is fastest when the input array is not
sorted but has uniformly distributed values?
a) jump search
b) linear search
c) binary search
d) interpolation search

9. Interpolation search is an in place algorithm.


a) true
b) false
Answer: a
Explanation: Interpolation search has an auxiliary space complexity of O(1). So it
qualifies as an in place algorithm.

10. Interpolation search has a better time complexity than exponential search for any
given array.
a) True
b) False
Answer: b
Explanation: The worst case time complexity of interpolation search and exponential
search are O(n) and O(log n) respectively. So exponential search is better when the
worst case scenario is considered.

11. What are the updated values of high and low in the array if the element being
searched is greater than the value at calculated index in interpolation search? (pos =
current position)
a) low = pos + 1, high remains unchanged
b) high = pos – 1, low remains unchanged
c) low = low +1, high = high – 1
d) low = pos +1, high = pos – 1

1. Which of the following is a sub string of “YUNFOUNDAY”?


a) SANO
b) FOUND
c) SAND
d) FOND

2. What will be the output of the following code?


#include<bits/stdc++.h>
using namespace std;
 void func(char* str2, char* str1) {
int m = strlen(str2);
int n = strlen(str1);
for (int i = 0; i <= n - m; i++)
{
int j;
 
 
for (j = 0; j < m; j++)
if (str1[i + j] != str2[j])
break;
 
if (j == m)
cout << i << endl;
}}
 int main() {
char str1[] = "1253234";
char str2[] = "323";
func(str2, str1);
return 0; }
a) 1
b) 2
c) 3
d) 4
Explanation: The given code describes the naive method of finding a pattern in a string.
So the output will be 3 as the given sub string begins at that index in the pattern.

3. What will be the worst case time complexity of the following code?

#include<bits/stdc++.h>
using namespace std;
 void func(char* str2, char* str1) {
int m = strlen(str2);
int n = strlen(str1);
for (int i = 0; i <= n - m; i++)
{
int j;
 
 
for (j = 0; j < m; j++)
if (str1[i + j] != str2[j])
break;
 
if (j == m)
cout << i << endl;
}}
 int main() {
char str1[] = "1253234";
char str2[] = "323";
func(str2, str1);
return 0; }
a) O(n)
b) O(m)
c) O(m * n)
d) O(m + n)
Explanation: The given code describes the naive method of pattern searching. By
observing the nested loop in the code we can say that the time complexity of the loop is
O(m*n).

4. What is the worst case time complexity of KMP algorithm for pattern searching (m =
length of text, n = length of pattern)?
a) O(n)
b) O(n*m)
c) O(m)
d) O(log n)

5. What will be the best case time complexity of the following code?

#include<bits/stdc++.h>
using namespace std; void func(char* str2, char* str1) {
int m = strlen(str2);
int n = strlen(str1);
 
for (int i = 0; i <= n - m; i++)
{
int j;
 
 
for (j = 0; j < m; j++)
if (str1[i + j] != str2[j])
break;
 
if (j == m)
cout << i << endl;
}}
 int main() {
char str1[] = "1253234";
char str2[] = "323";
func(str2, str1);
return 0; }
a) O(n)
b) O(m)
c) O(m * n)
d) O(m + n)

6. What is the time complexity of Z algorithm for pattern searching (m = length of text, n
= length of pattern)?
a) O(n + m)
b) O(m)
c) O(n)
d) O(m * n)

7. What is the auxiliary space complexity of Z algorithm for pattern searching (m =


length of text, n = length of pattern)?
a) O(n + m)
b) O(m)
c) O(n)
d) O(m * n)
8. The naive pattern searching algorithm is an in place algorithm.
a) true
b) false

Explanation: The auxiliary space complexity required by naive pattern searching


algorithm is O(1). So it qualifies as an in place algorithm.

9. Rabin Karp algorithm and naive pattern searching algorithm have the same worst
case time complexity.
a) true
b) false
Explanation: The worst case time complexity of Rabin Karp algorithm is O(m*n) but it
has a linear average case time complexity. So Rabin Karp and naive pattern searching
algorithm have the same worst case time complexity.

You might also like