forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheAlgorithms#165 from andrealmeid/master
Add counting sort
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
This is pure python implementation of counting sort algorithm | ||
For doctests run following command: | ||
python -m doctest -v counting_sort.py | ||
or | ||
python3 -m doctest -v counting_sort.py | ||
For manual testing run: | ||
python counting_sort.py | ||
""" | ||
|
||
from __future__ import print_function | ||
|
||
|
||
def counting_sort(collection): | ||
"""Pure implementation of counting sort algorithm in Python | ||
:param collection: some mutable ordered collection with heterogeneous | ||
comparable items inside | ||
:return: the same collection ordered by ascending | ||
Examples: | ||
>>> counting_sort([0, 5, 3, 2, 2]) | ||
[0, 2, 2, 3, 5] | ||
>>> counting_sort([]) | ||
[] | ||
>>> counting_sort([-2, -5, -45]) | ||
[-45, -5, -2] | ||
""" | ||
# if the collection is empty, returns empty | ||
if collection == []: | ||
return [] | ||
|
||
# get some information about the collection | ||
coll_len = len(collection) | ||
coll_max = max(collection) | ||
coll_min = min(collection) | ||
|
||
# create the counting array | ||
counting_arr_length = coll_max + 1 - coll_min | ||
counting_arr = [0] * counting_arr_length | ||
|
||
# count how much a number appears in the collection | ||
for number in collection: | ||
counting_arr[number - coll_min] += 1 | ||
|
||
# sum each position with it's predecessors. now, counting_arr[i] tells | ||
# us how many elements <= i has in the collection | ||
for i in range(1, counting_arr_length): | ||
counting_arr[i] = counting_arr[i] + counting_arr[i-1] | ||
|
||
# create the output collection | ||
ordered = [0] * coll_len | ||
|
||
# place the elements in the output, respecting the original order (stable | ||
# sort) from end to begin, updating counting_arr | ||
for i in reversed(range(0, coll_len)): | ||
ordered[counting_arr[collection[i] - coll_min]-1] = collection[i] | ||
counting_arr[collection[i] - coll_min] -= 1 | ||
|
||
return ordered | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin | ||
# otherwise 2.x's input builtin function is too "smart" | ||
if sys.version_info.major < 3: | ||
input_function = raw_input | ||
else: | ||
input_function = input | ||
|
||
user_input = input_function('Enter numbers separated by a comma:\n') | ||
unsorted = [int(item) for item in user_input.split(',')] | ||
print(counting_sort(unsorted)) |