forked from AllAlgorithms/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting_sort.py
More file actions
35 lines (31 loc) · 865 Bytes
/
Copy pathcounting_sort.py
File metadata and controls
35 lines (31 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#
# Python implementation of counting sort.
#
#
# The All ▲lgorithms Project
#
# https://allalgorithms.com/
# https://github.com/allalgorithms/cpp
#
# Contributed by: Simon Faillace Mullen
# Github: @macmullen
#
def counting_sort(arr):
# Create the counting sort array with length equal to the maximum number
# in the list.
count_array = [0] * (max(arr) + 1)
# Count the amount of repetitions for each number.
for number in arr:
count_array[number] += 1
# Append the amount of repetitions in order.
position = 0
for index, number in enumerate(count_array):
for amount in range(count_array[index]):
arr[position] = index
position += 1
arr = [8, 5, 8, 4, 3, 3, 2, 1, 5, 5, 5, 9, 7, 7, 8, 1, 9, 3, 2]
print("Unsorted array:")
print(arr)
counting_sort(arr)
print("Sorted array:")
print(arr)