Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:master
base:master
Choose a base branch
Loading
fromdwij2812:master
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionsallalgorithms/sorting/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
46 changes: 46 additions & 0 deletionsallalgorithms/sorting/bucket_sort.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,8 @@
pigeonhole_sort,
stooge_sort,
cocktail_shaker_sort,
tree_sort
tree_sort,
bucket_sort
)


Expand DownExpand Up@@ -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__":
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp