From 3cacfd919d3a98839fc1d96ec88696ff57ec0c4c Mon Sep 17 00:00:00 2001 From: Lakshya Tyagi Date: Fri, 29 Oct 2021 10:07:40 +0530 Subject: [PATCH] Create radix_sort.py --- sorting/radix_sort.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sorting/radix_sort.py diff --git a/sorting/radix_sort.py b/sorting/radix_sort.py new file mode 100644 index 0000000..e5d71eb --- /dev/null +++ b/sorting/radix_sort.py @@ -0,0 +1,28 @@ +def countingSort(arr, exp1): + n = len(arr) + output = [0] * (n) + count = [0] * (10) + for i in range(0, n): + index = (arr[i]/exp1) + count[int((index)%10)] += 1 + for i in range(1,10): + count[i] += count[i-1] + 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 + i = 0 + for i in range(0,len(arr)): + arr[i] = output[i] +def radixSort(arr): + max1 = max(arr) + exp = 1 + while max1/exp > 0: + countingSort(arr,exp) + exp *= 10 +arr = [ 170, 45, 75, 90, 802, 24, 2, 66] +radixSort(arr) +for i in range(len(arr)): + print(arr[i]),