30 likes | 227 Views
Radix Sort Using Queues (Taken from Udi Manber’s Algorithms textbook, section 6.4.1) Algorithm Straight_Radix ( X, n, k ) ; Input: X ( an array of integers, each with k digits, in the range 1 to n) Output: X (the array in sorted order) begin
E N D
Radix Sort Using Queues (Taken from Udi Manber’s Algorithms textbook, section 6.4.1) Algorithm Straight_Radix ( X, n, k ) ; Input: X ( an array of integers, each with k digits, in the range 1 to n) Output: X (the array in sorted order) begin We assume that all elements are initially in a global queue GQ ; { We use GQ for simplicity; it can be implemented through X } for i := 1 to d do {d is the number of possible digit values; d = 10 in case of decimal numbers} initialize queue Q[i] to be empty ; for i := k downto 1 do while GQ is not empty do dequeue x from GQ ; d := the ith digit of x; insert x into Q[d]; for t := 1 to d do insert Q[t] into GQ; for i := 1 to n do dequeue X[i] from GQ end