Which sort is unstable — Aptitude Question
Which sort is unstable: Which of these sorting algorithms is not stable in its usual implementation? Answer and worked solution on CodeKairo.
- Topic: Data Structures & Algorithms
- Section: Programming MCQs
- Difficulty: Easy
- Time target: 60 seconds
Question
Which of these sorting algorithms is not stable in its usual implementation?
Options
- Bubble sort
- Insertion sort
- Merge sort
- Quicksort
Show the answer and the worked solution
Correct answer: D. Quicksort
Worked solution
- Bubble sort and insertion sort move elements only past strictly larger neighbours, so equal keys never cross each other.
- Merge sort stays stable by taking from the left run whenever two keys tie.
- Quicksort swaps elements across long distances during partitioning, which can push one equal key past another, so it is the unstable one.
Answer: Quicksort.
The idea
Stability matters when records are sorted by one field after another. Heapsort and selection sort are the other classic unstable sorts, for the same reason: they move elements far from where they started.
Shortcut. Instability comes from long-distance swaps; of these four only quicksort's partition makes them, while the other three never move one equal key past another.
Common trap. "Merge sort" splits and recombines, so it looks as if it could scramble equal keys, but taking from the left run on a tie keeps it stable.
All Data Structures & Algorithms questions · placement mock tests