Top MCQs on Searching Algorithm with Answers (original) (raw)
Linear search is also called------
Given a sorted array of integers, what can be the minimum worst-case time complexity to find ceiling of a number x in given array? The ceiling of an element x is the smallest element present in array which is greater than or equal to x. Ceiling is not present if x is greater than the maximum element present in array. For example, if the given array is {12, 67, 90, 100, 300, 399} and x = 95, then the output should be 100.
The increasing order of performance of the searching algorithms are:
- linear search < jump search < binary search
- linear search > jump search < binary search
- linear search < jump search > binary search
- linear search > jump search > binary search
The average case occurs in the Linear Search Algorithm when:
- The item to be searched is in some where middle of the Array
- The item to be searched is not in the array
- The item to be searched is in the last of the array
- The item to be searched is either in the last or not in the array
Consider the C function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order.
C++ `
#include using namespace std;
int ProcessArray(int *listA, int x, int n) { int i, j, k; i = 0; j = n-1; do { k = (i+j)/2; if (x <= listA[k]) j = k-1; if (listA[k] <= x) i = k+1; } while (i <= j); if (listA[k] == x) return(k); else return -1; }
C
#include <stdio.h>
int ProcessArray(int *listA, int x, int n) { int i, j, k; i = 0; j = n-1; do { k = (i+j)/2; if (x <= listA[k]) j = k-1; if (listA[k] <= x) i = k+1; } while (i <= j); if (listA[k] == x) return(k); else return -1; }
Java
public class Main { public static int ProcessArray(int[] listA, int x, int n) { int i = 0, j = n - 1, k; do { k = (i + j) / 2; if (x <= listA[k]) j = k - 1; if (listA[k] <= x) i = k + 1; } while (i <= j); if (listA[k] == x) return k; else return -1; } }
Python
def ProcessArray(listA, x, n): i = 0 j = n - 1 while i <= j: k = (i + j) // 2 if x <= listA[k]: j = k - 1 if listA[k] <= x: i = k + 1 if listA[k] == x: return k else: return -1
JavaScript
function ProcessArray(listA, x, n) { let i = 0; let j = n - 1; let k; do { k = Math.floor((i + j) / 2); if (x <= listA[k]) j = k - 1; if (listA[k] <= x) i = k + 1; } while (i <= j); if (listA[k] === x) return k; else return -1; }
`
Which one of the following statements about the function ProcessArray is CORRECT?
- It will run into an infinite loop when x is not in listA.
- It is an implementation of binary search.
- It will always find the maximum element in listA.
- It will return −1 even when x is present in listA.
The average number of key comparisons done in a successful sequential search in a list of length n is
Consider the following program that attempts to locate an element x in a sorted array a[ ] using binary search. Assume _N>1. The program is erroneous. Under what conditions does the program fail?
C++ `
#include #include using namespace std;
int find(vectora, int n) { int i = 1, j = n; int x; do { int k = (i + j) / 2; if (a[k] < x) i = k + 1; else j = k; } while (a[k] != x && i < j);
if (a[k] == x)
cout << "x is in the array" << endl;
else
cout << "x is not in the array" << endl;
return 0;
}
C
#include <stdio.h> #include <stdbool.h>
int find(int a[], int n, int x) { int i = 1, j = n; int k; do { k = (i + j) / 2; if (a[k] < x) i = k + 1; else j = k; } while (a[k] != x && i < j);
if (a[k] == x)
printf("x is in the array\n");
else
printf("x is not in the array\n");
return 0;
}
Java
import java.util.List;
public class Main { public static void find(int arr[], int n, int x) { int i = 0, j = n; int k; do { k = (i + j) / 2; if (arr[k] < x) i = k + 1; else j = k; } while (i < j && arr[k] != x);
if (arr[k] == x)
System.out.println("x is in the array");
else
System.out.println("x is not in the array");
}
}
Python
def find(a, n, x): i = 0 j = n while i < j: k = (i + j) // 2 if a[k] < x: i = k + 1 else: j = k
if i < len(a) and a[i] == x:
print("x is in the array")
else:
print("x is not in the array")
JavaScript
function find(a, n, x) { let i = 0, j = n; let k; do { k = Math.floor((i + j) / 2); if (a[k] < x) i = k + 1; else j = k; } while (a[k] !== x && i < j);
if (a[k] === x)
console.log("x is in the array");
else
console.log("x is not in the array");
}
`
- x is the last element of the array a[]
- x is greater than all elements of the array a[]
- x is less than the last element of the array a[]
The necessary condition for using binary search in an array is :-
- The array should not be too long
- The array should of more size
- The array should be sorted
what is the name of the below searching Algorithm?
C++ `
int function(vector arr,int x){ int n = arr.size();
if(n == 0)
return -1;
// Find range for binary search by repeatedly doubling i
int i = 1;
while(i < n and arr[i] < x)
i *= 2;
// Perform binary search on the range [i/2, min(i, n-1)]
int left = i /2;
int right = min(i, n-1);
while(left <= right){
int mid = (left + right)/2;
if(arr[mid] == x) return mid;
else if(arr[mid] < x) left = mid + 1;
else right = mid - 1;
}
return -1;
}
C
#include <stdio.h> #include <stddef.h>
int binary_search(int arr[], int n, int x) { if(n == 0) return -1;
// Find range for binary search by repeatedly doubling i
int i = 1;
while(i < n && arr[i] < x)
i *= 2;
// Perform binary search on the range [i/2, min(i, n-1)]
int left = i / 2;
int right = (i < n) ? i : n - 1;
while(left <= right) {
int mid = (left + right) / 2;
if(arr[mid] == x) return mid;
else if(arr[mid] < x) left = mid + 1;
else right = mid - 1;
}
return -1;
}
Java
import java.util.*;
public class Main { public static int function(List arr, int x) { int n = arr.size();
if (n == 0)
return -1;
// Find range for binary search by repeatedly doubling i
int i = 1;
while (i < n && arr.get(i) < x)
i *= 2;
// Perform binary search on the range [i/2, min(i, n-1)]
int left = i / 2;
int right = Math.min(i, n - 1);
while (left <= right) {
int mid = (left + right) / 2;
if (arr.get(mid) == x) return mid;
else if (arr.get(mid) < x) left = mid + 1;
else right = mid - 1;
}
return -1;
}
}
Python
def function(arr, x): n = len(arr)
if n == 0:
return -1
# Find range for binary search by repeatedly doubling i
i = 1
while i < n and arr[i] < x:
i *= 2
# Perform binary search on the range [i/2, min(i, n-1)]
left = i // 2
right = min(i, n - 1)
while left <= right:
mid = (left + right) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
left = mid + 1
else:
right = mid - 1
return -1
JavaScript
function functionSearch(arr, x) { let n = arr.length;
if (n === 0)
return -1;
// Find range for binary search by repeatedly doubling i
let i = 1;
while (i < n && arr[i] < x)
i *= 2;
// Perform binary search on the range [i/2, Math.min(i, n-1)]
let left = Math.floor(i / 2);
let right = Math.min(i, n - 1);
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === x) return mid;
else if (arr[mid] < x) left = mid + 1;
else right = mid - 1;
}
return -1;
}
`
What is the time complexity for performing Jump search?
There are 15 questions to complete.
Take a part in the ongoing discussion