Sorting Questions with Answers

60 practice questions on Sorting (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 60 free in the AptiNest app.

Sample questions

Question 1Easy

Which of these sorting algorithms is NOT stable in its usual implementation?

  1. Insertion sort
  2. Bubble sort
  3. Selection sort
  4. 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

  1. Selection sort swaps the minimum into place across other elements.
  2. This can move an equal key past its twin.
  3. 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)?

  1. [5, 1, 4, 2, 8]
  2. [1, 4, 2, 5, 8]
  3. [1, 2, 4, 5, 8]
  4. [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

  1. Compare 5,1: swap: [1, 5, 4, 2, 8].
  2. Compare 5,4: swap: [1, 4, 5, 2, 8].
  3. Compare 5,2: swap: [1, 4, 2, 5, 8].
  4. 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?

  1. Merge sort
  2. Heap sort
  3. Selection sort
  4. 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

  1. A stable sort keeps equal keys in their original relative order.
  2. Merge sort takes from the left half when keys are equal, so it is stable.
  3. 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)?

  1. [5, 14, 10, 37, 13, 29]
  2. [5, 10, 14, 37, 13, 29]
  3. [10, 14, 13, 5, 29, 37]
  4. [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

  1. Pass 1: minimum 5 swaps with 29: [5, 14, 10, 37, 13, 29].
  2. Pass 2: minimum of rest 10 swaps with 14: [5, 10, 14, 37, 13, 29].
  3. 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:

  1. O(n)
  2. O(k log n)
  3. O(log n)
  4. 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

  1. Partition around a pivot like quicksort.
  2. Recurse only into the side containing the k-th element.
  3. 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?

  1. 4
  2. 3
  3. 2
  4. 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

  1. i = 0: min 1, swap: [1, 3, 2, 5, 4].
  2. i = 1: min 2, swap: [1, 2, 3, 5, 4]; i = 2: 3 already in place.
  3. 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?

  1. [27, 38, 3, 43, 9, 82, 5, 10]
  2. [3, 5, 9, 10, 27, 38, 43, 82]
  3. [82, 10, 9, 5, 43, 38, 27, 3]
  4. [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

  1. Pass 1 (pairs): [27, 38, 3, 43, 9, 82, 5, 10].
  2. Pass 2 (runs of 4): merge [27, 38] + [3, 43] and [9, 82] + [5, 10].
  3. [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?

  1. 2
  2. 6
  3. 1
  4. 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

  1. Pass 1 swaps 4/3 and 7/6: [1, 2, 3, 4, 5, 6, 7].
  2. Pass 2 makes no swaps, so it stops.
  3. 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?

  1. Merge sort
  2. Heap sort
  3. Quick sort
  4. 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

  1. Heap sort: O(n log n) worst case.
  2. It sorts inside the array with O(1) extra space.
  3. 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?

  1. [720, 355, 436, 457, 657, 329, 839]
  2. [720, 329, 436, 839, 355, 457, 657]
  3. [329, 355, 436, 457, 657, 720, 839]
  4. [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

  1. Pass 1 (units): [720, 355, 436, 457, 657, 329, 839].
  2. Pass 2 (tens, stable): 2 (720, 329), 3 (436, 839), 5 (355, 457, 657).
  3. [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 →

Related topics

See all Data Structures & Algorithms topics · All topics