Advertisement
brainuser5705

radix sort

Apr 21st, 2021
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import randrange
  2.  
  3. zero = []
  4. one = []
  5. two = []
  6. three = []
  7. four = []
  8. five = []
  9. six = []
  10. seven = []
  11. eight = []
  12. nine = []
  13.  
  14. array = []
  15. decimal_place = 0
  16.  
  17. def generate_random_array(size, bound):
  18.     global array
  19.     for i in range(size):
  20.         array.append(randrange(1,bound))
  21.  
  22. def get_digit(num):
  23.     global decimal_place
  24.     return (num//pow(10,decimal_place)) % 10
  25.  
  26. def separate():
  27.     global array, decimal_place
  28.     for num in array:
  29.         digit = get_digit(num)
  30.    
  31.         if digit == 0:
  32.             zero.append(num)
  33.         elif digit == 1:
  34.             one.append(num)
  35.         elif digit == 2:
  36.             two.append(num)
  37.         elif digit == 3:
  38.             three.append(num)
  39.         elif digit == 4:
  40.             four.append(num)
  41.         elif digit == 5:
  42.             five.append(num)
  43.         elif digit == 6:
  44.             six.append(num)
  45.         elif digit == 7:
  46.             seven.append(num)
  47.         elif digit == 8:
  48.             eight.append(num)
  49.         elif digit == 9:
  50.             nine.append(num)
  51.  
  52.     array = [] # reset
  53.  
  54. def rebuild():
  55.     global zero, one, two, three, four, five, six, seven, eight, nine, array
  56.     array = zero + one + two + three + four + five + six + seven + eight + nine
  57.     if (len(zero) == len(array)):
  58.         return True
  59.     zero = []
  60.     one = []
  61.     two = []
  62.     zero = []
  63.     three = []
  64.     four = []
  65.     five = []
  66.     six = []
  67.     seven = []
  68.     eight = []
  69.     nine = []
  70.     return False
  71.  
  72. def solve():
  73.     global array, decimal_place
  74.     while(True):
  75.         separate()
  76.         is_done = rebuild()
  77.         if (is_done):
  78.             break
  79.         else:
  80.             decimal_place += 1
  81.  
  82. generate_random_array(20, 1000)
  83. solve()
  84. print(array)
  85.  
  86.        
  87.  
  88.  
Advertisement
RAW Paste Data Copied
Advertisement