From e823c55f62c8aa1d72ec3bf2b58288b3dd413561 Mon Sep 17 00:00:00 2001 From: James Mc Dermott Date: Sat, 25 Feb 2017 15:06:58 +0000 Subject: [PATCH] Create radix_sort.py --- sorts/radix_sort.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 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 000000000..82f8a38b4 --- /dev/null +++ b/sorts/radix_sort.py @@ -0,0 +1,27 @@ +def radixsort(lst): + RADIX = 10 + maxLength = False + tmp , placement = -1, 1 + + while not maxLength: + maxLength = True + # declare and initialize buckets + buckets = [list() for _ in range( RADIX )] + + # split lst between lists + for i in lst: + tmp = i / placement + buckets[tmp % RADIX].append( i ) + if maxLength and tmp > 0: + maxLength = False + + # empty lists into lst array + a = 0 + for b in range( RADIX ): + buck = buckets[b] + for i in buck: + lst[a] = i + a += 1 + + # move to next + placement *= RADIX