Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sorting & Searching

Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

1. How many passes does an insertion sort algorithm consist of?

a) N
b) N-1
c) N+1
d) N2

2. What is the average case running time of an insertion sort algorithm?


a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)

3. Any algorithm that sorts by exchanging adjacent elements require O(N2) on


average.
a) True
b) False

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

14. Which of the following is correct with regard to insertion sort?


a) insertion sort is stable and it sorts In-place
b) insertion sort is unstable and it sorts In-place
c) insertion sort is stable and it does not sort In-place
d) insertion sort is unstable and it does not sort In-place

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)

17. Insertion sort is an example of an incremental algorithm.


a) True
b) False
18. Consider the code given below, which runs insertion sort:

Which condition will correctly implement the while loop?


a) (j > 0) || (arr[j − 1] > value)
b) (j > 0) && (arr[j − 1] > value)
c) (j > 0) && (arr[j + 1] > value)
d) (j > 0) && (arr[j + 1] < value)

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

23. Which of the following is not an exchange sort?


a) Bubble Sort
b) Quick Sort
c) Partition-exchange Sort
d) Insertion Sort

For Explanation go to:


From 1 to 13:

Insertion Sort Questions and Answers - Sanfoundry

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

For Explanation go to:


Insertion Sort MCQ - Objective Question Answer for Insertion Sort Quiz
1. What is an in-place sorting algorithm?
a) It needs O(1) or O(log n) memory to create auxiliary locations
b) The input is already sorted and in-place
c) It requires additional storage
d) It requires additional space

2. In the following scenarios, when will you use selection sort?


a) The input is already sorted
b) A large file has to be sorted
c) Large values need to be sorted with small keys
d) Small values need to be sorted with large keys

3. What is the worst case complexity of selection sort?


a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)

4. What is the advantage of selection sort over other sorting techniques?


a) It requires no additional storage space
b) It is scalable
c) It works best for inputs which are already sorted
d) It is faster than any other sorting technique

5. What is the average case complexity of selection sort?


a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)

6. What is the disadvantage of selection sort?


a) It requires auxiliary memory
b) It is not scalable
c) It can be used for small keys
d) It takes linear time to sort the elements
7. The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and
selection sort respectively are __________
a) 5 and 4
b) 4 and 5
c) 2 and 4
d) 2 and 5

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

9. What is the best case complexity of selection sort?


a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)

For Explanation go to:


Selection Sort Questions and Answers - Sanfoundry
1- Sort the following list using the Selection Sort algorithm.
H, V, A, T, L, N, K
What is the output of the algorithm after the third pass?
a) A, V, T, H, L, N, K
b) A, T, H, K, L, N, V
c) A, V, H, T, L, N, K
d) A, H, K, T, L, N, V
2- Which of the following statements is correct with respect to selection sort?
a) The list is divided into two lists, where the left list contains the unsorted
elements, and the right list contains the sorted elements.
b) The list is considered to be divided into three lists, where the left list
contains the unsorted elements, the right list contains the sorted elements and
the mid portion contains the key value which needs to be compared.
c) The list is not divided, but an extra array is required to store elements.
d) The list is divided into two lists, where the left list contains the sorted elements,
and the right list contains the unsorted elements.
3- How many swaps are required for the given array1 and array 2 using selection
sort respectively? Note: The sorted array should be in increasing order.
Array 1: 7 6 8 5 9 4 10 3
Array 2: 10 9 8 7 6 5 4 3
1. 7, 4
2. 7, 7
3. 7, 8
4. 7, 0
4- Consider array A has 5 elements A[ ]={ 30, 28, 12 , 24, 8}. How many
inversions are there in the array after the 2 passes using the selection sort
algorithm?
Hint: Inversion: Let array A[1...n] be an array of elements, If the two indexes I , j
of the array. If A[I]>A[j] then the pair I , j is known as inversion of the array.
a) 4
b) 3
c) 2
d) 1
5- The best case and worst case swaps in the selection sort respectively for n
elements?
a) zero, n-1
b) n-1, n
c) n, zero
d) n , n2
6- Which one of the following is the tightest upper bound that represents the
number of swaps required to sort n numbers using selection sort?
a) O(log n)
b) O(n)
c) O(n log n)
d) O(n2)
7- Which of the following sorting methods will be the best, if the number of
swapping done, is the only measure of efficiency?
a) Bubble sort
b) Quick sort
c) Insertion sort
d) Selection sort
8- Which one of the following in-place sorting algorithms needs the minimum
number of swaps?
a) Insertion sort
b) Quick sort
c) Heap sort
d) Selection sort
9- Which of the following input will give the worst-case time for selection sort to
sort an array in ascending order?
I. 1 2 3 4 5 6 7 8
II. 8 7 6 5 4 3 2 1
III. 8 7 5 6 3 2 1 4
a) I
b) II
c) II and III
d) I, II, and III
10- Consider the following array and what is the status of the array after the fifth
pass when we use the insertion sort?
Input: 4 3 2 10 12 1 5 6
Output: 1 2 3 4 5 6 10 12
After the first pass: 3 4 2 10 12 1 5 6
a) 1 2 3 4 10 12 5 6
b) 3 4 2 10 12 1 5 6
c) 2 3 4 10 12 1 5 6
d) 1 2 3 4 5 10 12 6
11- An array of 5 numbers has the following entries in order: 7 4 5 10 8. Prashant
uses selection sort to sort this array in descending order. What will the array
contain after two iterations of selection sort?
a) 10 8 7 5 4
b) 10 8 5 7 4
c) 8 10 5 7 4
d) None of these
12- From the given options, which of the following sorting algorithm has the
lowest-best case complexity?
I. Merge Sort
II. Quick sort
III. Selection sort
1. Only I
2. Only I and II
3. Only II and III
4. I, II and III
13- How many comparisons are required to sort an array of length 5 if the
straight selection sort is used and the array is already sorted in opposite order?
a) 1
b) 20
c) 10
d) 5
14- The _______ sort divides the array into sorted and unsorted sub-lists.
a) selection
b) bubble
c) Merge sort
d) all of the above

For Explanation go to:


Selection Sort MCQ Answer for Selection Sort Quiz
1. What is an external sorting algorithm?
a) Algorithm that uses tape or disk during the sort
b) Algorithm that uses main memory during the sort
c) Algorithm that involves swapping
d) Algorithm that are considered ‘in place’
Answer: a

2. What is an internal sorting algorithm?


a) Algorithm that uses tape or disk during the sort
b) Algorithm that uses main memory during the sort
c) Algorithm that involves swapping
d) Algorithm that are considered ‘in place’
Answer: b

3. What is the worst case complexity of bubble sort?


a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)
Answer: d

4. Select the appropriate code that performs bubble sort.


a)

for(int j=arr.length-1; j>=0; j--)


{
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
}
}
}
b)

for(int j=arr.length-1; j>=0; j--)


{
for(int k=0; k<j; k++)
{
if(arr[k] < arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
}
}
}

c)

for(int j=arr.length; j>=0; j--)


{
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
}
}
}

d)

for(int j=arr.length; j>=0; j--)


{
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+2])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
} } }
Answer: a
5. What is the average case complexity of bubble sort?
a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)
Answer: 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)

boolean swapped = false;


for(int j=arr.length-1; j>=0 && swapped; j--)
{
swapped = true;
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
swapped = false;
}
}
}
b)

boolean swapped = true;


for(int j=arr.length-1; j>=0 && swapped; j--)
{
swapped = false;
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
}
}
}

c)

boolean swapped = true;


for(int j=arr.length-1; j>=0 && swapped; j--)
{
swapped = false;
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
swapped = true;
}
}
}

d)

boolean swapped = true;


for(int j=arr.length-1; j>=0 && swapped; j--)
{
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
swapped = true;
}
}
}

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

For Explanation go to:


Bubble Sort Questions and Answers - Sanfoundry
1- Arrange the following items in ascending order using Bubble Sort. What is the
intermediate sequence of 37, 54, 21, 85, 68, 12, 9, and 57 after the second pass?
a) 21, 37, 12, 9, 54, 57, 68, 85
b) 37, 21, 54, 68, 12, 9, 57, 85
c) 21, 12, 9, 37, 54, 57, 68, 85
d) 21, 37, 54, 12, 9, 57, 68, 85
2- How many passes are required for sorting 8 elements list using bubble sort?
a) 0
b) 9
c) 8
d) 7
3- What will be the output list after completing first pass of bubble sort on input
array 32, 51, 27, 85, 66, 23, 13, 57?
a) 23, 13, 27, 33, 51, 57, 66, 85
b) 32, 51, 27, 66, 23, 13, 57, 85
c) 32, 27, 51, 66, 23, 13, 57, 85
d) 27, 33, 51, 23, 13, 57, 66, 85
4- Consider the following array and what is the status of the array after the
fourth pass when we use the bubble sort?
Array Elements: 20, 16, 12, 8, 4, 1
a) 16, 12, 8, 4, 1, 20
b) 12, 8, 4, 1, 16, 20
c) 8, 4, 1, 12, 16, 20
d) 4, 1, 8, 12, 16, 20
5- Consider the following array and what is the status of the array after the
third pass when we use the bubble sort?
Array Elements: 12, 95, 8, 78, 65, 59, 32, 1
a) 12, 8, 78, 65, 59, 32, 1, 95
b) 8, 12, 65, 59, 32, 1, 78, 95
c) 8, 12, 59, 32, 1, 65, 78, 95
d) 8, 12, 32, 1, 59, 65, 78, 95
6- The number of swapping needed to sort the numbers 8, 22, 7, 9, 31, 5, 13 in
ascending order, using bubble sort is:
a) 11
b) 12
c) 13
d) 10
7- How many passes does a Bubble sort algorithm require for sorting a given list
of 'n' items?
a) n 2
b) √n
c) n + 1
d) n – 1
8- What will be the output list after completing first pass of bubble sort on input
array 32, 51, 27, 85, 66, 23, 13, 57?
a) 23, 13, 27, 33, 51, 57, 66, 85
b) 32, 51, 27, 66, 23, 13, 57, 85
c) 32, 27, 51, 66, 23, 13, 57, 85
d) 27, 33, 51, 23, 13, 57, 66, 85
9- The given array is arr = {1, 2, 4, 3}. Bubble sort is used to sort the array
elements. How many passes will be done to sort the array?
a) 4 4 ‫ و ال‬3‫ھﻛﺗﺑﮭﺎ و اﺟﻲ اﺣﻠﮭﺎ ھﻼﻗﻲ ﻧﻔﺳﻲ ﻋﻣﻠت ﺳواب ﻣرة واﺣدة ﺑس ﺑﯾن ال‬

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

For Explanation go to:


Bubble Sort MCQ Answer for Bubble Sort
1. Which of the following sorting algorithms is the fastest?
a) Merge sort
b) Quick sort
c) Insertion sort
d) Shell sort
Answer: b
Explanation: Quick sort is the fastest known sorting algorithm because of its highly
optimized inner loop.

2. Quick sort follows Divide-and-Conquer strategy.


a) True
b) False
Answer: a

3. What is the worst case time complexity of a quick sort algorithm?


a) O(N)
b) O(N log N)
c) O(N2)
d) O(log N)
Answer: c

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.

7. What is the average running time of a quick sort algorithm?


a) O(N2)
b) O(N)
c) O(N log N)
d) O(log N)
Answer: c

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.

9. Quick sort uses join operation rather than merge operation.


a) true
b) false
Answer: a
Explanation: Quick sort uses join operation since join is a faster operation than merge.

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.

For Explanation go to:


Quicksort Questions and Answers - Sanfoundry
1. Quick sort is a __________
a) greedy algorithm
b) divide and conquer algorithm
c) dynamic programming algorithm
d) backtracking algorithm
Answer: b

2. What is the worst case time complexity of the Quick sort?


a) O(n log n)
b) O(n)
c) O(n3)
d) O(n2)
Answer: d

3. Apply Quick sort on a given sequence 7 11 14 6 9 4 3 12. What is the sequence


after first phase, pivot is first element?
a) 6 4 3 7 11 9 14 12
b) 6 3 4 7 9 14 11 12
c) 7 6 14 11 9 4 3 12
d) 7 6 4 3 9 14 11 12
Answer: b

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

5. Quick sort is a stable sorting algorithm.


a) True
b) False
Answer: b

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

10. Quick sort is a space-optimized version of ____


a) Bubble sort
b) Selection sort
c) Insertion sort
d) Binary tree sort
Answer: d

For Explanation go to:

Quicksort Interview Questions and Answers - Sanfoundry


1. Select the appropriate recursive call for Quicksort (arr is the array, low is the
starting index and high is the ending index of the array, partition returns the
pivot element, we will see the code for partition very soon)
a)

public static void quickSort(int[] arr, int low, int high)


{
int pivot;
if(high>low)
{
pivot = partition(arr, low, high);
quickSort(arr, low, pivot-1);
quickSort(arr, pivot+1, high);
}
}

b)

public static void quickSort(int[] arr, int low, int high)


{
int pivot;
if(high<low)
{
pivot = partition(arr, low, high);
quickSort(arr, low, pivot-1);
quickSort(arr, pivot+1, high);
}
}

c)

public static void quickSort(int[] arr, int low, int high)


{
int pivot;
if(high>low)
{
pivot = partition(arr, low, high);
quickSort(arr, low, pivot);
quickSort(arr, pivot, high);
}
}
d)

public static void quickSort(int[] arr, int low, int high)


{
int pivot;
if(high>low)
{
pivot = partition(arr, low, high);
quickSort(arr, low, pivot);
quickSort(arr, pivot+2, high);
}
}

Answer: a

2. What is the worst case complexity of Quicksort?


a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)
Answer: d

3. What is a randomized Quicksort?


a) The leftmost element is chosen as the pivot
b) The rightmost element is chosen as the pivot
c) Any element in the array is chosen as the pivot
d) A random number is generated which is used as the pivot
Answer: c

4. What is the best case complexity of Quicksort?


a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)
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

8. Which of the following is not true about Quicksort?


a) in-place algorithm
b) pivot position can be changed
c) adaptive sorting algorithm
d) can be implemented as a stable sort
Answer: b

9. Quick sort uses which of the following method to implement sorting?


a) merging
b) partitioning
c) selection
d) exchanging

Answer: b

For Explanation go to:

Quicksort Questions for Entrance Exams - Sanfoundry


Quicksort using Median of Three partitioning Questions and Answers -
Sanfoundry
1- Let P be a quicksort program to sort numbers in ascending order using the
first element as the pivot. Let t1 and t2 be the number of comparisons made by P
for the inputs [1 2 3 4 5] and [4 1 5 3 2] respectively. Which one of the following
holds?
a) t1 = 5
b) t1 < t2
c) t1 > t2
d) t1 = t2
2- An array of 25 distinct elements is to be sorted using quick sort. Assume that
the pivot element is chosen uniformly at random. The probability that the pivot
element gets placed in the worst possible location in the first round of
partitioning (rounded off to 2 decimal places) is _________.
a) 0.08
b) 0.02
c) 0.04
d) 0.06
3- Assume that the algorithms considered here sort the input sequences in
ascending order. If the input is already in ascending order, which of the following
are TRUE?
I. Quicksort runs in Θ (n2) time.
II. Bubble sort runs in Θ (n2) time.
III. Merge sort runs in Θ (n) time.
IV. Insertion sort runs in Θ (n) time.
a) I and II only
b) I and III only
c) II and IV only
d) I and IV only
4- Which one of the following is the recurrence equation for the worst case time
complexity of the Quicksort algorithm for sorting (𝑛 ≥ 2) numbers? In the
recurrence equations given in the options below, 𝑐 is a constant.
a) T(𝑛) = 2 T(𝑛/2) + 𝑐n
b) T(𝑛) = T(𝑛 – 1) + T(1) + 𝑐n
c) T(𝑛) = 2T(𝑛 – 1) + 𝑐n
d) T(𝑛) = T(𝑛/2) + 𝑐n
5- Randomized quicksort is an extension of quicksort where the pivot is chosen
randomly. What is the worst case complexity of sorting n number using
randomized quicksort?
a) O(n)
b) O(n log n)
c) O(n2)
d) O(n!)
6- You have an array of n elements. Suppose you implement quick sort by always
choosing the central element of the array as the pivot. Then the tightest upper
bound for the worst-case performance is ……………….
a) 0(n2)
b) 0(n long n)
c) 0(n log n)
d) 0(n3)
7- A machine needs a minimum of 100 sec to sort 1000 names by quick sort. The
minimum time needed to sort 100 names will be approximately ………………….
a) 50.2 sec
b) 6.7 sec
c) 72.7 sec
d) 11.2 sec
8- What is recurrence for worst case of Quicksort and what is the time
complexity in Worst case?
a) Recurrence is T(n) = T(n-2) + O(n) and time complexity is O(n2)
b) Recurrence is T(n) = T(n-1) + O(n) and time complexity is O(n2)
c) Recurrence is T(n) = 2T(n/2) + O(n) and time complexity is O(n Log n)
d) Recurrence is T(n) = T(n/10) + T(9n/10) + O(n) and time complexity is O(n Log n)
9- Suppose we are sorting an array of eight integers using
quicksort, and we have just finished the first partitioning with the
array looking like this:
2 5 1 7 9 12 11 10
Which statement is correct?
a) The pivot could be either the 7 or the 9.
b) The pivot could be the 7, but it is not the 9
c) The pivot is not the 7, but it could be the 9
d) Neither the 7 nor the 9 is the pivot.

For Explanation go to:

Quick Sort MCQ


Top MCQs on QuickSort Algorithm with Answers - GeeksforGeeks
1. Merge sort uses which of the following technique to implement sorting?
a) backtracking
b) greedy algorithm
c) divide and conquer
d) dynamic programming
Answer: c

2. What is the average case time complexity of merge sort?


a) O(n log n)
b) O(n2)
c) O(n2 log n)
d) O(n log n2)
Answer: a
Explanation: The recurrence relation for merge sort is given by T(n) = 2T(n/2) + n. It is
found to be equal to O(n log n) using the master theorem.

3. What is the auxiliary space complexity of merge sort?


a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: c
Explanation: An additional space of O(n) is required in order to merge two sorted
arrays. Thus merge sort is not an in place sorting algorithm.
4. Merge sort can be implemented using O(1) auxiliary space.
a) true
b) false
Answer: a
Explanation: Standard merge sort requires O(n) space to merge two sorted arrays. We
can optimize this merging process so that it takes only constant space. This version is
known as in place merge sort.

5. What is the worst case time complexity of merge sort?


a) O(n log n)
b) O(n2)
c) O(n2 log n)
d) O(n log n2)
Answer: a
6. Which of the following method is used for sorting in merge sort?
a) merging
b) partitioning
c) selection
d) exchanging
Answer: a

7. What will be the best case time complexity of merge sort?


a) O(n log n)
b) O(n2)
c) O(n2 log n)
d) O(n log n2)
Answer: a

8. Which of the following is not a variant of merge sort?


a) in-place merge sort
b) bottom up merge sort
c) top down merge sort
d) linear merge sort
Answer: d

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

10. Which of the following is not in place sorting algorithm by default?


a) merge sort
b) quick sort
c) heap sort
d) insertion sort
Answer: a

11. Which of the following is not a stable sorting algorithm?


a) Quick sort
b) Cocktail sort
c) Bubble sort
d) Merge sort
Answer: a
12. Which of the following stable sorting algorithm takes the least time when
applied to an almost sorted array?
a) Quick sort
b) Insertion sort
c) Selection sort
d) Merge sort
Answer: d

13. Merge sort is preferred for arrays over linked lists.


a) true
b) false
Answer: b
Explanation: Merge sort is preferred for linked list over arrays. It is because in a linked
list the insert operation takes only O(1) time and space which implies that we can
implement merge operation in constant time.

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.

15. Choose the correct code for merge sort.


a)

void merge_sort(int arr[], int left, int right)


