Skip to content

Commit 4946198

Browse files
Create pigeonhole_sort.py
Based on the famous PigeonHole Principle
1 parent 2d34fc1 commit 4946198

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
#know what is Pigeonhole_principle
3+
# https://www.youtube.com/watch?v=IeTLZPNIPJQ
4+
5+
6+
def pigeonhole_sort(a):
7+
8+
# (number of pigeonholes we need)
9+
my_min = min(a)
10+
my_max = max(a)
11+
size = my_max - my_min + 1
12+
13+
# total pigeonholes
14+
holes = [0] * size
15+
16+
# filling up the pigeonholes.
17+
for x in a:
18+
holes[x - my_min] += 1
19+
20+
# Put the elements back into the array in order.
21+
i = 0
22+
for count in range(size):
23+
while holes[count] > 0:
24+
holes[count] -= 1
25+
a[i] = count + my_min
26+
i += 1
27+
28+
29+
30+
31+
a = [10, 3, 2, 7, 4, 6, 8]
32+
33+
#list only integers
34+
print(pigeonhole_sort(a) )
35+
print(a)
36+
37+

0 commit comments

Comments
 (0)