- Notifications
You must be signed in to change notification settings - Fork25
Added Bucket Sorting Algorithm#32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
dwij2812 wants to merge5 commits intoabranhe:masterChoose a base branch fromdwij2812:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsallalgorithms/sorting/__init__.py
This file contains hidden or 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
46 changes: 46 additions & 0 deletionsallalgorithms/sorting/bucket_sort.py
This file contains hidden or 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,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. |
32 changes: 32 additions & 0 deletionsdocs/sorting/bucket-sort.md
This file contains hidden or 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,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 |
6 changes: 5 additions & 1 deletiontests/test_sorting.py
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.