forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpigeonhole_sort.py
More file actions
44 lines (33 loc) · 1005 Bytes
/
pigeonhole_sort.py
File metadata and controls
44 lines (33 loc) · 1005 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
37
38
39
40
41
42
43
44
"""
Pigeonhole Sort
Pigeonhole sort is suitable for sorting lists where the number of
elements and the range of possible key values are approximately equal.
Reference: https://en.wikipedia.org/wiki/Pigeonhole_sort
Complexity:
Time: O(n + range) best / O(n + range) average / O(n + range) worst
Space: O(range)
"""
from __future__ import annotations
def pigeonhole_sort(array: list[int]) -> list[int]:
"""Sort an array in ascending order using pigeonhole sort.
Args:
array: List of integers to sort.
Returns:
A sorted list.
Examples:
>>> pigeonhole_sort([3, 1, 2])
[1, 2, 3]
"""
max_value = max(array)
min_value = min(array)
size = max_value - min_value + 1
holes = [0] * size
for value in array:
holes[value - min_value] += 1
i = 0
for count in range(size):
while holes[count] > 0:
holes[count] -= 1
array[i] = count + min_value
i += 1
return array