{
if (left > right)
{

int mid = (right-left)/2;


merge_sort(arr, left, mid);
merge_sort(arr, mid+1, right);

merge(arr, left, mid, right); //function to merge sorted arrays


}
}
b)

void merge_sort(int arr[], int left, int right)


{
if (left < right)
{

int mid = left+(right-left)/2;


merge_sort(arr, left, mid);
merge_sort(arr, mid+1, right);

merge(arr, left, mid, right); //function to merge sorted arrays


}
}

c)

void merge_sort(int arr[], int left, int right)


{
if (left < right)
{

int mid = left+(right-left)/2;


merge(arr, left, mid, right); //function to merge sorted arrays
merge_sort(arr, left, mid);
merge_sort(arr, mid+1, right);

}
}

d)

void merge_sort(int arr[], int left, int right)


{
if (left < right)
{

int mid = (right-left)/2;


merge(arr, left, mid, right); //function to merge sorted arrays
merge_sort(arr, left, mid);
merge_sort(arr, mid+1, right);
} }
Answer: b

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

For Explanation go to:

Merge Sort Questions and Answers - Sanfoundry


1- Which Sorting algorithm is based on divide and conquer method:
a) Merge Sort
b) Quick Sort
c) Bubble Sort
d) Both A and B
2- Which of the following sort algorithms has execution time that is least
dependent on initial ordering of the input?
a) Insertion sort
b) Quick sort
c) Merge sort
d) Selection sort
3- ________ sorting algorithms has the lowest worst-case complexity.
a) Selection Sort
b) Bubble Sort
c) Merge Sort
d) Quick Sort
4- Which of the following is the worst-case time complexity of the merge-sort
algorithm?
1. Θ(n log n)
2. Θ(n1.5 log n)
3. Θ(n2)
4. Θ(n)
5- Two-way merge sort algorithm is used to sort the following elements in
ascending order. 200, 470, 150, 80, 90, 40, 400, 300, 120, 70.
What is the order of these elements after the second pass of the merge sort
algorithm?
a) 40, 80, 90, 150, 200, 300, 400, 470, 70, 120
b) 80, 150, 200, 470, 40, 90, 300, 400, 70, 120
c) 40, 70, 80, 90, 120, 150, 200, 300, 400, 470
d) 200, 470, 80, 150, 40, 90, 300, 400, 70, 120
6- Suppose P, Q, R, S, T are sorted sequences having lengths 20, 24, 30, 35, 50
respectively. They are to be merged into a single sequence by merging two
sequences at a time. The number of comparisons that will be needed in the
worst case by the optimal algorithm for doing this is ____.
a) 44
b) 150
c) 358
d) 315
7- Assume that a merge sort algorithm in the worst case takes 30 seconds for an
input of size 64.
Which of the following most closely approximates the maximum input size of a
problem that can be solved in 6 minutes?
a) 256
b) 512
c) 1024
d) 2048
8- What is mean by stable sorting algorithm?
1. A sorting algorithm is stable if it doesn’t preserve the order of duplicate keys
2. A sorting algorithm is stable if it preserves the order of duplicate keys
3. A sorting algorithm is stable if it preserves the order of all keys
4. A sorting algorithm is stable if it preserves the order of non-duplicate keys
9- The worst-case running times of Insertion sort, Merge sort and Quick sort,
respectively, are:
1. Θ (n log n), Θ (n log n), and Θ (n2)
2. Θ (n2), Θ (n2), and Θ (n log n)
3. Θ (n2), Θ (n log n), and Θ (n log n)
4. Θ (n2), Θ (n log n), and Θ (n2)
10- A list of n strings, each of length n, is sorted into lexicographic order using
merge - sort algorithm. The worst case running time of this computation is:
a) O(n log n)
b) O(n2 log n)
c) O(n2 + log n)
d) O(n3)
11- If one uses straight two-way merge sort algorithm to sort the following
elements in ascending order 20, 47, 15, 8, 9, 4, 40, 30, 12, 17 then the order of
these elements after the second pass of the algorithm is:
a) 8, 9, 15, 20, 47, 4, 12, 17, 30, 40
b) 8, 15, 20, 47, 4, 9, 30, 40, 12, 17
c) 15, 20, 47, 4, 8, 9, 12, 30, 40, 17
d) 4, 8, 9, 15, 20, 47, 12, 17, 30, 40
12- Which of the following sorting algorithms uses recursion?
a) Heap sort
b) Bubble sort
c) Merge sort
d) Insertion sort
13- Under what condition will a merge-sort execute faster?
a) If the data is already sorted in ascending order
b) If the data is already sorted in descending order
c) It will always take the same amount of time to execute
14- Which of the following is true about merge sort?
a) Merge Sort works better than quick sort if data is accessed from slow sequential
memory.
b) Merge Sort is stable sort by nature
c) Merge sort outperforms heap sort in most of the practical situations.
d) All of the above.
15- Given two sorted list of size m and n respectively. The number of
comparisons needed the worst case by the merge sort algorithm will
be ……………………
a) m x n
b) maximum of m and n
c) minimum of m and n
d) m + n – 1
16- Consider a situation where swap operation is very costly. Which of
the following sorting algorithms should be preferred so that the
number of swap operations are minimized in general?
a) Heap Sort
b) Selection Sort
c) Insertion Sort
d) Merge Sort
For Explanation go to:

