Skip to content

Commit 19d67c2

Browse files
committed
souravjain540#402-Pigeonhole Sort
Add Pigeonhole Sort implementation, As well as debugged addition.py file
1 parent e706727 commit 19d67c2

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: pigeonhole_sort","url":"/Users/dhirajmarathe/Documents/GitHub/Basic-Python-Programs/pigeonhole_sort.py","tests":[{"id":1707050869996,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"/Users/dhirajmarathe/Documents/GitHub/Basic-Python-Programs/pigeonhole_sort.py","group":"local","local":true}

addition.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
num1 = int(input("Enter First Number: ")
2-
num2 = int(input("Enter Second Number: ")
1+
num1 = int(input("Enter First Number: "))
2+
num2 = int(input("Enter Second Number: "))
33

44
sum = num1+num2
55

6-
print(""Sum of {0} and {1} is {2}" .format(num1, num2, sum))
6+
print("Sum of {0} and {1} is {2}" .format(num1, num2, sum))

pigeonhole_sort.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def pigeonhole_sort(arr):
2+
# Find the minimum and maximum values in the array
3+
min_val, max_val = min(arr), max(arr)
4+
5+
# Calculate the range of values
6+
range_size = max_val - min_val + 1
7+
8+
# Create pigeonholes (an array to store counts of each element)
9+
pigeonholes = [0] * range_size
10+
11+
# Populate pigeonholes with counts of elements
12+
for num in arr:
13+
pigeonholes[num - min_val] += 1
14+
15+
# Reconstruct the sorted array
16+
sorted_arr = []
17+
for i in range(range_size):
18+
sorted_arr.extend([i + min_val] * pigeonholes[i])
19+
20+
return sorted_arr
21+
22+
# Example usage:
23+
arr = [3, 8, 2, 4, 5, 6, 1, 7]
24+
sorted_arr = pigeonhole_sort(arr)
25+
print("Original Array:", arr)
26+
print("Sorted Array:", sorted_arr)

0 commit comments

Comments
 (0)