what is difference between stable sorts vs. unstable sorts

what is difference between stable sorts vs. unstable sorts

·

1 min read

Definition

Stable and unstable is depend on how sort any duplicated list.

If the algorithm keeps original position of duplicated values, it is stable sort algorithm
If the algorithm positioned duplicated values randomly, it is unstable sort algorithm

Example

[A,C,A,B,C,D] -> [A,A,B,C,C,D]

raw list=[A(position 1), C(position 2), A(position 3), B, C(position 5), D]
stable sorted list=[A(position 1), A(position 3), B, C(position 2), C(position 5), D]
unstable sorted list=[A(position 3), A(position 1), B, C(position 5), C(position 2), D]

Stable Sorting

Bubble Sort
Insertion Sort
Merge Sort

Unstable Sorting

Quick Sort
Selection sort
Heap Sort