What this topic covers
18 subtopics- Binary search7
- Library sorts6
- Binary search variants4
- Merge sort4
- Insertion sort3
- Quick sort3
- Stability3
- Binary search on the answer2
- Bubble sort2
- Non-comparison sorts2
- Selection2
- External sorting1
Show all 18 subtopics
- Heap sort1
- Lower bounds1
- Searching1
- Selection sort1
- Shell sort1
- Space1
- Beginner 15
- Intermediate 15
- Advanced 15
About this topic
Sorting questions in GATE and placement tests are mostly traces: what does the array look like after one pass, or after partitioning? Learn what each algorithm guarantees after k passes – bubble sort fixes the largest k elements at the end, selection sort the smallest k at the front, insertion sort keeps the first k + 1 sorted. Binary search questions test the boundaries: lo <= hi or lo < hi, mid + 1 or mid. All traces here were computed by running the algorithm.
Free preview
Sample questions
Try each one first, then open the answer for the explanation and why the other options are wrong.
What does the array look like after the first pass of bubble sort (ascending)?
- 05
- 11
- 24
- 32
- 48
- 1
- 4
- 2
- 5
- 8
- 1
- 5
- 4
- 2
- 8
- 1
- 2
- 4
- 5
- 8
- 5
- 1
- 4
- 2
- 8
Show answer and explanation
Explanation
One pass compares neighbours left to right and swaps when out of order: (5,1) swap, (5,4) swap, (5,2) swap, (5,8) no swap. The largest element "bubbles" to the end.
Why the other options are wrong
- BOnly the first swap is applied.
- CThat is fully sorted – it needs another pass.
- DThe pass makes three swaps.
Binary search for 23 uses mid = (lo + hi) // 2. Which values are compared, in order?
- 02lo
- 15
- 28
- 312
- 416
- 523
- 638
- 756
- 872
- 991hi
Show answer and explanation
Explanation
lo = 0, hi = 9 → mid 4 (16 < 23) → lo = 5 → mid 7 (56 > 23) → hi = 6 → mid 5 (23) found. Three comparisons for ten elements.
Why the other options are wrong
- AThe first middle is index 4.
- CThe middle of 5–9 is index 7.
- DAfter 16 the range is indices 5–9, whose middle is 56.
Quick sort partitions with the last element (70) as pivot using the Lomuto scheme. What is the array after this single partition?
- 010
- 180
- 230
- 390
- 440
- 550
- 670
- 10
- 30
- 40
- 50
- 70
- 80
- 90
- 30
- 10
- 40
- 50
- 70
- 80
- 90
- 10
- 80
- 30
- 90
- 40
- 50
- 70
- 10
- 30
- 40
- 50
- 70
- 90
- 80
Show answer and explanation
Explanation
Lomuto keeps a boundary i of elements ≤ pivot: 10, 30, 40, 50 are swapped forward in turn, then the pivot is swapped into position 4. Elements on each side are not sorted yet – 90, 80 remain in that order.
Why the other options are wrong
- APartitioning does not sort the right side.
- BLomuto keeps the small elements in their scan order.
- CElements are moved.
How many inversions (pairs i < j with a[i] > a[j]) does this array have? (A modified merge sort counts them in O(n log n).)
- 01
- 120
- 26
- 34
- 45
The answer is a number – work it out, then open the solution.
Show answer and explanation
Explanation
(20, 6), (20, 4), (20, 5), (6, 4), (6, 5). During a merge, when an element from the right half is taken first, it is smaller than every element still waiting in the left half – add that count in one step.