From 62f358c6768f7690e0f081fd1bcab904ee0eba7a Mon Sep 17 00:00:00 2001 From: "karanraul02@gmail.com" Date: Wed, 25 Oct 2023 18:55:45 +0530 Subject: [PATCH] added pigeonhole sort --- PigeonholeSort.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 PigeonholeSort.py diff --git a/PigeonholeSort.py b/PigeonholeSort.py new file mode 100644 index 00000000..fe41fcdc --- /dev/null +++ b/PigeonholeSort.py @@ -0,0 +1,27 @@ + +def pigeonhole_sort(a): + my_min = min(a) + my_max = max(a) + size = my_max - my_min + 1 + + holes = [0] * size + + for x in a: + assert type(x) is int, "integers only please" + holes[x - my_min] += 1 + + i = 0 + for count in range(size): + while holes[count] > 0: + holes[count] -= 1 + a[i] = count + my_min + i += 1 + + +a = [8, 3, 2, 7, 4, 6, 8] +print("Sorted order is : ", end =" ") + +pigeonhole_sort(a) + +for i in range(0, len(a)): + print(a[i], end =" ")