File tree 2 files changed +28
-14
lines changed
Arrays-Sorting/src/Pigeon_hole
2 files changed +28
-14
lines changed Original file line number Diff line number Diff line change
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
+
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments