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 diff --git a/allalgorithms/sorting/bucket_sort.py b/allalgorithms/sorting/bucket_sort.py new file mode 100644 index 0000000..239f9ce --- /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 bucket_sort(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 bucket_sort(data) where data is the array of the numbers to be sorted. 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 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__":