From c3b2209a9782ba4adba93db07ee40a20f569d4ca Mon Sep 17 00:00:00 2001 From: Dwij Sukeshkumar Sheth Date: Wed, 2 Oct 2019 10:07:58 +0530 Subject: [PATCH 1/5] Added Bucket Sorting Algorithm --- allalgorithms/sorting/bucket_sort.py | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 allalgorithms/sorting/bucket_sort.py diff --git a/allalgorithms/sorting/bucket_sort.py b/allalgorithms/sorting/bucket_sort.py new file mode 100644 index 0000000..be00d2c --- /dev/null +++ b/allalgorithms/sorting/bucket_sort.py @@ -0,0 +1,46 @@ +# -*- coding: UTF-8 -*- +# +# Bucket Sort Algorithm +# The All â–²lgorithms library for python +# +# Contributed by: Dwij Sukeshkumar Sheth +# Github: @dwij2812 +# + +# Python3 program to sort an array +# using bucket sort +def insertionSort(b): + for i in range(1, len(b)): + up = b[i] + j = i - 1 + while j >=0 and b[j] > up: + b[j + 1] = b[j] + j -= 1 + b[j + 1] = up + return b + +def bucketSort(x): + arr = [] + slot_num = 10 # 10 means 10 slots, each + # slot's size is 0.1 + for i in range(slot_num): + arr.append([]) + + # Put array elements in different buckets + for j in x: + index_b = int(slot_num * j) + arr[index_b].append(j) + + # Sort individual buckets + for i in range(slot_num): + arr[i] = insertionSort(arr[i]) + + # concatenate the result + k = 0 + for i in range(slot_num): + for j in range(len(arr[i])): + x[k] = arr[i][j] + k += 1 + return x + +# For using this make the function call bucketSort(data) where data is the array of the numbers to be sorted. From c398c504e9dae26ac01839b129e6bc7ede70d4b0 Mon Sep 17 00:00:00 2001 From: Dwij Sukeshkumar Sheth Date: Thu, 3 Oct 2019 08:51:41 +0530 Subject: [PATCH 2/5] Added Tests --- tests/test_sorting.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 127f341..b97dbc5 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -8,7 +8,8 @@ pigeonhole_sort, stooge_sort, cocktail_shaker_sort, - tree_sort + tree_sort, + bucket_sort ) @@ -36,6 +37,9 @@ def test_cocktail_shaker_sort(self): def tree_sort(self): self.assertEqual([-44, 1, 2, 3, 7, 19], tree_sort([7, 3, 2, 19, -44, 1])) + + def bucket_sort(self): + self.assertEqual([-44, 1, 2, 3, 7, 19], bucket_sort([7, 3, 2, 19, -44, 1])) if __name__ == "__main__": From e2b18e7ce422ee428e97885caab7a3bb498f90ae Mon Sep 17 00:00:00 2001 From: Dwij Sukeshkumar Sheth Date: Thu, 3 Oct 2019 08:56:34 +0530 Subject: [PATCH 3/5] Update __init__.py --- allalgorithms/sorting/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/allalgorithms/sorting/__init__.py b/allalgorithms/sorting/__init__.py index 17da872..d70adf7 100644 --- a/allalgorithms/sorting/__init__.py +++ b/allalgorithms/sorting/__init__.py @@ -6,3 +6,4 @@ from .stooge_sort import stooge_sort from .cocktail_shaker_sort import cocktail_shaker_sort from .tree_sort import tree_sort +from .bucket_sort import bucket_sort From e128416e1214c079a148272779529c447767e24f Mon Sep 17 00:00:00 2001 From: Dwij Sukeshkumar Sheth Date: Thu, 3 Oct 2019 08:56:55 +0530 Subject: [PATCH 4/5] Update bucket_sort.py --- allalgorithms/sorting/bucket_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/allalgorithms/sorting/bucket_sort.py b/allalgorithms/sorting/bucket_sort.py index be00d2c..239f9ce 100644 --- a/allalgorithms/sorting/bucket_sort.py +++ b/allalgorithms/sorting/bucket_sort.py @@ -19,7 +19,7 @@ def insertionSort(b): b[j + 1] = up return b -def bucketSort(x): +def bucket_sort(x): arr = [] slot_num = 10 # 10 means 10 slots, each # slot's size is 0.1 @@ -43,4 +43,4 @@ def bucketSort(x): k += 1 return x -# For using this make the function call bucketSort(data) where data is the array of the numbers to be sorted. +# For using this make the function call bucket_sort(data) where data is the array of the numbers to be sorted. From 27331190d6d19e5d64c17bbad47b146d7da40d8f Mon Sep 17 00:00:00 2001 From: Dwij Sukeshkumar Sheth Date: Thu, 3 Oct 2019 09:01:41 +0530 Subject: [PATCH 5/5] Added Documentation for Bucket Sort --- docs/sorting/bucket-sort.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 docs/sorting/bucket-sort.md diff --git a/docs/sorting/bucket-sort.md b/docs/sorting/bucket-sort.md new file mode 100644 index 0000000..9676620 --- /dev/null +++ b/docs/sorting/bucket-sort.md @@ -0,0 +1,32 @@ +# Bucket Sort + +Bucket sort is a comparison sort algorithm that operates on elements by dividing them into different buckets and then sorting these buckets individually. Each bucket is sorted individually using a separate sorting algorithm or by applying the bucket sort algorithm recursively. Bucket sort is mainly useful when the input is uniformly distributed over a range. + +Assume one has the following problem in front of them: + +One has been given a large array of floating point integers lying uniformly between the lower and upper bound. This array now needs to be sorted. A simple way to solve this problem would be to use another sorting algorithm such as Merge sort, Heap Sort or Quick Sort. However, these algorithms guarantee a best case time complexity of O(NlogN). However, using bucket sort, the above task can be completed in O(N) time. + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.sorting import bucket_sort + +arr = [77, 2, 10, -2, 1, 7] + +print(bucket_sort(arr)) +# -> [-2, 1, 2, 7, 10, 77] +``` + +## API + +### bucket_sort(array) + +> Returns a sorted array + +##### Params: + +- `array`: Unsorted Array