Sorting · Algorithms
24 questions · asked in 15 of 27 years (2000–2026)
MCQ 22 · NAT 21-mark 17 · 2-mark 7
Questions 21–24 of 24
Answer: (C)
A binary tree of height has at most leaves, so and . The height is the number of comparisons in the worst case.
Also (the largest factors of are each at least ), so .
So every comparison sort needs comparisons in the worst case. Merge sort and heap sort reach , so this bound is tight. (A) is too small, is not tight since is achievable, and is larger than needed.
Answer: (C)
What would be the worst case time complexity of the insertion Sort algorithm, if the inputs are restricted to permutations of 1....n with at most n inversions?
Answer: (D)
First, see how the question defines an Inversion.
In a permutation of n distinct integers, an inversion is a pair where and .
- Note the difference between a swap and an inversion.
- Swapping is done by the programmer, so it is an explicit step. An Inversion is already present in the input, so it is implicit.
Ex :- Take the input =>
The inversions here are: , , , , , , and . So an inversion belongs to the input. It is not an operation (swap) done by us.
The time complexity of Insertion Sort is , where is the total number of inversions.
Ex :- Take the input =>
How many comparisons are needed to put in its right place?
- First, is compared with . The result is True, as it is smaller than .
- Next, it is compared with , and again the result is True.
- The same happens with . All of them give True.
So comparisons were inversions, and more comparison is needed, where the while loop fails.
To put in its right place, comparisons are needed.
So the total comparisons in Insertion Sort are: number of elements where the while loop fails + total number of inversions in the input
- Elements where the while loop fails: Take the input . The first element stays as it is. Each other element has one failed while-loop comparison. So the total is comparisons.
- Total number of inversions in the input: Best Case : and Worst Case :
Total time complexity of insertion sort:
The question says there are at most inversions, so the time complexity is :- .
Correct Answer:
Answer: (A)
Binary search finds the position with about comparisons, instead of up to for linear search.
But in an array, making room means shifting the larger elements one place to the right. In the worst case (reverse sorted input) all elements move, whatever way the position was found.
Worst case total: .
The shifting dominates, so the worst case stays .
Answer: (A)
Answer: (C)
- If the random pivot is the smallest or largest element every time, each partition splits n elements into 0 and n 1. Then , which is . The chance of this is . It is very small but not zero.
- If all elements are equal and the usual partition is used, every split is also 0 and n 1, whatever pivot is chosen.
So the worst case is . O(n!) is also a valid upper bound, since for , but it is not tight. O(n) and O(n log n) are too small.
Answer: (C)
Keep practising