Skip to content

Commit b0aab7b

Browse files
wuyudistokhos
wuyudi
authored andcommitted
Update pigeon_sort.py (TheAlgorithms#2359)
* Update pigeon_sort.py * Update pigeon_sort.py * Update pigeon_sort.py
1 parent acf1bf3 commit b0aab7b

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

sorts/pigeon_sort.py

+9-19
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,17 @@ def pigeon_sort(array):
2626
if len(array) == 0:
2727
return array
2828

29-
# Manually finds the minimum and maximum of the array.
30-
min = array[0]
31-
max = array[0]
32-
33-
for i in range(len(array)):
34-
if array[i] < min:
35-
min = array[i]
36-
elif array[i] > max:
37-
max = array[i]
29+
_min, _max = min(array), max(array)
3830

3931
# Compute the variables
40-
holes_range = max - min + 1
41-
holes = [0 for _ in range(holes_range)]
42-
holes_repeat = [0 for _ in range(holes_range)]
32+
holes_range = _max - _min + 1
33+
holes, holes_repeat = [0] * holes_range, [0] * holes_range
4334

4435
# Make the sorting.
45-
for i in range(len(array)):
46-
index = array[i] - min
47-
if holes[index] != array[i]:
48-
holes[index] = array[i]
49-
holes_repeat[index] += 1
50-
else:
51-
holes_repeat[index] += 1
36+
for i in array:
37+
index = i - _min
38+
holes[index] = i
39+
holes_repeat[index] += 1
5240

5341
# Makes the array back by replacing the numbers.
5442
index = 0
@@ -63,6 +51,8 @@ def pigeon_sort(array):
6351

6452

6553
if __name__ == "__main__":
54+
import doctest
55+
doctest.testmod()
6656
user_input = input("Enter numbers separated by comma:\n")
6757
unsorted = [int(x) for x in user_input.split(",")]
6858
print(pigeon_sort(unsorted))

0 commit comments

Comments
 (0)