MCPcopy Create free account
hub / github.com/geekcomputers/Python / pigeonhole_sort

Function pigeonhole_sort

Sorting Algorithms/pigeonhole_sort.py:5–24  ·  view source on GitHub ↗
(a)

Source from the content-addressed store, hash-verified

3
4
5def pigeonhole_sort(a):
6 # (number of pigeonholes we need)
7 my_min = min(a)
8 my_max = max(a)
9 size = my_max - my_min + 1
10
11 # total pigeonholes
12 holes = [0] * size
13
14 # filling up the pigeonholes.
15 for x in a:
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
27a = [10, 3, 2, 7, 4, 6, 8]

Callers 1

pigeonhole_sort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected