Added an implementation code in python (#33305)

This commit is contained in:
P Karthik Chowdary
2019-06-26 02:12:43 +05:30
committed by Randell Dawson
parent 7e2e9a61bc
commit 850512c2ab

View File

@@ -97,7 +97,38 @@ void radixsort(ll arr[],int n,int maxx){ //maxx is the maximum elemen
}
}
```
An implementation in python :
```
def counting_sort(arr, max_value, get_index):
counts = [0] * max_value
# Counting - O(n)
for a in arr:s
counts[get_index(a)] += 1
# Accumulating - O(k)
for i, c in enumerate(counts):
if i == 0:
continue
else:
counts[i] += counts[i-1]
# Calculating start index - O(k)
for i, c in enumerate(counts[:-1]):
if i == 0:
counts[i] = 0
counts[i+1] = c
ret = [None] * len(arr)
# Sorting - O(n)
for a in arr:
index = counts[get_index(a)]
ret[index] = a
counts[get_index(a)] += 1
return ret
```
### More Information: