mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create counting_sort.py
This commit is contained in:
parent
28d28b817e
commit
4d2e7857b2
26
sorting/counting_sort.py
Normal file
26
sorting/counting_sort.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
|
def counting_sort(list):
|
||||||
|
|
||||||
|
K = max(lst)
|
||||||
|
counts = [0] * (K + 1)
|
||||||
|
for elem in lst:
|
||||||
|
counts[elem] += 1
|
||||||
|
|
||||||
|
starting_index = 0
|
||||||
|
for i, count in enumerate(counts):
|
||||||
|
counts[i] = starting_index
|
||||||
|
starting_index += count
|
||||||
|
|
||||||
|
sorted_lst = [0] * len(lst)
|
||||||
|
|
||||||
|
for elem in lst:
|
||||||
|
|
||||||
|
sorted_lst[counts[elem]] = elem
|
||||||
|
counts[elem] += 1
|
||||||
|
|
||||||
|
for i in range(len(lst)):
|
||||||
|
lst[i] = sorted_lst[i]
|
Loading…
x
Reference in New Issue
Block a user