forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pigeonhole Sort implementation, As well as debugged addition.py file
- Loading branch information
1 parent
e706727
commit 19d67c2
Showing
3 changed files
with
30 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |