Question Bank on Sorting Algorithms
Question Bank on Sorting Algorithms
1. Give names of two algorithms with reasons that contradict the statement: “A list in
increasing order is the best case for all sorting algorithms”. Justify the contradiction for
the given array A = [1,2,3,4,5,6].
Arrange these algorithms in the increasing order of time taken to perform the desired
sorting. Mention the time complexity of each one and give the reasoning for it.
3. Assuming that all elements are distinct, what are the possible locations for the smallest
and largest element in a min-heap? Give an example to show that a min-heap may not be
a sorted array but a sorted array is always a min-heap.
4. Which of the following sorting algorithm will take least time when all elements of input array are
identical? Justify your answer.
5. You are given n numbers each of which is either 0 or 1. Give an O(n) algorithm to sort
these numbers and justify the time complexity.
6. What is the significance of having the input data uniformly distributed for applying
bucket sort? What happens if this does not hold?
7. Derive the time complexity of insertion sort for best, average and worst-case input.
8. Illustrate the operation of the build max heap procedure on the following array
A = [20,21,15,25,35,12,26,17,6,8,30]
9. Let A be an array with elements [3,0,9,2,7,5,8,1,10,6]. Randomised-Select is used to
select the maximum element from this array. Describe a sequence of partitions that would
result in the worst-case performance of the algorithm.
10. With the help of example explain why the best case of Mergesort is always the worst case
for Quicksort. Discuss the scenario for worst case of Mergesort. Write the recurrence
relation of the best case of Quicksort and Mergesort, explaining the difference between
them.
11. Consider the following algorithm of sequential search that searches an element k in an
array of n elements.
Discuss and find the time complexity of this algorithm for the average and worst case.
12. Consider a sorted array A[1…n] having all distinct elements and n>=3. How much time
quicksort will take to sort this array and why? Suppose instead of normal quicksort, a
modified quicksort algorithm is used where pivot is the median of set of 3 elements [First
element, middle element, and last element]. Derive the running time of this modified
quicksort algorithm for sorting array A. Which algorithm will be more suitable for sorting
array A? If the algorithm is further modified such that Partition function splits the array
A into two sub-lists each of which contains at least one-fifth of the elements then what
will be recurrence relation for running time?
In order to sort A[1 : n], we recursively sort A[1 : n-1] and then insert A[n] into the
sorted array A[1 : n-1]. Write a recurrence for the running time of this algorithm
14.
14. What are the minimum and maximum number of elements in a binary heap of height h?
15. Which of the following algorithms are stable: Insertion sort, Quicksort. Justify with the help of an
example.
16. “Counting sort is a comparison sort algorithm.” Yes or No. Justify your answer using the Input
A= < 4, 3, 2, 4, 1, 5, 2, 4, 3 >
17. A priority queue can be implemented in two different ways using min-heap and using singly
linked list in which elements are stored in sorted order (Smaller values indicates higher priority).
Compare the time complexity of following operations when performed on two different
implementations of priority queue.
18. “A list in increasing order is the best case for all sorting algorithms”. Comment and justify your
answer.
19. Write an efficient algorithm to find maximum element in the following min heap:
12 23 45 26 28 50 96 35 66 98 45 55 91
Run the algorithm for this data. Give the exact running time of algorithm.
20. Given an array of N integers ranging from 0 to 165-1. The numbers are represented using
hexadecimal representation. Write a most time and space efficient algorithm to sort these
numbers.
21. A priority queue is implemented in two ways – (i) using max heap and (ii) using array sorted in
decreasing order of key values. Compare the time complexity of both implementations when
priority of a certain element is increased.
24. Give an efficient algorithm to find maximum element in min heap. Give the exact running time of
algorithm.
25. Write an algorithm that inputs n integers in the range 0 to k and answers any query about how
many integers out of n falls into a range [a..b]. Running time of your algorithm should be θ (n+k)
26. Sort the following array in descending order using heap sort:
14 9 16 12 19 15 7 2
27. Find the fifth smallest element in the array [2,5,7,16,10,1,6,8,12,4,9,18,17,0,3,15,11,14] using
RANDOMISED_SELECT algoruthm
28. A priority queue is implemented in two ways – (i) using max heap and (ii) using array sorted in
decreasing order of key values. Compare the time complexity of both implementations when priority
of certain element is increased.
29. Discuss the best and worst case for insertion sort algorithm. Find the time complexity in each case.
30. Discuss the best and worst case for mergesort algorithm. Find the time complexity in each case.
31. Show that at most 3*Floor(n/2) comparisons are sufficient to find maximum and minimum
simultaneously in a given array of size n.
3
32. Consider the following recursive function:
3
TOH(in n, char from, char to,.char aux) .
{
if(n==I)
Cout<<"Move disk I from "<<from<<" to "<<to;
else
{
TOH (n-1, from,aux,to );
cout<<"Move disk"<<n<<" from"<<from<<"to"<<to;
TOH(n-1 ,aux,to,from);
}
}
Let T(n) represents the total number of moves required to move n disks from one tower to another.
Give a recurrence relation for T(n). Find T(n) in terms of big O notation.
33. A class of 50 students was given a test of maximum 100 marks. The marks obtained by
students were recorded roll number wise. Now the teacher wants to sort the list in the
increasing order of marks obtained. The students who got same marks will be sorted on the
basis of their roll number. Design a linear time algorithm for this sorting.
5
34. Which algorithm suits best to sort an array in decreasing order if the array contains distinct
elements arranged in increasing order. Justify your answer.
35. An algorithm divides a problem of n elements into four equal parts and solves only
three sub-problems. The cost of dividing into sub-problems and combining their
solutions is constant. Give the recurrence relation for the running time of this algorithm
36. A list of n nos. is nearly sorted in decreasing order. This list needs to be sorted in
increasing order. Which algorithm should be used and why?
2
37. Give a comparison tree to sort three distinct values a, b and c. Discuss the cases
corresponding to worst case performance and best case performance. What is the
average no. of comparisons performed assuming that any permutation of a, b and c is
possible.
38 Give an O(n lg k) time algorithm to merge k sorted lists into one sorted list where n is
the total number of elements in all the input list.
5
39. Discuss the two ways in which a heap data structure can be implemented.
2
40. Derive an expression for the running time of quicksort when it is applied on an array
of n distinct elements arranged in decreasing order. The quicksort has been modified in
such a way that everytime first element is chosen as pivot.
41. How many times partition is called in quicksort? Find the running time of partition?
43. Show the steps to convert the following array is a min priority queue :
14 9 18 12 19 15 7 6
Now add a new node with key 17
44. In counting sort, the last for loop starts from length[a] and goes till 1. If it is rewritten as
For j → 1 to length[a]
then still it works properly. Then why the loop is started from length[a]?
.