Advertisement
colinm86

Untitled

Oct 13th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 KB | None | 0 0
  1. from queue import Queue
  2. '''
  3. Implement a radix sorting machine. A radix sort for base 10 integers is a mechanical sorting
  4. technique that utilizes a collection of bins, one main bin and 10 digit bins. Each bin acts
  5. like a queue and maintains its values in the order that they arrive. The algorithm begins by
  6. placing each number in the main bin. Then it considers each value digit by digit. The first value
  7. is removed and placed in a digit bin corresponding to the digit being considered. For example,
  8. if the ones digit is being considered, 534 is placed in digit bin 4 and 667 is placed in digit bin 7.
  9. Once all the values are placed in the corresponding digit bins, the values are collected from bin 0 to bin 9
  10. and placed back in the main bin. The process continues with the tens digit, the hundreds, and so on.
  11. After the last digit is processed, the main bin contains the values in order.
  12. '''
  13. class RadixSort:
  14.     def __init__(self,lst):
  15.         """[Initialized properties]
  16.        Arguments: lst {List} -- List of type int
  17.        """
  18.         if not isinstance(lst,list):
  19.             raise TypeError('must pass in a list')
  20.         for i in range(len(lst)):
  21.             if not isinstance(lst[i],int):
  22.                 raise TypeError('list must hold integers only')
  23.         self.__lst = self.list_int_to_str(lst)
  24.         self.__mainqueue = self.createMainQueue()
  25.         self.__binsqueue = self.createTenBinsInLst()
  26.         self.__maxnum = max(lst)
  27.        
  28.     @property
  29.     def lst(self):
  30.         return self.__lst
  31.    
  32.     @property
  33.     def maxnum(self):
  34.         return self.__maxnum
  35.  
  36.     @property
  37.     def mainqueue(self):
  38.         return self.__mainqueue
  39.  
  40.     @property
  41.     def binsqueue(self):
  42.         return self.__binsqueue
  43.  
  44.     @lst.setter
  45.     def lst(self,newlst):
  46.         self.__lst = newlst
  47.    
  48.     def getMaxNum(self):
  49.         """
  50.        Returns: {Integer} -- returns maxnum property
  51.        """
  52.         return self.maxnum
  53.  
  54.    
  55.     def list_int_to_str(self,lst):
  56.         """converts list of int to list of string
  57.        Arguments: lst {List} -- List of integers
  58.        Returns: {List} -- List of strings
  59.        """
  60.         return [str(i) for i in lst]
  61.  
  62.     def list_str_to_int(self,lst):
  63.         """converts list of string to list of int
  64.        Arguments: lst {List} -- List of strings
  65.        Returns: {List} -- List of int
  66.        """
  67.         return [int(i) for i in lst]
  68.    
  69.     def createMainQueue(self):
  70.         """
  71.        Initializes mainqueue propert by placing each item from list in into it
  72.        Returns: {Queue}
  73.        """
  74.         q = Queue()
  75.         for num in self.lst:
  76.             q.enqueue(num)
  77.         return q
  78.  
  79.    
  80.     def createTenBinsInLst(self):
  81.         """
  82.        Create a list of Queues and initialize them with index 0 to 9
  83.        Returns: {List} - An empty queue object is in each index position
  84.        """
  85.         lst = []
  86.         for i in range(10):
  87.             lst.append(Queue())
  88.         return lst
  89.  
  90.     def sort(self):
  91.         """
  92.            Main function for class. Sorts list passed in and returns list in sorted order
  93.            Returns: {List} -- Sorted smallest to largest
  94.        """
  95.         for i in range(1, self.maxnum + 1):
  96.             already_used = []
  97.             while not self.mainqueue.isEmpty():
  98.                 num = self.mainqueue.dequeue()
  99.                 if i > len(num):
  100.                     already_used.append(num)
  101.                     continue
  102.                 current_que_num = int(num[len(num) - i])
  103.                 correct_bin = self.binsqueue[current_que_num]
  104.                 correct_bin.enqueue(num)
  105.             for used in already_used:
  106.                 self.mainqueue.enqueue(used)
  107.             for i in range(len(self.binsqueue)):
  108.                 while not self.binsqueue[i].isEmpty():
  109.                     self.mainqueue.enqueue(self.binsqueue[i].dequeue())
  110.         sorted_list = []
  111.         while not self.mainqueue.isEmpty():
  112.             sorted_list.append(self.mainqueue.dequeue())
  113.         return self.list_str_to_int(sorted_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement