What is difference between stable and unstable sorting algorithm?

Any sorting algorithm is considered to be stable if the relative position of equivalent elements remain same before and after the sorting.
List of Stable and Unstable Sorting Algorithms

Stable Sorting Algorithms:-

Insertion Sort
Merge Sort
Bubble Sort etc.
Unstable Sorting Algorithms:-

Heap Sort
Selection sort
Shell sort
Quick Sort etc.
 
Stable sorting algorithms maintain the relative order of records with equal keys .Lets take a array of numbers A[8,3,1,4,5,3] there are two threes is the array.
So let us write(just for understanding) the three at position A[5]=3* so our array is now A[8,3,1,4,5,3*] .Now when we sort the array A we will have A[1,3,3*,4,5] or A[1,3*,3,4,5] based on the algorithm we use.If 3 comes before the 3* the sort is stable that is there relative order is maintained .This has to be followed for every element which occur more than once in an array.
 
Back
Top