In: Computer Science
Algorithm coures
Stability is one of the most vital factors to be considered during sorting if we have key-value pairs of duplicate possible keys.
Stability of the sorting algorithms is be explained as:
A sorting algorithm is said to be stable if two values with equal keys maintain the same order while being sorted from the input array.
Sorting algorithms: Quick Sort and Heap sort are both considered unstable.
Quick sort is unstable as it sorts the non-adjacent elements and does not preserve the relative order of equal keys.
Whereas Heap sort is unstable as the outcome of the result of Heap sort comes from removing items from the created heap in purely sized order. It does not give you any information about the ordering of items in the originally present sequence. All unstable sorts can be converted to stable form by the augmentation of each key with its initial index, and compare when the original keys are equal.
Thanks.