Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions sorting/counting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

"""Python implementation of in-place counting sort algorithm"""

def countingsort(array, maxval):
n = len(array)
m = maxval + 1
# init with zeros
count = [0] * m
for a in array:
# count occurences
count[a] += 1
i = 0
for a in range(m):
# make 'count[a]' copies of 'a'
for c in range(count[a]):
array[i] = a
i += 1
return array

10 changes: 10 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@
print "Bucket Sort incorrect"
except:
print "Bucketsort function errored or is incomplete"

try:
from counting import countingsort
if(countingsort(list(nums), numpy.max(nums)) == sortedNums):
print "Counting Sort success!"
else:
print "Counting Sort incorrect."
except:
print "Countingsort function errored or is incomplete."