Merge Sort MCQ Answer for Merge Sort Quiz


13.6. Merge Sort — AP CSA Java Review
Top MCQs on MergeSort Algorithm with Answers - GeeksforGeeks
1. What is recurrence for worst case of QuickSort and what is the
time complexity in Worst case?
A. Recurrence is T(n) = T(n-2) + O(n) and time complexity is O(n2)
B. Recurrence is T(n) = T(n-1) + O(n) and time complexity is O(n2)
C. Recurrence is T(n) = 2T(n/2) + O(n) and time complexity is O(n Log n)
D. Recurrence is T(n) = T(n/10) + T(9n/10) + O(n) and time complexity is
O(n Log n)
2. Which of the following is not a stable sorting algorithm?
A. Insertion sort
B. Selection sort
C. Bubble sort
D. Merge sort

3. What is an external sorting algorithm?


A. Algorithm that uses tape or disk during the sort
B. Algorithm that uses main memory during the sort
C. Algorithm that involves swapping
D. Algorithm that are considered ‘in place’

4. If the number of records to be sorted is small, then …… sorting


can be efficient.
A. Merge
B. Heap
C. Selection
D. Bubble

5. Suppose we have an O(n) time algorithm that finds median of an


unsorted array. Now consider a Quicksort implementation where
we first find median using the above algorithm, then use median as
pivot. What will be the worst-case time complexity of this modified
Quicksort.
A. O(n2 Log n)
B. O(n2)
C. O(n Log n Log n)
D. O(n Log n)
6. Which of the following is not an in-place sorting algorithm?
A. Selection sort
B. Heap sort
C. Quick sort
D. Merge sort

7. What is the advantage of bubble sort over other sorting


techniques?
A. It is faster
B. Consumes less memory
C. Detects whether the input is already sorted
D. All of the mentioned

8. The complexity of sorting algorithm measures the …… as a


function of the number n of items to be sorter.
A. average time
B. running time
C. average-case complexity
D. case-complexity

9. Suppose we are sorting an array of eight integers using quicksort,


and we have just finished the first partitioning with the array
looking like this:
2 5 1 7 9 12 11 10
Which statement is correct?
A. The pivot could be either the 7 or the 9.
B. The pivot could be the 7, but it is not the 9
C. The pivot is not the 7, but it could be the 9
D. Neither the 7 nor the 9 is the pivot.

10.Consider the situation in which assignment operation is very


costly. Which of the following sorting algorithm should be
performed so that the number of assignment operations is
minimized in general?
A. Insertion sort
B. Selection sort
C. Heap sort
D. None
11. In the following scenarios, when will you use selection sort?
A. The input is already sorted
B. A large file has to be sorted
C. Large values need to be sorted with small keys
D. Small values need to be sorted with large keys

12. What is the worst-case complexity of selection sort?


A. O(n log n)
B. O(log n)
C. O(n)
D. O(n2)

13. What is the advantage of selection sort over other sorting


techniques?
a) It requires no additional storage space
b) It is scalable
c) It works best for inputs which are already sorted
d) It is faster than any other sorting technique

14. What is the average case complexity of selection sort?


A. O(n log n)
B. O(log n)
C. O(n)
D. O(n2)

15. What is the disadvantage of selection sort?


A. It requires auxiliary memory
B. It is not scalable
C. It can be used for small keys
D. None of the mentioned

16. The given array is arr = {3,4,5,2,1}. The number of iterations in


bubble sort and selection sort respectively are,
A. 5 and 4
B. 4 and 5
C. 2 and 4
D. 2 and 5
17. 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

18. What is the best-case complexity of selection sort?


A. O(n log n)
B. O(log n)
C. O(n)
D. O(n2)

19. What is an internal sorting algorithm?


A. Algorithm that uses tape or disk during the sort
B. Algorithm that uses main memory during the sort
C. Algorithm that involves swapping
D. Algorithm that are considered ‘in-place’

20. What is the worst-case complexity of bubble sort?


A. O(n log n)
B. O(log n)
C. O(n)
D. O(n2)

21. What is the average case complexity of bubble sort?


A. O(n log n)
B. O(log n)
C. O(n)
D. O(n2)

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)

24. Quicksort can be categorized into which of the following?


A. Brute Force technique
B. Divide and conquer
C. Greedy algorithm
D. Dynamic programming.

25. What is a randomized Quicksort?


A. The leftmost element is chosen as the pivot
B. The rightmost element is chosen as the pivot
C. Any element in the array is chosen as the pivot
D. A random number is generated which is used as the pivot

For Explanation go to:


Sorting Data Structure & Algorithm MCQ - Letsfindcourse
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

2. What is the best case for linear search?


a) O (n log n)
b) O (log n)
c) O (n)
d) O (1)

3. What is the worst case for linear search?


a) O(n log n)
b) O(log n)
c) O(n)
d) O(1)

4. Which of the following is a disadvantage of linear search?


a) Requires more space
b) Greater time complexities compared to other searching algorithms
c) Not easy to understand
d) Not easy to implement

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?

I. linear search is also called random search

II. At worst case, the number of comparisons needed in linear search is N

a) Only I

b) Only II

c) Both I and II

d) None of these

7. Which of the following is the time complexity to search an element in the


linked list?

a) O(1)

b) O(n)

c) O(log n)

d) O(n log n)

8. The worst-case occur in linear search algorithm when …….


a) Item is somewhere in the middle of the array
b) Item is not in the array at all
c) Item is the last element in the array
d) Item is the last element in the array or item is not there at all

9. Which of the following is not a limitation of binary search algorithm?


a) must use a sorted array
b) requirement of sorted array is expensive when a lot of insertion and deletions
are needed
c) there must be a mechanism to access middle element directly
d) binary search algorithm is not efficient when the data elements more than 1500.

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.

13. 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

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)

18. 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

19. Binary Search can be categorized into which of the following?


a) Brute Force technique
b) Divide and conquer
c) Greedy algorithm
d) Dynamic programming

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

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


a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)
23. Choose true statement:

I - Binary search is faster than linear search.

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

24. The minimum number of comparisons for a particular record among 32


sorted records through binary search method will be:

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……………..

a) Put the elements in order, check each item in turn

b) Elements do not need to be in order, compare to the middle value, split the list in
order and repeat

c) Elements do not need to be in order, check each item in turn.

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…………………

a) Put the elements in order, check each item in turn.

b) Elements do not need to be in order, compare to the middle value, split the list in
order and repeat

c) Elements do not need to be in order, check each item in turn.

d) Put the elements in order, compare with the middle value, split the list in order
and repeat.

29. When searching for the value 34 in the sorted list


[3,5,7,8,10,11,13,15,20,25,30,33] using binary search we check the following
element indexes:

a) 6,9,10,11

b) 6,8,9,10

c) 5,8,9,10

d) 5,8,10,11

You might also like