Data Structures & AlgorithmsModerateCognizant · Capgemini pattern

Insertion sort (ascending) is applied to [5, 2, 4, 6, 1, 3]. What does the array look like after the first 3 passes (i.e. after inserting the elements at indices 1, 2 and 3)?

  1. A. 1 2 4 5 6 3
  2. B. 2 4 5 6 1 3
  3. C. 1 2 3 5 4 6
  4. D. 2 4 5 1 6 3

Correct answer: B. 2 4 5 6 1 3

Pass 1 inserts 2 → 2 5 4 6 1 3; pass 2 inserts 4 → 2 4 5 6 1 3; pass 3 inserts 6, which is already in place → 2 4 5 6 1 3. Only the prefix is sorted; 1 and 3 have not been touched yet. 1 2 4 5 6 3 is what you get after pass 4.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic