Skip to content

Commit c24f4da

Browse files
committed
Counting sort algorithm
1 parent 9752a83 commit c24f4da

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Algorithm/Sorting/counting_sort.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
arr = list(map(int, input().split()))
2+
3+
length = len(arr)
4+
5+
countArray = [0] * (max(arr) + 1)
6+
7+
outputArray = [0] * length
8+
9+
for i in range(length):
10+
countArray[arr[i]] +=1
11+
12+
for i in range(1, len(countArray)):
13+
countArray[i] += countArray[i - 1]
14+
15+
for i in range(length - 1, -1, -1):
16+
outputArray[countArray[arr[i]] -1 ] = arr[i]
17+
countArray[arr[i] ] -=1
18+
19+
print(outputArray)

0 commit comments

Comments
 (0)