Update counting_sort.py

This commit is contained in:
marina 2023-08-02 23:41:18 -07:00 committed by GitHub
parent 02c26f8d8a
commit bcc9398a27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,26 +1,26 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# author: bt3gl
def counting_sort(list): def counting_sort(array):
K = max(lst) k = max(array)
counts = [0] * (K + 1) counts = [0] * (k + 1)
for elem in lst:
counts[elem] += 1 for e in array:
counts[e] += 1
starting_index = 0 starting_index = 0
for i, count in enumerate(counts): for i, count in enumerate(counts):
counts[i] = starting_index counts[i] = starting_index
starting_index += count starting_index += count
sorted_lst = [0] * len(lst) sorted_list = [0] * len(array)
for elem in lst: for e in array:
sorted_lst[counts[elem]] = elem sorted_list[counts[e]] = e
counts[elem] += 1 counts[e] += 1
for i in range(len(lst)): for i in range(len(array)):
lst[i] = sorted_lst[i] array[i] = sorted_list[i]