Skip to content

Commit de86161

Browse files
authored
Code refactored and removed unecessary loops
1 parent 6e7364f commit de86161

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

python/Count_Sort.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
def countingSort(array):
22
size = len(array)
3-
output = [0] * size
43

5-
count = [0] * 10
4+
output, count = [0] * size, [0] * 10
65

7-
for i in range(0, size):
6+
for i in range(size):
87
count[array[i]] += 1
98

109
for i in range(1, 10):
1110
count[i] += count[i - 1]
1211

13-
i = size - 1
14-
while i >= 0:
12+
for i in range(size - 1, -1, -1):
1513
output[count[array[i]] - 1] = array[i]
1614
count[array[i]] -= 1
17-
i -= 1
1815

19-
for i in range(0, size):
20-
array[i] = output[i]
16+
return output
2117

2218

2319
data = [4, 2, 2, 8, 3, 3, 1]
24-
countingSort(data)
25-
print("Sorted Array in Ascending Order: ")
26-
print(data)
20+
print(f"Sorted Array in Ascending Order: {countingSort(data)}")

0 commit comments

Comments
 (0)