Sorting & Searching
Sorting & Searching
Sorting & Searching
a) N
b) N-1
c) N+1
d) N2
4. What is the running time of an insertion sort algorithm if the input is pre-
sorted?
a) O(N2)
b) O(N log N)
c) O(N)
d) O(M log N)
5. What will be the number of passes to sort the elements using insertion sort?
14, 12,16, 6, 3, 10
a) 6
b) 5
c) 7
d) 1
6. Using insertion-sort, how will the array elements look like after second pass?
34, 8, 64, 51, 32, 21
a) 8, 21, 32, 34, 51, 64
b) 8, 32, 34, 51, 64, 21
c) 8, 34, 51, 64, 32, 21
d) 8, 34, 64, 51, 32, 21
7. Which of the following real time examples is based on insertion sort?
a) arranging a pack of playing cards
b) database scenarios and distributes scenarios
c) arranging books on a library shelf
d) real-time systems
8. In Java, what are the basic loops required to perform an insertion sort?
a) do- while
b) if else
c) for and while
d) for and if
9. Binary search can be used in an insertion sort algorithm to reduce the number
of comparisons.
a) True
b) False
10. Which of the following options contain the correct feature of an insertion sort
algorithm?
a) anti-adaptive
b) dependable
c) stable, not in-place
d) stable, adaptive
11. Which of the following sorting algorithms is the fastest for sorting small
arrays?
a) Quick sort
b) Insertion sort
c) Shell sort
d) Heap sort
12. For the best case input, the running time of an insertion sort algorithm is?
a) Linear
b) Binary
c) Quadratic
d) Depends on the input
13. Which of the following examples represent the worst case input for an
insertion sort?
a) array in sorted order
b) array sorted in reverse order
c) normal unsorted array
d) large array
15. Which of the following sorting algorithm is best suited if the elements are
already sorted?
a) Heap Sort
b) Quick Sort
c) Insertion Sort
d) Merge Sort
16. The worst-case time complexity of insertion sort is O(n2). What will be the
worst-case time complexity of insertion sort if the correct position for inserting
element is calculated using binary search?
a) O(n log n)
b) O(n2)
c) O(n)
d) O(log n)
19. Which of the following is good for sorting arrays having less than 100
elements?
a) Quick Sort
b) Selection Sort
c) Merge Sort
d) Insertion Sort
20. Consider an array of length 5, arr[5] = {9,7,4,2,1}. What are the steps of
insertions done while running insertion sort on the array?
a) 7 9 4 2 1 4 7 9 2 1 2 4 7 9 1 1 2 4 7 9
b) 9 7 4 1 2 9 7 1 2 4 9 1 2 4 7 1 2 4 7 9
c) 7 4 2 1 9 4 2 1 9 7 2 1 9 7 4 1 9 7 4 2
d) 7 9 4 2 1 2 4 7 9 1 4 7 9 2 1 1 2 4 7 9
21. Statement 1: In insertion sort, after m passes through the array, the first m
elements are in sorted order.
Statement 2: And these elements are the m smallest elements in the array.
a) Both the statements are true
b) Statement 1 is true but statement 2 is false
c) Statement 1 is false but statement 2 is true
d) Both the statements are false
22. In insertion sort, the average number of comparisons required to place the
7th element into its correct position is _________________.
a) 9
b) 4
c) 7
d) 14
From 14 to 23:
Insertion Sort Interview Questions and Answers - Sanfoundry
1- Consider the following array and what is the status of the array after the
fourth pass when we use the insertion sort? Array Elements: 20, 16, 12, 8, 4, 1
a. 16, 20, 12, 8, 4, 1
b. 12, 16, 20, 8, 4, 1
c. 8, 12, 16, 20, 4, 1
d. 4, 8, 12, 16, 20, 1
2- How many pass will run insertion sort to sort the 8 elements?
a. 9
b. 8
c. 7
d. 6
3- Which sorting is using the given example? A player is playing a card game and
sorting the cards. The Player first picks one card then picks the next card and put
it after the first card if it is bigger or before the first card if it is smaller; then he
picks another card and inserts it into its proper position.
a. Bubble Sort
b. Selection Sort
c. Insertion Sort
d. None of the above
4- If the array A contains the items 10, 4, 7, 23, 67, 12 and 5 in that order, what
will be the resultant array A after third pass of insertion sort?
a. 4, 7, 10, 23, 67, 12, 5
b. 4, 5, 7, 67, 10, 12, 23
c. 10, 7, 4, 67, 23, 12, 5
d. 67, 12, 10, 5, 4, 23, 7
5- Consider the following array {23, 32, 45, 69, 72, 73, 89, 97} Which algorithm
out of the following options uses the least number of comparisons (among the
array elements) to sort above array in ascending order?
a. Insertion sort
b. Selection sort
c. Quicksort using the last element as pivot
d. Merge sort
6- If the array A contains the items 10, 4, 7, 23, 67, 12 and 5 in that order, what
will be the resultant array A after third pass of insertion sort?
a. 67, 12, 10, 5, 4, 7, 23
b. 4, 7, 10, 23, 67, 12, 5
c. 4, 5, 7, 67, 10, 12, 23
d. 10, 7, 4, 67, 23, 12, 5
7. Which of the following is TRUE of the time complexity of the insertion sort
algorithm to sort a list of n integers?
a. Ω (n2)
b. Ω (n)
c. O (n)
d. O (n log (n))
8. The given array is arr = {1,2,3,4,5}. (Bubble sort is implemented with a flag
variable) The number of iterations in selection sort and bubble sort respectively
are __________
a) 5 and 4
b) 1 and 4
c) 0 and 4
d) 4 and 1
c)
d)
6. Which of the following is not an advantage of optimized bubble sort over other
sorting techniques in case of sorted elements?
a) It is faster
b) Consumes less memory
c) Detects whether the input is already sorted
d) Consumes less time
Answer: c
7. The given array is arr = {1, 2, 4, 3}. Bubble sort is used to sort the array
elements. How many iterations will be done to sort the array?
a) 4
b) 2
c) 1
d) 0
Answer: a
8. How can you improve the best case efficiency in bubble sort? (The input is
already sorted)
a)
c)
d)
Answer: c
9. What is the best case efficiency of bubble sort in the improvised version?
a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)
Answer: c
Explanation: Some iterations can be skipped if the list is sorted, hence efficiency
improves to O(n).
10. The given array is arr = {1,2,4,3}. Bubble sort is used to sort the array
elements. How many iterations will be done to sort the array with improvised
version?
a) 4
b) 2
c) 1
d) 0
Answer: b
b) 2
c) 1
d) 3
10- What is the formula to be used to calculate the total number of comparisons
in bubble sort with n elements after k iterations?
1. (4k2 - k)/2
2. (2kn - k2 + k)/2
3. (2kn - k2 -k)/3
4. (2kn + k2 + k)/2
11- What is the minimum number of swaps required to sort the given set of
elements in increasing order using bubble sort algorithm? {5, 3, 2, 6, 12, 1, 10, 7,
4, 8, 9}
a) 10
b) 20
c) 16
d) 21
12- Consider an array with 6 elements starting with index 0 given below
20, 10, 9, 40, 21, 19
What is the sequence of the data after 1st iteration, if bubble sort is applied on
the given set of data?
a) 9, 10, 19, 20, 21, 40
b) 10, 9, 20, 19, 21, 40
c) 10, 9, 20, 21, 19, 40
d) 40, 9, 20, 21, 19, 10
13- Consider the below-given statement written in C language: int A = {25, 9, 15,
30, 11, 17, 22} Bubble sort is applied on array A, Which element is present at
index 2 after the second iteration?
a) 17
b) 9
c) 22
d) 11
4. Which of the following methods is the most effective for picking the pivot
element?
a) first element
b) last element
c) median-of-three partitioning
d) random element
Answer: c
5. Find the pivot element from the given input using median-of-three partitioning
method.
8, 1, 4, 9, 6, 3, 5, 2, 7, 0.
a) 8
b) 7
c) 9
d) 6
Answer: d
Explanation: Left element=8, right element=0,
Centre = [position (left + right)/2] = 6.
6. Which is the safest method to choose a pivot element?
a) choosing a random element as pivot
b) choosing the first element as pivot
c) choosing the last element as pivot
d) median-of-three partitioning method
Answer: a
Explanation: This is the safest method to choose the pivot element since it is very
unlikely that a random pivot would consistently provide a poor partition.
8. Which of the following sorting algorithms is used along with quick sort to sort
the sub arrays?
a) Merge sort
b) Shell sort
c) Insertion sort
d) Bubble sort
Answer: c
Explanation: Insertion sort is used along with quick sort to sort the sub arrays.
It is used only at the end.
10. How many sub arrays does the quick sort algorithm divide the entire array
into?
a) one
b) two
c) three
d) four
Answer: b
11. Which is the worst method of choosing a pivot element?
a) first element as pivot
b) last element as pivot
c) median-of-three partitioning
d) random element as pivot
Answer: a
12. Which among the following is the best cut-off range to perform insertion sort
within a quick sort?
a) N=0-5
b) N=5-20
c) N=20-30
d) N>30
Answer: b
Explanation: A good cut-off range is anywhere between N=5 and N=20 to avoid nasty
degenerate cases.
4. The best case behavior occurs for quick sort is, if partition splits the array of
size n into __________
a) n/2 : (n/2) – 1
b) n/2 : n/3
c) n/4 : 3n/2
d) n/4 : 3n/4
Answer: a
6. Consider the Quick sort algorithm in which the partitioning procedure splits
elements into two sub-arrays and each sub-array contains at least one-fourth of
the elements. Let T(n) be the number of comparisons required to sort array of n
elements. Then T(n)<=?
a) T(n) <= 2 T(n/4) + cn
b) T(n) <= T(n/4) + T(3n/4) + cn
c) T(n) <= 2 T(3n/4) + cn
d) T(n) <= T(n/3) + T(3n/4) + cn
7. Consider the Quick sort algorithm which sorts elements in ascending order
using the first element as pivot. Then which of the following input sequence will
require a maximum number of comparisons when this algorithm is applied on it?
a) 22 25 56 67 89
b) 52 25 76 67 89
c) 22 25 76 67 50
d) 52 25 89 67 76
Answer: a
8. A machine needs a minimum of 200 sec to sort 1000 elements by Quick sort.
The minimum time needed to sort 200 elements will be approximately __________
a) 60.2 sec
b) 45.54 sec
c) 31.11 sec
d) 20 sec
Answer: c
9. Which one of the following sorting algorithms is best suited to sort an array of
1 million elements?
a) Bubble sort
b) Insertion sort
c) Merge sort
d) Quick sort
Answer: d
b)
c)
Answer: a
5. The given array is arr = {2,3,4,1,6}. What are the pivots that are returned as a
result of subsequent partitioning?
a) 1 and 3
b) 3 and 1
c) 2 and 6
d) 6 and 2
Answer: a
6. What is the average case complexity of Quicksort?
a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)
Answer: a
7. The given array is arr = {2,6,1}. What are the pivots that are returned as a result
of subsequent partitioning?
a) 1 and 6
b) 6 and 1
c) 2 and 6
d) 1
Answer: d
Answer: b
9. Choose the incorrect statement about merge sort from the following?
a) it is a comparison based sort
b) it is an adaptive algorithm
c) it is not an in place algorithm
d) it is stable algorithm
Answer: b
14. Which of the following sorting algorithm makes use of merge sort?
a) Tim sort
b) Intro sort
c) Bogo sort
d) Quick sort
Answer: a
Explanation: Tim sort is a hybrid sorting algorithm as it uses more than one sorting
algorithm internally. It makes use of merge sort and insertion sort.
c)
}
}
d)
16. Which of the following sorting algorithm does not use recursion?
a) quick sort
b) merge sort
c) heap sort
d) bottom up merge sort
Answer: d
22. The given array is arr = {1,2,4,3}. Bubble sort is used to sort the
array elements. How many iterations will be done to sort the array?
A. 4
B. 2
C. 1
D. 0
23.What is the best case efficiency of bubble sort in the improvised
version?
A. O(n log n)
B. O(log n)
C. O(n)
D. O(n2)
5. Consider the following array A, and the searching element is X. How many
comparisons are required to search an element X in array A. A[ ]= {25, 45, 87,
21, 18, 49, 13, 115, 83, 65} X = 83
a) 7
b) 8
c) 9
d) 10
6. Which of the following is/are true about the search in an array data
structure with N element?
a) Only I
b) Only II
c) Both I and II
d) None of these
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
10. The Average case occurs in the linear search algorithm …………………
a) when the item is somewhere in the middle of the array
b) when the item is not the array at all
c) when the item is the last element in the array
d) Item is the last element in the array or item is not there at all
11. Binary search algorithm cannot be applied to ……………………
a) sorted linked list
b) sorted binary trees
c) sorted linear array
d) pointer array
12. Which of the following is not the required condition for a binary search
algorithm?
a) The list must be sorted
b) There should be direct access to the middle element in any sub-list
c) There must be a mechanism to delete and/or insert elements in the list.
d) Number values should only be present.
14. 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
15. 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
16. What is the worst case complexity of binary search using recursion?
a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)
17. What is the average case time complexity of binary search using
recursion?
a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)
20. Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are
done until the element is found?
a) 1
b) 3
c) 4
d) 2
21. Given an array arr = {45,77,89,90,94,99,100} and key = 100; What are the
mid values (corresponding array elements) generated in the first and second
iterations?
a) 90 and 99
b) 90 and 100
c) 89 and 94
d) 94 and 99
II - Binary search may not be applied on all the input lists on which linear
search can be applied.
a) Only I
b) Only II
c) Both I and II
d) Neither I nor II
a) 16
b) 5
c) 8
d) 2
25. What is the best case complexity of binary search with iteration?
a) O(n log n)
b) O(log n)
c) O(n)
d) O(1)
26. What is the worst case time complexity of insertion sort where position of
the data to be inserted is calculated using binary search?
a) N
b) N*log(N)
c) N2
d) N*log(N)2
27. Select the best description to explain what a linear search algorithm
is……………..
b) Elements do not need to be in order, compare to the middle value, split the list in
order and repeat
d) Put the elements in order, compare with the middle value, split the list in order
and repeat.
28. Select the best description to explain what a binary search algorithm
is…………………
b) Elements do not need to be in order, compare to the middle value, split the list in
order and repeat
d) Put the elements in order, compare with the middle value, split the list in order
and repeat.
a) 6,9,10,11
b) 6,8,9,10
c) 5,8,9,10
d) 5,8,10,11