Skip to content

Commit

Permalink
Merge pull request #503 from priyanshu-28/master
Browse files Browse the repository at this point in the history
Radix.sort
  • Loading branch information
fineanmol authored Oct 3, 2021
2 parents ea96841 + 4bbabec commit b28294e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions Contributors.html
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@
<a class="box-item" href="https://github.com/RAJASETHI"><span>Raja Sethi</span></a>
<a class="box-item" href="https://github.com/PrajaktaSathe"><span>Prajakta Sathe</span></a>
<a class="box-item" href="https://github.com/Kaustubh251002"><span>Kaustubh Mishra</span></a>
<a class="box-item" href="https://github.com/priyanshu-28"><span>Priyanshu Pathak</span></a>



Expand Down
76 changes: 76 additions & 0 deletions Program's_Contributed_By_Contributors/Python_Programs/radixsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Implementation of the Queue ADT using a linked list.
class lQueue :

# Creates an empty queue.
def __init__( self ):
self._qhead = None
self._qtail = None
self._count = 0

# Returns True if the queue is empty.
def isEmpty( self ):
return self._qhead is None

# Returns the number of items in the queue.
def __len__( self ):
return self._count

# Adds the given item to the queue.
def enqueue( self, item ):
node = _QueueNode( item )

if self.isEmpty() :
self._qhead = node
else :
self._qtail.next = node

self._qtail = node
self._count += 1

# Removes and returns the first item in the queue.
def dequeue( self ):
assert not self.isEmpty(), "Cannot dequeue from an empty queue."
node = self._qhead

if self._qhead is self._qtail :
self._qtail = None

self._qhead = self._qhead.next
self._count -= 1
return node.item

# Private storage class for creating the linked list nodes.
class _QueueNode( object ):
def __init__( self, item ):
self.item = item
self.next = None

#### Sorts a sequence of positive integers using the radix sort algorithm.

def radixSort( intList, numDigits ):

# Create a list of queues to represent the bins.
binArray = [-1]*10
for k in range( 10 ):
binArray[k] = lQueue()

# The value of the current column.
column = 1

# Iterate over the number of digits in the largest value.
for d in range( numDigits ):
# Distribute the keys across the 10 bins.
for key in intList :
digit = (key // column) % 10
binArray[digit].enqueue( key )

# Gather the keys from the bins and place them back in intList.
i=0
for bin in binArray :
while not bin.isEmpty() :
intList[i] = bin.dequeue()
i += 1
# Advance to the next column value.
column *= 10

return(intList)

0 comments on commit b28294e

Please sign in to comment.