Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 4d2e785

Browse files
Create counting_sort.py
1 parent 28d28b8 commit 4d2e785

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

sorting/counting_sort.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# author: bt3gl
4+
5+
6+
def counting_sort(list):
7+
8+
K = max(lst)
9+
counts = [0] * (K + 1)
10+
for elem in lst:
11+
counts[elem] += 1
12+
13+
starting_index = 0
14+
for i, count in enumerate(counts):
15+
counts[i] = starting_index
16+
starting_index += count
17+
18+
sorted_lst = [0] * len(lst)
19+
20+
for elem in lst:
21+
22+
sorted_lst[counts[elem]] = elem
23+
counts[elem] += 1
24+
25+
for i in range(len(lst)):
26+
lst[i] = sorted_lst[i]

0 commit comments

Comments
 (0)