Data Structures & AlgorithmsHard

In the 'shortest block to sort so the whole array becomes sorted' problem, why does the block start at the first mismatch with the sorted copy?

  1. A. The first element is always the smallest.
  2. B. Elements before the first mismatch already hold their final sorted values, so the sorted block must begin exactly there.
  3. C. Sorting always starts from index 0.
  4. D. The first mismatch is always the array maximum.

Correct answer: B. Elements before the first mismatch already hold their final sorted values, so the sorted block must begin exactly there.

Positions before the first mismatch are already in their final place, so any sorted block must include everything from that position onward. The same argument fixes the right end at the last mismatch.

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

Exams that ask this topic