From 273bca1e3fe45ce11cacd08a726e71049721dfcd Mon Sep 17 00:00:00 2001 From: Lalit Chaudhary <86573888+CodeLalit007@users.noreply.github.com> Date: Wed, 5 Oct 2022 00:42:05 +0530 Subject: [PATCH] Create counting-sort.py --- algorithms/Python/sorting/counting-sort.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 algorithms/Python/sorting/counting-sort.py diff --git a/algorithms/Python/sorting/counting-sort.py b/algorithms/Python/sorting/counting-sort.py new file mode 100644 index 00000000..44f21455 --- /dev/null +++ b/algorithms/Python/sorting/counting-sort.py @@ -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)))