Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

counting sort #433

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Python/counting_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Counting sort in Python programming


def countingSort(array):
size = len(array)
output = [0] * size

# Initialize count array
count = [0] * 10

# Store the count of each elements in count array
for i in range(0, size):
count[array[i]] += 1

# Store the cummulative count
for i in range(1, 10):
count[i] += count[i - 1]

# Find the index of each element of the original array in count array
# place the elements in output array
i = size - 1
while i >= 0:
output[count[array[i]] - 1] = array[i]
count[array[i]] -= 1
i -= 1

# Copy the sorted elements into original array
for i in range(0, size):
array[i] = output[i]


data = [4, 2, 2, 8, 3, 3, 1]
countingSort(data)
print("Sorted Array in Ascending Order: ")
print(data)