From 083f657b545e66582895ca074ea73e5a06f885f0 Mon Sep 17 00:00:00 2001 From: MrWeast Date: Fri, 1 Dec 2023 15:44:54 -0500 Subject: [PATCH 1/4] Create radix_sort.py added python implementation of radix sort --- src/python/radix_sort.py | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/python/radix_sort.py diff --git a/src/python/radix_sort.py b/src/python/radix_sort.py new file mode 100644 index 00000000..0d887226 --- /dev/null +++ b/src/python/radix_sort.py @@ -0,0 +1,43 @@ +def radix_sort(arr): + # Find the maximum number to know the number of digits + max_num = max(arr) + + # Do counting sort for every digit, starting from the least significant digit + exp = 1 + while max_num // exp > 0: + counting_sort(arr, exp) + exp *= 10 + + +def counting_sort(arr, exp): + n = len(arr) + output = [0] * n + count = [0] * 10 + + for i in range(n): + index = arr[i] // exp + count[index % 10] += 1 + + for i in range(1, 10): + count[i] += count[i - 1] + + i = n - 1 + while i >= 0: + index = arr[i] // exp + output[count[index % 10] - 1] = arr[i] + count[index % 10] -= 1 + i -= 1 + + for i in range(n): + arr[i] = output[i] + + +def main(): + arr = [170, 2, 45, 75, 75, 90, 802, 24, 2, 66] + print("Unsorted array:", arr) + radix_sort(arr) + print("Sorted array:", arr) + + +if __name__ == "__main__": + main() From dedbe8878df52d3b7e33bf5a96461eec8f43a23a Mon Sep 17 00:00:00 2001 From: MrWeast Date: Fri, 1 Dec 2023 15:46:21 -0500 Subject: [PATCH 2/4] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9def01ab..d2859132 100644 --- a/README.md +++ b/README.md @@ -3276,8 +3276,8 @@ In order to achieve greater coverage and encourage more people to contribute to - - + + From 9f03e18184bb13c8480f4371963efb0469e8717f Mon Sep 17 00:00:00 2001 From: MrWeast Date: Fri, 1 Dec 2023 15:50:50 -0500 Subject: [PATCH 3/4] Update radix_sort.py added a random array to test the sort --- src/python/radix_sort.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/python/radix_sort.py b/src/python/radix_sort.py index 0d887226..b15dd4a2 100644 --- a/src/python/radix_sort.py +++ b/src/python/radix_sort.py @@ -1,3 +1,5 @@ +import random + def radix_sort(arr): # Find the maximum number to know the number of digits max_num = max(arr) @@ -33,11 +35,23 @@ def counting_sort(arr, exp): def main(): + print("Fixed Testing Array") arr = [170, 2, 45, 75, 75, 90, 802, 24, 2, 66] print("Unsorted array:", arr) radix_sort(arr) print("Sorted array:", arr) + print("Random Testing Array") + arr = [] + for i in range(0,10): + arr.append(random.randint(0,20)) + print("Unsorted array:", arr) + radix_sort(arr) + print("Sorted array:", arr) + + + + if __name__ == "__main__": main() From 506ae678aa52cf8380bda1c28778b80128251992 Mon Sep 17 00:00:00 2001 From: MrWeast Date: Fri, 1 Dec 2023 15:58:41 -0500 Subject: [PATCH 4/4] black formatting fixed format --- src/python/radix_sort.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/python/radix_sort.py b/src/python/radix_sort.py index b15dd4a2..fc91c414 100644 --- a/src/python/radix_sort.py +++ b/src/python/radix_sort.py @@ -1,5 +1,6 @@ import random + def radix_sort(arr): # Find the maximum number to know the number of digits max_num = max(arr) @@ -43,15 +44,12 @@ def main(): print("Random Testing Array") arr = [] - for i in range(0,10): - arr.append(random.randint(0,20)) + for i in range(0, 10): + arr.append(random.randint(0, 20)) print("Unsorted array:", arr) radix_sort(arr) print("Sorted array:", arr) - - - if __name__ == "__main__": main()