Skip to content

Commit e522743

Browse files
authored
Merge pull request larymak#263 from neil-codes/main
Added Counting Sort and Radix Sort
2 parents 63da663 + bfd7268 commit e522743

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

Data Structures and Algorithms/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
- Bogo Sort
1010

11+
- Counting Sort
12+
1113
- Linear search (for numbers)
1214

1315
- Linear search (for strings)
@@ -28,6 +30,8 @@
2830

2931
- Quick Sort (for strings)
3032

33+
- Radix Sort
34+
3135
- Recursion
3236

3337
- Recursive Binary Search
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def counting_sort(list):
2+
"""
3+
sorts a list of nonnegative integers in ascending order
4+
5+
takes O(n + k) time where
6+
k is the maximum value in the list
7+
n is the length of the list
8+
"""
9+
10+
k = max(list)
11+
n = len(list)
12+
count = [0] * (k+1)
13+
new_lst = [0] * n
14+
15+
# Counting Sort algorithm
16+
for i in range(n):
17+
count[list[i]] += 1
18+
19+
for i in range(1, k+1):
20+
count[i] += count[i-1]
21+
22+
for i in range(n):
23+
new_lst[count[list[i]]-1] = list[i]
24+
count[list[i]] -= 1
25+
26+
# Copy elements back into original list
27+
for i in range(n):
28+
list[i] = new_lst[i]
29+
30+
return list
31+
32+
# Test Case
33+
def verify(list):
34+
list_copy = list[:]
35+
list_copy.sort()
36+
return list == list_copy
37+
38+
test_list = [1, 3, 1, 4, 10, 6, 3, 4, 5, 8, 3, 4, 8]
39+
sorted = counting_sort(test_list)
40+
print(sorted)
41+
print(verify(sorted))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def counting_sort(list, digit):
2+
"""
3+
sorts a list of nonnegative integers in ascending order
4+
unstable version used as a helper for radix sort
5+
6+
takes O(n + k) time where
7+
k is the maximum value in the list
8+
n is the length of the list
9+
"""
10+
n = len(list)
11+
count = [0] * 10
12+
new_list = [0] * n
13+
14+
for i in range(n) :
15+
index = int((list[i]/digit)%10)
16+
count[index] += 1
17+
18+
for i in range(1, 10):
19+
count[i] = count[i] + count[i-1]
20+
21+
for i in range (n-1, -1, -1):
22+
index = int((list[i]/digit)%10)
23+
new_list[count[index]-1] = list[i]
24+
count[index] -= 1
25+
26+
for i in range(n):
27+
list[i] = new_list[i]
28+
29+
return list
30+
31+
# Test Case
32+
def verify(list):
33+
list_copy = list[:]
34+
list_copy.sort()
35+
return list == list_copy
36+
37+
def main():
38+
test_list = [1, 3, 1, 4, 9, 6, 3, 4, 5, 8, 3, 4, 8]
39+
sorted = counting_sort(test_list, 1)
40+
print(sorted)
41+
print(verify(sorted))
42+
43+
if __name__ == "__main__":
44+
main()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from counting_sort_2 import counting_sort, verify
2+
3+
def radix_sort(list):
4+
"""
5+
sorts a list of integers in ascending order in a particular base
6+
7+
takes O(d(n + k)) where:
8+
d is the number of digits of the maximum element
9+
k is the maximum value in the list
10+
n is the length of the list
11+
"""
12+
13+
max_val = max(list)
14+
digit = 1
15+
16+
while digit < max_val:
17+
counting_sort(list, digit)
18+
digit *= 10
19+
20+
return list
21+
22+
# Test Case
23+
test_list = [54, 897, 434, 4, 8, 209, 33, 578, 443, 930, 564, 25, 347, 6, 1]
24+
sorted = radix_sort(test_list)
25+
print(sorted)
26+
print(verify(sorted))
27+

0 commit comments

Comments
 (0)