From 3dc3066b5a3dd3e0da39ead98be7237305bf85a3 Mon Sep 17 00:00:00 2001 From: SHAKTI SINGH Date: Mon, 1 Oct 2018 16:04:24 +0530 Subject: [PATCH 1/3] pigeonhole sorting in python --- sorts/pigeonhole_sort.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 sorts/pigeonhole_sort.py diff --git a/sorts/pigeonhole_sort.py b/sorts/pigeonhole_sort.py new file mode 100644 index 000000000000..774247d4d2e1 --- /dev/null +++ b/sorts/pigeonhole_sort.py @@ -0,0 +1,40 @@ +# Python program to implement Pigeonhole Sorting in python + +#Algorithm for the pigeonhole sorting + +def pigeonhole_sort(a): + # size of range of values in the list (ie, number of pigeonholes we need) + + min_val = min(a) # min()finds the minimum value + max_val = max(a) # max() finds the maximum value + + size = max_val - min_val + 1 # size is difference of max and min values plus one + + # list of pigeonholes of size equal to the variable size + holes = [0] * size + + # Populate the pigeonholes. + for x in a: + assert type(x) is int, "integers only please" + holes[x - my_min] += 1 + + # Putting the elements back into the array in an order. + i = 0 + for count in range(size): + while holes[count] > 0: + holes[count] -= 1 + a[i] = count + my_min + i += 1 + +def main(): + + 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 = ' ') + +if __name__ == '__main__': + main() From b3a3d2155a9e8c1d69e1356d0a4276760d79e516 Mon Sep 17 00:00:00 2001 From: SHAKTI SINGH Date: Mon, 1 Oct 2018 16:07:04 +0530 Subject: [PATCH 2/3] variable name update in pigeonhole_sort.py --- sorts/pigeonhole_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/pigeonhole_sort.py b/sorts/pigeonhole_sort.py index 774247d4d2e1..4990b28c89fe 100644 --- a/sorts/pigeonhole_sort.py +++ b/sorts/pigeonhole_sort.py @@ -16,14 +16,14 @@ def pigeonhole_sort(a): # Populate the pigeonholes. for x in a: assert type(x) is int, "integers only please" - holes[x - my_min] += 1 + holes[x - min_val] += 1 # Putting the elements back into the array in an order. i = 0 for count in range(size): while holes[count] > 0: holes[count] -= 1 - a[i] = count + my_min + a[i] = count + min_val i += 1 def main(): From 4d9f5471bc2ca92be3587634e7b7d6f99bc946d3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 6 Dec 2019 07:34:04 +0100 Subject: [PATCH 3/3] Add doctest --- sorts/pigeonhole_sort.py | 80 +++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/sorts/pigeonhole_sort.py b/sorts/pigeonhole_sort.py index 4990b28c89fe..d8eb61fc0e08 100644 --- a/sorts/pigeonhole_sort.py +++ b/sorts/pigeonhole_sort.py @@ -1,40 +1,44 @@ -# Python program to implement Pigeonhole Sorting in python - -#Algorithm for the pigeonhole sorting - -def pigeonhole_sort(a): - # size of range of values in the list (ie, number of pigeonholes we need) - - min_val = min(a) # min()finds the minimum value - max_val = max(a) # max() finds the maximum value - - size = max_val - min_val + 1 # size is difference of max and min values plus one - - # list of pigeonholes of size equal to the variable size - holes = [0] * size - - # Populate the pigeonholes. - for x in a: - assert type(x) is int, "integers only please" - holes[x - min_val] += 1 - - # Putting the elements back into the array in an order. - i = 0 - for count in range(size): - while holes[count] > 0: - holes[count] -= 1 - a[i] = count + min_val - i += 1 - +# Python program to implement Pigeonhole Sorting in python + +# Algorithm for the pigeonhole sorting + + +def pigeonhole_sort(a): + """ + >>> a = [8, 3, 2, 7, 4, 6, 8] + >>> b = sorted(a) # a nondestructive sort + >>> pigeonhole_sort(a) # a distructive sort + >>> a == b + True + """ + # size of range of values in the list (ie, number of pigeonholes we need) + + min_val = min(a) # min() finds the minimum value + max_val = max(a) # max() finds the maximum value + + size = max_val - min_val + 1 # size is difference of max and min values plus one + + # list of pigeonholes of size equal to the variable size + holes = [0] * size + + # Populate the pigeonholes. + for x in a: + assert isinstance(x, int), "integers only please" + holes[x - min_val] += 1 + + # Putting the elements back into the array in an order. + i = 0 + for count in range(size): + while holes[count] > 0: + holes[count] -= 1 + a[i] = count + min_val + i += 1 + + def main(): - - 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 = ' ') - -if __name__ == '__main__': + pigeonhole_sort([8, 3, 2, 7, 4, 6, 8]) + print("Sorted order is: ", " ", join(a)) + + +if __name__ == "__main__": main()