# Why Counting Sort (and Radix Sort) don't work with Negative Numbers!

**URL:** https://forum.kirupa.com/t/why-counting-sort-and-radix-sort-dont-work-with-negative-numbers/667105
**Category:** web dev
**Created:** [May 25, 2024, 4:08am UTC](https://forum.kirupa.com/t/why-counting-sort-and-radix-sort-dont-work-with-negative-numbers/667105 "2024-05-25T04:08:42Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [May 25, 2024, 4:08am UTC](https://forum.kirupa.com/t/why-counting-sort-and-radix-sort-dont-work-with-negative-numbers/667105/1 "2024-05-25T04:08:42Z")

</div>

In my [Counting Sort](https://www.kirupa.com/data_structures_algorithms/counting_sort.htm) tutorial, I mentioned that for counting sort to work, the numbers should be:

1. Made up of positive numbers (0 and greater)

2. The range between the smallest and the largest number needs to be fairly small, around the same order of magnitude as the input size

When it comes to #1, the answer to why it doesn’t work can be explained here: [Diving Deep into Array Index Positions](https://www.kirupa.com/javascript/deep_dive_array_index_positions.htm)

When our arrays encounter a negative index position caused by a negative value we are trying to sort, JavaScript works just fine. The reason is that, while negative index positions aren’t valid, they end up being treated like object keys/properties.

Look at the following image where we have **-2** as a key on our `people` array:

 ![](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/7/4/746eeca45330cbbb8caf7eb4292a56a91d4e58dc.png)

This is also the problem. While JavaScript is OK with what look like negative index positions, they don’t work with the array behavior of Arrays. This means our intermediate arrays, created as part of Counting Sort, store the negative value. These values are stored as non-array key/value pairs, so they don’t get picked up by array operations.

Cheers,  
Kirupa 🙂

---

<div class="post-metadata">

### Author: ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)
#### Post date: [May 25, 2024, 8:24pm UTC](https://forum.kirupa.com/t/why-counting-sort-and-radix-sort-dont-work-with-negative-numbers/667105/2 "2024-05-25T20:24:19Z")

</div>

> [@kirupa](#):
>
> If the numbers we are sorting contains negative numbers, counting sort seems to work just fine.

Just copying and pasting from the tutorial, looks like it filters out negative numbers rather than including them in the sorted output?

---

<div class="post-metadata">

### Author: ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)
#### Post date: [May 25, 2024, 8:36pm UTC](https://forum.kirupa.com/t/why-counting-sort-and-radix-sort-dont-work-with-negative-numbers/667105/3 "2024-05-25T20:36:29Z")

</div>

```javascript
countingSort([3, -2, 1, 400, { valueOf: _ => -10, toString: _ => 'length' }])

```

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [May 25, 2024, 10:32pm UTC](https://forum.kirupa.com/t/why-counting-sort-and-radix-sort-dont-work-with-negative-numbers/667105/4 "2024-05-25T22:32:48Z")

</div>

Yikes. You are right. Actually, it does something worse. It creates an empty array with the negative numbers included in the total length, but the intermediate arrays no longer work because the array[-negativeIndex] operation results in subsequent array operations failing because they don’t account for values outside of the array range.

Let me update the initial post to fully backtrack on what I had initially said :facepalm:

---

<div class="post-metadata">

### Author: ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)
#### Post date: [May 26, 2024, 1:05am UTC](https://forum.kirupa.com/t/why-counting-sort-and-radix-sort-dont-work-with-negative-numbers/667105/5 "2024-05-26T01:05:42Z")

</div>

It was an interesting prompt about array subscripts in any event. I think what got me started thinking was something like, if `array[-1]` and `array[-2]` are both `undefined`, how could code differentiate the two for sorting purposes.
