What are the differences between them? Compare the stability of QuickSort and MergeSort and discuss which one would be a better choice in a scenario where stability is important.
What are the differences between them? Compare the stability of QuickSort and MergeSort and discuss which one would be a better choice in a scenario where stability is important.
QuickSort is not stable because it involves swapping while sorting it swaps when next elements is less then pivot, and it will swaps the first greater elements.
MergeSort is stable, merge sort does not involves swapping. it divide the unsort array into half each time until the length becomes 1, and array with 1 elements is always sorted. then combine 2 array and sort them, if they are the same, it will just append. Do the same process to other elements until we comined all elements. (1 combine to 2, 2 combine to 4 …..)
Hence Merge sort is better in a scenario where stability is important.