Sorting and Divide and Conquer · Programming, Data Structures and Algorithms
5 questions · asked in 3 of 3 years (2024–2026)
MCQ 2 · MSQ 2 · NAT 11-mark 3 · 2-mark 2
2024Questions per year2026
Questions 1–5 of 5
Consider that the quick sort algorithm is used to sort an array of distinct randomly ordered elements. In every call, the pivot is chosen as the first element of the current subarray.
Let denote the expected time to sort the array. Assume that the time to partition is linear in the size of the current subarray.
Which of the following recurrence relations correctly represents in this scenario?
Let denote the expected time to sort the array. Assume that the time to partition is linear in the size of the current subarray.
Which of the following recurrence relations correctly represents in this scenario?
Answer: (D)
The pivot is the first element and the input is randomly ordered, so the pivot is equally likely to have any rank in the current subarray of size .
If the pivot lands at position , the left part has size and the right part has size . Each has probability . Partitioning costs . Averaging over all splits gives the expected-case recurrence:
The other options each fix one split:
- (A) is the worst case, where the pivot is always an extreme element.
- (B) is a split in a fixed 1:3 ratio at every step. Here the split sizes vary over all values, so it is not the average.
- (C) is the ideal case, where the pivot is always the median.
Answer: (D)
If the pivot lands at position , the left part has size and the right part has size . Each has probability . Partitioning costs . Averaging over all splits gives the expected-case recurrence:
The other options each fix one split:
- (A) is the worst case, where the pivot is always an extreme element.
- (B) is a split in a fixed 1:3 ratio at every step. Here the split sizes vary over all values, so it is not the average.
- (C) is the ideal case, where the pivot is always the median.
Answer: (D)
Consider the problem of sorting the given array in ascending order:
Consider two sorting algorithms Bubble Sort and Insertion Sort .
Let be the total number of comparisons done by on the elements of and be the total number of comparisons done by on the elements of .
Which of the following options is/are correct?
Consider two sorting algorithms Bubble Sort and Insertion Sort .
Let be the total number of comparisons done by on the elements of and be the total number of comparisons done by on the elements of .
Which of the following options is/are correct?
One or more options are correct.
Answer: (B), (C), (D)
Only one pair, (5,4), is out of order, so one swap sorts the array. Both algorithms still compare pairs that are already in order.
Bubble sort (checks every adjacent pair in a pass and stops after a pass with no swap): Pass 1 compares (1,2), (2,3), (3,5), (5,4), which is comparisons, and swaps only (5,4). Pass 2 compares (1,2), (2,3), (3,4), which is comparisons with no swap, so it stops. . If the early stop is not used, passes 3 and 4 add more comparisons and .
Insertion sort: inserting 2, 3 and 5 takes comparison each, because each is larger than the element before it. Inserting 4 takes comparisons: it is smaller than 5, so 5 shifts right (one swap), then 4 is compared with 3 and stops. .
(A) is false, because , not .
(B) is true, because in both versions of bubble sort.
(C) is true: the only out-of-order pair is fixed by one swap.
(D) is true: bubble sort compares (1,2), (2,3), (3,5), and insertion sort compares 2 with 1, 3 with 2, 5 with 3. In every one of these pairs the elements are already in order.
Answer: (B), (C), (D)
Bubble sort (checks every adjacent pair in a pass and stops after a pass with no swap): Pass 1 compares (1,2), (2,3), (3,5), (5,4), which is comparisons, and swaps only (5,4). Pass 2 compares (1,2), (2,3), (3,4), which is comparisons with no swap, so it stops. . If the early stop is not used, passes 3 and 4 add more comparisons and .
Insertion sort: inserting 2, 3 and 5 takes comparison each, because each is larger than the element before it. Inserting 4 takes comparisons: it is smaller than 5, so 5 shifts right (one swap), then 4 is compared with 3 and stops. .
(A) is false, because , not .
(B) is true, because in both versions of bubble sort.
(C) is true: the only out-of-order pair is fixed by one swap.
(D) is true: bubble sort compares (1,2), (2,3), (3,5), and insertion sort compares 2 with 1, 3 with 2, 5 with 3. In every one of these pairs the elements are already in order.
Answer: (B), (C), (D)
Suppose that insertion sort is applied to the array and it takes exactly two swaps to sort the array. Select all possible values of .
One or more options are correct.
Answer: (A), (C)
Run insertion sort on and count the swaps for each possible :
Case : Insert into . It makes swap (it moves past ). Insert : no swap. Insert : it makes swap (it moves past ). Total swaps: . Matches.
Case : Insert : no swap. Insert : no swap. Insert : it makes swap (it moves past and stops at ). Total swaps: only . Doesn't match.
Case : Insert : no swap. Insert : no swap. Insert : it makes swaps (it moves past , then past ). Total swaps: . Matches.
Case : Insert : no swap. Insert : it makes swap (it moves past ). Insert : it makes swaps (it moves past , then past ). Total swaps: . Doesn't match.
Answer: x=10 and x=14
Case : Insert into . It makes swap (it moves past ). Insert : no swap. Insert : it makes swap (it moves past ). Total swaps: . Matches.
Case : Insert : no swap. Insert : no swap. Insert : it makes swap (it moves past and stops at ). Total swaps: only . Doesn't match.
Case : Insert : no swap. Insert : no swap. Insert : it makes swaps (it moves past , then past ). Total swaps: . Matches.
Case : Insert : no swap. Insert : it makes swap (it moves past ). Insert : it makes swaps (it moves past , then past ). Total swaps: . Doesn't match.
Answer: x=10 and x=14
Consider sorting the following array of integers in ascending order using an in-place Quicksort algorithm that uses the last element as the pivot.
The minimum number of swaps performed during this Quicksort is ____
The minimum number of swaps performed during this Quicksort is ____
Answer: 0
With the last element as pivot, each partition moves the elements smaller than the pivot to its left and the larger ones to its right. A swap is needed only when an element is on the wrong side of the pivot.
Pivot is the largest element, so are already on its left and is already in its final place. The right part is empty. No swap is needed.
Next, has pivot , again the largest, so again no swap. Then has pivot and has pivot , each with no swap. has one element and needs nothing.
So every partition finds nothing out of place, and the number of swaps is . A swap of an element with itself changes nothing, so it is not counted. The count cannot go below .
Answer: 0
Pivot is the largest element, so are already on its left and is already in its final place. The right part is empty. No swap is needed.
Next, has pivot , again the largest, so again no swap. Then has pivot and has pivot , each with no swap. has one element and needs nothing.
So every partition finds nothing out of place, and the number of swaps is . A swap of an element with itself changes nothing, so it is not counted. The count cannot go below .
Answer: 0
Consider the following sorting algorithms:
(i) Bubble sort
(ii) Insertion sort
(iii) Selection sort
Which ONE among the following choices of sorting algorithms sorts the numbers in the array in increasing order after exactly two passes over the array?
(i) Bubble sort
(ii) Insertion sort
(iii) Selection sort
Which ONE among the following choices of sorting algorithms sorts the numbers in the array in increasing order after exactly two passes over the array?
Answer: (B)
Trace for two passes under each algorithm.
Bubble sort swaps adjacent pairs that are out of order. Pass 1: . Pass 2: . Not sorted, since 2 and 1 are still out of order.
Insertion sort inserts the next element into the sorted prefix. Pass 1 inserts 3: . Pass 2 inserts 2: . Not sorted, since 1 has not been placed yet.
Selection sort puts the smallest remaining element in the next position. Pass 1 swaps 1 with 4: . Pass 2 swaps 2 with 3: . Sorted.
Only selection sort, (iii), is sorted after exactly two passes.
Answer: (B)
Bubble sort swaps adjacent pairs that are out of order. Pass 1: . Pass 2: . Not sorted, since 2 and 1 are still out of order.
Insertion sort inserts the next element into the sorted prefix. Pass 1 inserts 3: . Pass 2 inserts 2: . Not sorted, since 1 has not been placed yet.
Selection sort puts the smallest remaining element in the next position. Pass 1 swaps 1 with 4: . Pass 2 swaps 2 with 3: . Sorted.
Only selection sort, (iii), is sorted after exactly two passes.
Answer: (B)
Keep practising
Topics in Programming, Data Structures and Algorithms
Programming in PythonStacks, Queues and Linked ListsTreesHash TablesLinear and Binary Search
Sorting and Divide and ConquerYou are here
Graph Theory and Graph Algorithms