Sample questions
Question 1Easy
Which of these sorting algorithms is NOT stable in its usual implementation?
- Insertion sort
- Bubble sort
- Selection sort
- Merge sort
Answer: C. Selection sort
Explanation
Selection sort's long-distance swap can jump an element past an equal one.
Step-by-step solution
- Selection sort swaps the minimum into place across other elements.
- This can move an equal key past its twin.
- So it is not stable.
Why the other options are wrong
- A. Insertion sort is stable.
- B. Bubble sort is stable.
- D. Merge sort is stable.
Tip: Unstable: selection, heap, quick. Stable: bubble, insertion, merge.
Question 2Easy
What does the array [5, 1, 4, 2, 8] look like after the FIRST pass of bubble sort (ascending)?
- [5, 1, 4, 2, 8]
- [1, 4, 2, 5, 8]
- [1, 2, 4, 5, 8]
- [1, 5, 4, 2, 8]
Answer: B. [1, 4, 2, 5, 8]
Explanation
Compare adjacent pairs: swap 5,1 -> [1,5,4,2,8]; swap 5,4 -> [1,4,5,2,8]; swap 5,2 -> [1,4,2,5,8]; 5 < 8 no swap. The largest element bubbles to the end.
Step-by-step solution
- Compare 5,1: swap: [1, 5, 4, 2, 8].
- Compare 5,4: swap: [1, 4, 5, 2, 8].
- Compare 5,2: swap: [1, 4, 2, 5, 8].
- Compare 5,8: no swap. After pass 1: [1, 4, 2, 5, 8].
Why the other options are wrong
- A. Swaps happen in the first pass.
- C. This is the fully sorted array, after more passes.
- D. This shows only the first swap.
Tip: After pass k, the last k elements are in final position.
Question 3Easy
Which of these sorting algorithms is stable in its standard implementation?
- Merge sort
- Heap sort
- Selection sort
- Quick sort
Answer: A. Merge sort
Explanation
A stable sort keeps equal keys in their original relative order. Merge sort is stable if equal elements are taken from the left half first; the others can reorder equal keys.
Step-by-step solution
- A stable sort keeps equal keys in their original relative order.
- Merge sort takes from the left half when keys are equal, so it is stable.
- Heap, selection and quick sort move elements across long distances and can reorder equal keys.
Why the other options are wrong
- B. Heap operations swap distant elements.
- C. The swap can jump an element over an equal one.
- D. Partitioning swaps elements across the pivot.
Tip: Stable: merge, insertion, bubble, counting. Unstable: quick, heap, selection.
Question 4Easy
What does [29, 14, 10, 37, 13, 5] look like after 2 passes of selection sort (ascending; each pass swaps the minimum of the unsorted part to the front)?
- [5, 14, 10, 37, 13, 29]
- [5, 10, 14, 37, 13, 29]
- [10, 14, 13, 5, 29, 37]
- [5, 10, 13, 37, 14, 29]
Answer: B. [5, 10, 14, 37, 13, 29]
Explanation
Pass i places the i-th smallest at index i-1 by one swap. After 2 passes: [5, 10, 14, 37, 13, 29].
Step-by-step solution
- Pass 1: minimum 5 swaps with 29: [5, 14, 10, 37, 13, 29].
- Pass 2: minimum of rest 10 swaps with 14: [5, 10, 14, 37, 13, 29].
- Done after 2 passes.
Why the other options are wrong
- A. That is after 1 pass.
- C. Not selection sort.
- D. That is after 3 passes.
Question 5Medium
Quickselect finds the k-th smallest element in average time:
- O(n)
- O(k log n)
- O(log n)
- O(n log n)
Answer: A. O(n)
Explanation
It recurses into only one side of each partition: n + n/2 + n/4 + ... = O(n) on average.
Step-by-step solution
- Partition around a pivot like quicksort.
- Recurse only into the side containing the k-th element.
- Average n + n/2 + n/4 + ... = O(n).
Why the other options are wrong
- B. That is a heap-based method.
- C. Too small.
- D. That is full sorting.
Question 6Medium
How many swaps does selection sort perform on [3, 1, 2, 5, 4], if it swaps only when the minimum is not already in place?
- 4
- 3
- 2
- 5
Answer: B. 3
Explanation
Pass 1 swaps 3 and 1, pass 2 swaps 3 and 2, pass 3 needs no swap, pass 4 swaps 5 and 4: 3 swaps.
Step-by-step solution
- i = 0: min 1, swap: [1, 3, 2, 5, 4].
- i = 1: min 2, swap: [1, 2, 3, 5, 4]; i = 2: 3 already in place.
- i = 3: min 4, swap: [1, 2, 3, 4, 5]. Total 3.
Why the other options are wrong
- A. Counts the pass where 3 was in place.
- C. Misses one swap.
- D. Too many.
Question 7Medium
Bottom-up merge sort first merges runs of size 1 into sorted pairs, then pairs into runs of 4, and so on. What is [38, 27, 43, 3, 9, 82, 10, 5] after the pass that produces sorted runs of size 4?
- [27, 38, 3, 43, 9, 82, 5, 10]
- [3, 5, 9, 10, 27, 38, 43, 82]
- [82, 10, 9, 5, 43, 38, 27, 3]
- [3, 27, 38, 43, 5, 9, 10, 82]
Answer: D. [3, 27, 38, 43, 5, 9, 10, 82]
Explanation
Pairs: [27, 38, 3, 43, 9, 82, 5, 10]; then merging adjacent pairs gives [3, 27, 38, 43, 5, 9, 10, 82].
Step-by-step solution
- Pass 1 (pairs): [27, 38, 3, 43, 9, 82, 5, 10].
- Pass 2 (runs of 4): merge [27, 38] + [3, 43] and [9, 82] + [5, 10].
- [3, 27, 38, 43, 5, 9, 10, 82].
Why the other options are wrong
- A. That is after the pairs pass.
- B. That is fully sorted.
- C. Sorted descending.
Question 8Medium
Bubble sort with early exit (stop after a pass with no swaps) sorts [1, 2, 4, 3, 5, 7, 6]. How many passes are made, counting the final pass with no swaps?
- 2
- 6
- 1
- 3
Answer: A. 2
Explanation
- Pass 1 fixes both out-of-place pairs (4,3) and (7,6);
- pass 2 makes no swaps, so the sort stops after 2 passes.
Step-by-step solution
- Pass 1 swaps 4/3 and 7/6: [1, 2, 3, 4, 5, 6, 7].
- Pass 2 makes no swaps, so it stops.
- 2 passes.
Why the other options are wrong
- B. That is the count without early exit.
- C. The first pass had swaps, so another is needed.
- D. Only one pass is needed after sorting.
Question 9Hard
Which algorithm sorts in O(n log n) worst case, in place, using O(1) extra space?
- Merge sort
- Heap sort
- Quick sort
- Counting sort
Answer: B. Heap sort
Explanation
Heap sort is in place and guaranteed n log n;
merge sort needs O(n) extra space and quicksort is O(n^2) in the worst case.
Step-by-step solution
- Heap sort: O(n log n) worst case.
- It sorts inside the array with O(1) extra space.
- So heap sort.
Why the other options are wrong
- A. Merge sort needs O(n) extra.
- C. Quicksort's worst case is O(n^2).
- D. Counting sort needs O(k) extra and is not comparison-based.
Question 10Hard
LSD radix sort (stable, base 10) is applied to [329, 457, 657, 839, 436, 720, 355]. What is the array after the second pass?
- [720, 355, 436, 457, 657, 329, 839]
- [720, 329, 436, 839, 355, 457, 657]
- [329, 355, 436, 457, 657, 720, 839]
- [329, 355, 457, 436, 657, 720, 839]
Answer: B. [720, 329, 436, 839, 355, 457, 657]
Explanation
Pass 1 (units) gives [720, 355, 436, 457, 657, 329, 839]; pass 2 stably sorts that by the tens digit: [720, 329, 436, 839, 355, 457, 657].
Step-by-step solution
- Pass 1 (units): [720, 355, 436, 457, 657, 329, 839].
- Pass 2 (tens, stable): 2 (720, 329), 3 (436, 839), 5 (355, 457, 657).
- [720, 329, 436, 839, 355, 457, 657].
Why the other options are wrong
- A. That is after pass 1.
- C. Fully sorted.
- D. Not stable by tens digit.
Practise all 60 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →