Is merge sort stable or unstable, and why?

Prepare for the TJR Bootcamp Test with flashcards and detailed questions. Get hints and explanations for each query. Ace your exam!

Multiple Choice

Is merge sort stable or unstable, and why?

Explanation:
Stability means that equal keys keep their original relative order after sorting. In merge sort, each merge combines two sorted halves while preserving the order within each half. When two elements have the same key, taking the element from the left half first ensures the one that appeared earlier in the input stays before the later equal element in the merged result. Because every merge step does this, equal elements retain their input order throughout the process, so the sort is stable. If you ever took the right element when keys are equal, you’d disrupt that order and lose stability. Stability is a property of the merge process, not of the input being already sorted.

Stability means that equal keys keep their original relative order after sorting. In merge sort, each merge combines two sorted halves while preserving the order within each half. When two elements have the same key, taking the element from the left half first ensures the one that appeared earlier in the input stays before the later equal element in the merged result. Because every merge step does this, equal elements retain their input order throughout the process, so the sort is stable. If you ever took the right element when keys are equal, you’d disrupt that order and lose stability. Stability is a property of the merge process, not of the input being already sorted.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy