Create counting-sort.py

pull/918/head
Lalit Chaudhary 2022-10-05 00:42:05 +05:30 committed by GitHub
parent 5d18a66cd8
commit 273bca1e3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions

View File

@ -0,0 +1,17 @@
def countSort(arr):
output = [0 for i in range(len(arr))]
count = [0 for i in range(256)]
ans = ["" for _ in arr]
for i in arr:
count[ord(i)] += 1
for i in range(256):
count[i] += count[i-1]
for i in range(len(arr)):
output[count[ord(arr[i])]-1] = arr[i]
count[ord(arr[i])] -= 1
for i in range(len(arr)):
ans[i] = output[i]
return ans
arr = "geeksforgeeks"
ans = countSort(arr)
print("Sorted character array is % s" %("".join(ans)))