Data Structures & AlgorithmsHard

Stably partition 5 2 7 4 1 8 6 3 so that all EVEN values come first and all ODD values come after, keeping the original relative order inside each group. Give the resulting sequence.

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

Correct answer: B. 2 4 8 6 5 7 1 3

A stable partition is two passes that copy each group in its original order. Sorting, putting odd values first, or reversing the result each break the stated requirement.

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

Exams that ask this topic