2

Question: I was going through the list of sorts and found a very intriguing image of all sort stabilities.I am not going to ask what does the stability of sorting mean since it has already been answered.My question is how selection sort ,is unstable since it is so widely used.

Here is the image of the sorts and their stability and complexities:-

Image of all sorts

As you can see from the image the last column provides us the stability of each of these sorts.

Source of image:-

https://medium.com/@_marcos_otero/the-real-10-algorithms-that-dominate-our-world-e95fa9f16c04

I am not experienced with quick and heapsort but I am used to selection sort and used it in various programs and never found any kind of unstable behaviour.I know that the complexity wise bubblesort is far better than selection sort but there actual work is same which is sorting and arranging a group of values either in ascending or descending order right?So this stability in sortings is a bit confusing and mostly forces me to think how one sort is stabler than the other.

Can anyone explain me the unstable behaviour and how their stabilities differ with respect to their complexities in these sortings?

  • [Why isn't heapsort stable?](//stackoverflow.com/q/19336881) – 001 Feb 21 '18 at 19:29
  • @JohnnyMopp I have asked specifically for selection sort. –  Feb 21 '18 at 19:30
  • 3
    Stability is unrelated to complexity. A sort is stable if all items which compare as equal appear in the same order in the output list as in the input list. It's nothing to do with "forming a strong algorithmic foundation". – bcsb1001 Feb 21 '18 at 19:31
  • Actually, you stated _"My question is how selection sort ,quicksort and heapsort are unstable"_. – 001 Feb 21 '18 at 19:32
  • @JohnnyMopp OK sorry , it's my fault I agree.But you also must understand that why I have written selection sort also in bold letters.Downvoting is absolutely your choice and your right but I request you to already clarify with the user before doing that.I am editing it right now.So please give me at least 3 minutes. –  Feb 21 '18 at 19:34
  • 1
    FWIW, I didn't DV. – 001 Feb 21 '18 at 19:36
  • @JohnnyMopp my question was just down voted after your comment.Just a misunderstanding,hope you don't mind. –  Feb 21 '18 at 19:38
  • 1
    May be it will help you. https://stackoverflow.com/q/20761396/6242317 – Vicky Feb 21 '18 at 20:20

1 Answers1

13

What makes selection sort unstable is that the step of swapping a pair of elements could possibly change the relative order of another pair of elements that have equal keys. For instance, when sorting the array

2 2' 1

since the element with the minimum key is 1, you'll have to push it to the lowest position of the array by swapping 1 with 2:

1 2' 2

Swapping 1 with 2 changed the relative order of the two equal elements (2' and 2).

That is, two elements with equal keys do not appear in the same order in the sorted output as they appear in the input array. Hence, selection sort is unstable.

snakile
  • 52,936
  • 62
  • 169
  • 241
  • 2
    This answer is so clear, I just want to add this sentence, which may help others to understand the property of stability, from _Introduction to Algorithms_ written by CLRS (page 196); **.. Normally the property of stability is important only when satellite data are carried around with the element being sorted.** You can think `'` as satellite data in this example. First 2 is not carrying satellite data and second 2 is carrying satellite data `'`. – ulubeyn Feb 21 '18 at 22:59