Skip to content

Commit 6c128f5

Browse files
committed
move pigeon hole to sort folder
1 parent 9d24c9f commit 6c128f5

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
"""Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists of elements
4+
where the number of elements (n) and the length of the range of possible key values (N) are approximately the same"""
5+
6+
# Time complexity O(n + N)
7+
# Space complexity O(n)
8+
9+
def pigeonhole_sort(list):
10+
min_elem = min(list)
11+
max_elem = max(list)
12+
size = max_elem - min_elem + 1
13+
holes = [0] * size # auxiliary list of pigeonholes
14+
for x in list: # Populate the pigeonholes.
15+
holes[x - min_elem] += 1
16+
i = 0
17+
for count in range(size): # Put the elements back into the array in order.
18+
while holes[count] > 0:
19+
holes[count] -= 1
20+
list[i] = count + min_elem
21+
i += 1
22+
return list
23+
24+
25+
list = [10,2,7,9,4,5,3]
26+
print pigeonhole_sort(list)
27+
28+

Pigeonhole/pigeon_hole.py

-14
This file was deleted.

0 commit comments

Comments
 (0)