Skip to content

Commit 5fd8183

Browse files
authored
Create Pigeonhole_Sort.py
1 parent 0afa310 commit 5fd8183

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Python program to implement Pigeonhole Sort
2+
3+
def pigeonhole_sort(a):
4+
# size of range of values in the list
5+
# (ie, number of pigeonholes we need)
6+
my_min = min(a)
7+
my_max = max(a)
8+
size = my_max - my_min + 1
9+
10+
# our list of pigeonholes
11+
holes = [0] * size
12+
13+
# Populate the pigeonholes.
14+
for x in a:
15+
assert type(x) is int, "integers only"
16+
holes[x - my_min] += 1
17+
18+
# Put the elements back into the array in order.
19+
i = 0
20+
for count in range(size):
21+
while holes[count] > 0:
22+
holes[count] -= 1
23+
a[i] = count + my_min
24+
i += 1
25+
26+
27+
a = [8, 1, 2, 7, 4, 5, 8]
28+
print("Sorted order is : ", end = ' ')
29+
30+
pigeonhole_sort(a)
31+
32+
for i in range(0, len(a)):
33+
print(a[i], end = ' ')

0 commit comments

Comments
 (0)