Skip to content

✨ add counting sort#51

Open
IronMax03 wants to merge 2 commits intomainfrom
enhancement/counting_sort
Open

✨ add counting sort#51
IronMax03 wants to merge 2 commits intomainfrom
enhancement/counting_sort

Conversation

@IronMax03
Copy link
Member

@IronMax03 IronMax03 commented Apr 25, 2025

Closes #34

@IronMax03 IronMax03 changed the title add counting sort ✨ add counting sort Apr 27, 2025
```
CountingSort(array, max_value)
Let count be an array with a length equal to max_value. All elements are in count are equal to -1.
Ler output be an array of same length as array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let *


```
CountingSort(array, max_value)
Let count be an array with a length equal to max_value. All elements are in count are equal to -1.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All elements shall be at 0
e.g
https://en.wikipedia.org/wiki/Counting_sort

CountingSort(array, max_value)
Let count be an array with a length equal to max_value. All elements are in count are equal to -1.
Ler output be an array of same length as array

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment
// count occurence, of each element,


for i in array:
count[i - 1] = count[i - 1] + 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Calculate cumulative count (position)


for i to length(count) - 1:
count[i + 1] = count[i + 1] + count[i]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Place elements in sorted order

count[i + 1] = count[i + 1] + count[i]

# sorting using count
for i in range(len(array)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shall be in reversed order

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    for num in reversed(array):  # Process in reverse for stability
        index = count[num - 1] - 1
        output[index] = num
        count[num - 1] -= 1

for i to length(count) - 1:
count[i + 1] = count[i + 1] + count[i]

for i to length(array):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for i = length(array)-1 down to 0:
output[count[array[i]]-1] = array[i]
count[array[i]] -= 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

✨ implement Counting sort

2 participants