Is merge sort stable? I read it in a book which says merge sort is stable as long as the merge operation implemented properly. Is that true?
Unlike some (efficient) implementations of quicksort, merge sort is a stable sort. Merge sort's most common implementation does not sort in place; therefore, the memory size of the input must be allocated for the sorted output to be stored in (see below for variations that need only n/2 extra spaces).
Several common sorting algorithms are stable by nature, such as Merge Sort, Timsort, Counting Sort, Insertion Sort, and Bubble Sort. Others such as Quicksort, Heapsort and Selection Sort are unstable. We can modify unstable sorting algorithms to be stable.
A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input unsorted array. Some Sorting Algorithm is stable by nature like Insertion Sort, Merge Sort and Bubble Sort etc.
true. It depends on how you properly implement the merge sort. http://en.wikipedia.org/wiki/Stable_sort#Stability
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With