From 06353e993067fee5cf3d8d23038c51c6cd9fe584 Mon Sep 17 00:00:00 2001 From: Akash19304 <123761006+Akash19304@users.noreply.github.com> Date: Mon, 6 Nov 2023 23:24:34 +0530 Subject: [PATCH] Added Pigeonhole Sort --- pigeonhole_sort.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 pigeonhole_sort.py diff --git a/pigeonhole_sort.py b/pigeonhole_sort.py new file mode 100644 index 00000000..fe55d225 --- /dev/null +++ b/pigeonhole_sort.py @@ -0,0 +1,26 @@ +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 =" ")