forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpigeonholeSort.py
More file actions
36 lines (29 loc) · 839 Bytes
/
pigeonholeSort.py
File metadata and controls
36 lines (29 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def pigeonholeSort(a):
my_min = min(a)
my_max = max(a)
size = my_max - my_min + 1
# our list of pigeonholes
holes = [0] * size
# Populate the pigeonholes.
for x in a:
assert type(x) is int, "Input Only Integers"
holes[x - my_min] += 1
# Put the elements back into the array in order.
i = 0
for count in range(size):
while holes[count] > 0:
holes[count] -= 1
a[i] = count + my_min
i += 1
#Get The Size of The Array
print("Enter the Size of the Array :")
n=int(input())
a=[]
#Get The Elements of The Array
for i in range(0,n):
ele=int(input())
a.append(ele)
print("Pigeon Hole Sorted order is : ", end = ' ')
pigeonholeSort(a)
for i in range(0, len(a)):
print(a[i], end = ' ')