From 3fc885f83882d6052fdae14f3a401f081e282a2c Mon Sep 17 00:00:00 2001 From: Kumar Abhishek Date: Sat, 19 Oct 2024 12:18:48 +0530 Subject: [PATCH 1/2] radix__sort.py --- sorts/radix__sort.py | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 sorts/radix__sort.py diff --git a/sorts/radix__sort.py b/sorts/radix__sort.py new file mode 100644 index 000000000000..a53334f58c4f --- /dev/null +++ b/sorts/radix__sort.py @@ -0,0 +1,57 @@ +# Python program for implementation of Radix Sort + +# A function to do counting sort of arr[] according to +# the digit represented by exp. +def counting_sort(arr, exp1): + n = len(arr) + + # The output array elements that will have sorted arr + output = [0] * (n) + + # initialize count array as 0 + count = [0] * (10) + + # Store count of occurrences in count[] + for i in range(0, n): + index = (arr[i]/exp1) + count[int((index)%10)] += 1 + + # Change count[i] so that count[i] now contains actual + # position of this digit in output array + for i in range(1,10): + count[i] += count[i-1] + + # Build the output array + i = n-1 + while i>=0: + index = (arr[i]/exp1) + output[ count[ int((index)%10) ] - 1] = arr[i] + count[int((index)%10)] -= 1 + i -= 1 + + # Copying the output array to arr[], + # so that arr now contains sorted numbers + i = 0 + for i in range(0,len(arr)): + arr[i] = output[i] + +# Method to do Radix Sort +def radix_sort(arr): + + # Find the maximum number to know number of digits + max1 = max(arr) + + # Do counting sort for every digit. Note that instead + # of passing digit number, exp is passed. exp is 10^i + # where i is current digit number + exp = 1 + while max1 // exp > 0: + counting_sort(arr,exp) + exp *= 10 + +# Driver code to test above +arr = [ 170, 45, 75, 90, 802, 24, 2, 66] +radix_sort(arr) + +for i in range(len(arr)): + print(arr[i],end=" ") From 2b5c43d4d0e533eae4c7a5c76ddf4c20e0d69aeb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Oct 2024 06:49:35 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/radix__sort.py | 95 ++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/sorts/radix__sort.py b/sorts/radix__sort.py index a53334f58c4f..db029824bc66 100644 --- a/sorts/radix__sort.py +++ b/sorts/radix__sort.py @@ -1,57 +1,58 @@ -# Python program for implementation of Radix Sort - -# A function to do counting sort of arr[] according to -# the digit represented by exp. -def counting_sort(arr, exp1): - n = len(arr) - - # The output array elements that will have sorted arr - output = [0] * (n) - - # initialize count array as 0 - count = [0] * (10) - - # Store count of occurrences in count[] - for i in range(0, n): - index = (arr[i]/exp1) - count[int((index)%10)] += 1 - - # Change count[i] so that count[i] now contains actual - # position of this digit in output array - for i in range(1,10): - count[i] += count[i-1] - - # Build the output array - i = n-1 - while i>=0: - index = (arr[i]/exp1) - output[ count[ int((index)%10) ] - 1] = arr[i] - count[int((index)%10)] -= 1 - i -= 1 - - # Copying the output array to arr[], - # so that arr now contains sorted numbers - i = 0 - for i in range(0,len(arr)): - arr[i] = output[i] +# Python program for implementation of Radix Sort + +# A function to do counting sort of arr[] according to +# the digit represented by exp. +def counting_sort(arr, exp1): + n = len(arr) + + # The output array elements that will have sorted arr + output = [0] * (n) + + # initialize count array as 0 + count = [0] * (10) + + # Store count of occurrences in count[] + for i in range(0, n): + index = arr[i] / exp1 + count[int((index) % 10)] += 1 + + # Change count[i] so that count[i] now contains actual + # position of this digit in output array + for i in range(1, 10): + count[i] += count[i - 1] + + # Build the output array + i = n - 1 + while i >= 0: + index = arr[i] / exp1 + output[count[int((index) % 10)] - 1] = arr[i] + count[int((index) % 10)] -= 1 + i -= 1 + + # Copying the output array to arr[], + # so that arr now contains sorted numbers + i = 0 + for i in range(0, len(arr)): + arr[i] = output[i] + # Method to do Radix Sort def radix_sort(arr): + # Find the maximum number to know number of digits + max1 = max(arr) - # Find the maximum number to know number of digits - max1 = max(arr) + # Do counting sort for every digit. Note that instead + # of passing digit number, exp is passed. exp is 10^i + # where i is current digit number + exp = 1 + while max1 // exp > 0: + counting_sort(arr, exp) + exp *= 10 - # Do counting sort for every digit. Note that instead - # of passing digit number, exp is passed. exp is 10^i - # where i is current digit number - exp = 1 - while max1 // exp > 0: - counting_sort(arr,exp) - exp *= 10 # Driver code to test above -arr = [ 170, 45, 75, 90, 802, 24, 2, 66] +arr = [170, 45, 75, 90, 802, 24, 2, 66] radix_sort(arr) for i in range(len(arr)): - print(arr[i],end=" ") + print(arr[i], end=" ")