When we talk about sorting algorithms, it’s important to know about stability.
A stable sorting algorithm keeps the order of items that are the same, while an unstable sorting algorithm does not.
So, how do we figure out if an algorithm is stable or unstable?
Definition: A sorting algorithm is stable if it keeps the order of items that are equal. For example, if you have two identical items, let's say 'A' and 'B', and 'A' is listed before 'B' in the original list, a stable algorithm will keep 'A' before 'B' in the sorted list too.
Examples of Stable Algorithms:
Examples of Unstable Algorithms:
In short, to figure out if a sorting algorithm is stable or unstable, think about how it deals with items that are the same. If their order stays the same, then the algorithm is stable. If not, it is unstable.
When we talk about sorting algorithms, it’s important to know about stability.
A stable sorting algorithm keeps the order of items that are the same, while an unstable sorting algorithm does not.
So, how do we figure out if an algorithm is stable or unstable?
Definition: A sorting algorithm is stable if it keeps the order of items that are equal. For example, if you have two identical items, let's say 'A' and 'B', and 'A' is listed before 'B' in the original list, a stable algorithm will keep 'A' before 'B' in the sorted list too.
Examples of Stable Algorithms:
Examples of Unstable Algorithms:
In short, to figure out if a sorting algorithm is stable or unstable, think about how it deals with items that are the same. If their order stays the same, then the algorithm is stable. If not, it is unstable.