A sorting algorithm is stable if it maintains the order of occurrence same elements. Let’s just take an example to understand.

Say, we have this array of elements

Array (A) Index Number |
First | Second | Third | Fourth | Fifth |
---|---|---|---|---|---|

Contents |
6 | 3 | 2 | 4 | 2 |

Now, the sorted sequence of the above array will be array B which is:

Array (A) Index Number |
First | Second | Third | Fourth | Fifth |
---|---|---|---|---|---|

Contents |
2 | 2 | 3 | 4 | 6 |

Now, the question is, which element of **A** is the first element of **B**. Is it the third element or is it the fifth element?

Stable sorting algorithms will maintain the position of similar element. That is, the first **2** of array **A** will appear as first **2** in **B**. Therefore, in stable sorting algorithms, the array **A** (input) and array **B** (output) will be as follows:

Input Array (A) Index Number |
First | Second | Third | Fourth | Fifth |
---|---|---|---|---|---|

Contents |
6 | 3 | 2a |
4 | 2b |

Output Array (B) Index Number |
First | Second | Third | Fourth | Fifth |
---|---|---|---|---|---|

Contents |
2a |
2b |
3 | 4 | 6 |

On the other hand, in unstable sort this order is not maintained, that is:

Input Array (A) Index Number |
First | Second | Third | Fourth | Fifth |
---|---|---|---|---|---|

Contents |
6 | 3 | 2a |
4 | 2b |

Output Array (B) Index Number |
First | Second | Third | Fourth | Fifth |
---|---|---|---|---|---|

Contents |
2b |
2a |
3 | 4 | 6 |

The suffix **a** and **b** are used to represent the order of occurance of similar elements.

Heap Sort and Quick Sort are not stable sorting algorithm whereas, bubble sort, merge sort, insertion sort, selection sort, counting sort, radix sort are stable.