1

Say I have the unsorted array = [(3,a), (1,p), (3,c), (7,f), (5,g), (3,b), (7,d), (8,w)].

My count array will be = [1,0,3,0,1,0,2,1]. For example, count[1] means that the frequency of element 1 is 1. (Assume index starts at 1 and not 0).

So, if we just use the count array, then the sorted array will be [1,3,3,3,5,7,7,8].

To get the sorted array, we are just appending element i count[i] times into an array where i in the range [1,8] for this specific example. We do this by iterating on count array.

Now I know one issue is that the data associated with the positive integers are discarded. This is one issue.

We can see that we are not using the elements from the input array (unsorted array). We are simply appending element i count[i] times.

So, since the above counting sort just appends i count[i] times and not directly using elements from the input array (unsorted array), is that the reason why it's not considered stable for the above example?

The notes I am reading on says "No, because it counts the values but does not distinguish between them" to the question "Is counting sort stable?" for the specific example above but I am having a hard time understanding what it means to say "but does not distinguish between them".

Thank you for any insight.

Fried_Mind
  • 11
  • 4
  • A stable sort would arrange the `3`s as `(3,a),(3,c),(3,b)` because that's the order in which they appear in the original array. A non-stable sort would arrange them in some other order, like `(3,c),(3,a),(3,b)`. But your sort throws away the information that allows you to see the difference between one 3 and another. So your sort is a none-of-the-above sort. It *is* possible to create a stable counting sort that preserves the information. It's also possible to create a non-stable counting sort. – user3386109 Jan 26 '22 at 02:27
  • @user3386109 "But your sort throws away the information that allows you to see the difference between one 3 and another". Yeah, this is what I was confused about. I thought since the above counting sort doesn't take into consideration the elements directly from the input array to sort the elements, it's not considered stable. But what you are saying is that it's "none of the above". Just still a little confused because my notes say "it's not stable because it does not distinguish between them" but I am having a hard time trying to understand that statement – Fried_Mind Jan 26 '22 at 02:35
  • 1
    When you can't distinguish between elements of the array that have the same sort key, there is no concept of stable or non-stable, because there's no way to know whether the elements have been rearranged or not. So the statement from your notes has no meaning. A sort is stable when elements with the same sort key appear in the same order in the output array as they do in the input array. – user3386109 Jan 26 '22 at 02:46
  • 1
    @user3386109 Yeah this makes sense and kind of what I thought as well. Will have to point this out to my lecturers and see what they say about it but can't as its semester holidays. Thanks for the explanation. – Fried_Mind Jan 26 '22 at 03:25

0 Answers0