-
-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathcountingsort.py
42 lines (32 loc) · 1.07 KB
/
countingsort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class CountingSort:
def __init__(self, array):
self.array = array
def sort(self):
if not self.array:
return []
# Find the maximum and minimum values
max_val = max(self.array)
min_val = min(self.array)
range_of_elements = max_val - min_val + 1
# Initialize the count array
count = [0] * range_of_elements
output = [0] * len(self.array)
# Count occurrences
for num in self.array:
count[num - min_val] += 1
# Update count array to store cumulative sums
for i in range(1, len(count)):
count[i] += count[i - 1]
# Build the output array
for num in reversed(self.array):
output[count[num - min_val] - 1] = num
count[num - min_val] -= 1
# Copy sorted elements back to original array
self.array[:] = output
def get_sorted_array(self):
return self.array
# Example Usage:
arr = [4, 2, 2, 8, 3, 3, 1]
cs = CountingSort(arr)
cs.sort()
print("Sorted Array:", cs.get_sorted_array())