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

Commit c5de395

Browse files
byteninjaa0MaximSmolskiy
andauthored
Add new test for bucket sort algorithm in sorts/bucket_sort.py (#11605)
* Add new test for bucket sort algorithm in sorts/bucket_sort.py * Update fractional_knapsack.py * Update matrix_class.py * Update bucket_sort.py * Update bucket_sort.py --------- Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
1 parent 488f143 commit c5de395

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎sorts/bucket_sort.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,35 @@ def bucket_sort(my_list: list, bucket_count: int = 10) -> list:
5151
>>> collection = random.sample(range(-50, 50), 50)
5252
>>> bucket_sort(collection) == sorted(collection)
5353
True
54+
>>> data = [1, 2, 2, 1, 1, 3]
55+
>>> bucket_sort(data) == sorted(data)
56+
True
57+
>>> data = [5, 5, 5, 5, 5]
58+
>>> bucket_sort(data) == sorted(data)
59+
True
60+
>>> data = [1000, -1000, 500, -500, 0]
61+
>>> bucket_sort(data) == sorted(data)
62+
True
63+
>>> data = [5.5, 2.2, -1.1, 3.3, 0.0]
64+
>>> bucket_sort(data) == sorted(data)
65+
True
66+
>>> bucket_sort([1]) == [1]
67+
True
68+
>>> data = [-1.1, -1.5, -3.4, 2.5, 3.6, -3.3]
69+
>>> bucket_sort(data) == sorted(data)
70+
True
71+
>>> data = [9, 2, 7, 1, 5]
72+
>>> bucket_sort(data) == sorted(data)
73+
True
5474
"""
5575

5676
if len(my_list) == 0 or bucket_count <= 0:
5777
return []
5878

5979
min_value, max_value = min(my_list), max(my_list)
80+
if min_value == max_value:
81+
return my_list
82+
6083
bucket_size = (max_value - min_value) / bucket_count
6184
buckets: list[list] = [[] for _ in range(bucket_count)]
6285

@@ -73,3 +96,6 @@ def bucket_sort(my_list: list, bucket_count: int = 10) -> list:
7396
testmod()
7497
assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
7598
assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
99+
assert bucket_sort([1.1, 1.2, -1.2, 0, 2.4]) == [-1.2, 0, 1.1, 1.2, 2.4]
100+
assert bucket_sort([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5]
101+
assert bucket_sort([-5, -1, -6, -2]) == [-6, -5, -2, -1]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /