Skip to content

Commit

Permalink
souravjain540#402-Pigeonhole Sort
Browse files Browse the repository at this point in the history
Add Pigeonhole Sort implementation,
As well as debugged addition.py file
  • Loading branch information
dhiraj9087 committed Feb 4, 2024
1 parent e706727 commit 19d67c2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +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}
6 changes: 3 additions & 3 deletions addition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
num1 = int(input("Enter First Number: ")
num2 = int(input("Enter Second Number: ")
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))

sum = num1+num2

print(""Sum of {0} and {1} is {2}" .format(num1, num2, sum))
print("Sum of {0} and {1} is {2}" .format(num1, num2, sum))
26 changes: 26 additions & 0 deletions pigeonhole_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def pigeonhole_sort(arr):
# Find the minimum and maximum values in the array
min_val, max_val = min(arr), max(arr)

# Calculate the range of values
range_size = max_val - min_val + 1

# Create pigeonholes (an array to store counts of each element)
pigeonholes = [0] * range_size

# Populate pigeonholes with counts of elements
for num in arr:
pigeonholes[num - min_val] += 1

# Reconstruct the sorted array
sorted_arr = []
for i in range(range_size):
sorted_arr.extend([i + min_val] * pigeonholes[i])

return sorted_arr

# Example usage:
arr = [3, 8, 2, 4, 5, 6, 1, 7]
sorted_arr = pigeonhole_sort(arr)
print("Original Array:", arr)
print("Sorted Array:", sorted_arr)

0 comments on commit 19d67c2

Please sign in to comment.