sorting - I don't understand stable algorithm definition -
in computer science, stable sorting algorithm preserves order of records equal keys.
i don't understand why sorting algorithm stable , others none. basic sorting algorithm orders items of array of int. if create class or struct , use same algorithm considering swapping of whole object, , choosing order key, o age ecc, every algorithm can preserve order of records!
i think missed definition.
thanks lot.
say have array following:
a = [5, 4, 2a, 2b, 1]
where a
, b
there denote first 2 (2a
) comes before second 2 (2b
). in stable sorting algorithm, result be:
a_stable = [1, 2a, 2b, 4, 5]
that is, relative order of elements hasn't changed - 2a
came before 2b
in original array, , remains way in sorted array.
with non-stable algorithm, result be:
a_nonstable = [1, 2b, 2a, 4, 5]
this still correctly sorted, it's positions relative in original, unsorted array have changed.
Comments
Post a Comment