From bf22fdf926783007710ea0e32767ff6e297f43f1 Mon Sep 17 00:00:00 2001 From: FrancoMLG Date: Thu, 6 Jun 2024 00:10:46 -0400 Subject: [PATCH] Addition of counting sort in Python --- Python/Sorting-Techniques/countingSort.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Python/Sorting-Techniques/countingSort.py diff --git a/Python/Sorting-Techniques/countingSort.py b/Python/Sorting-Techniques/countingSort.py new file mode 100644 index 00000000..504f7aec --- /dev/null +++ b/Python/Sorting-Techniques/countingSort.py @@ -0,0 +1,18 @@ +def countingSort(unsortedArr): + highest = 0 + for i in unsortedArr: + if i > highest: + highest = i + tempArr = [0] * (highest + 1) + for i in unsortedArr: + tempArr[i] += 1 + for i in range(len(tempArr)): + if i != 0: + tempArr[i] += tempArr[i - 1] + sortedArr = [0] * len(unsortedArr) + for i in unsortedArr: + sortedArr[tempArr[i] - 1] = i + tempArr[i] -= 1 + print(sortedArr) + +countingSort([4, 7, 9, 3, 1, 5, 2, 8, 0, 6]) \ No newline at end of file