-
Notifications
You must be signed in to change notification settings - Fork 272
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Bucket Sort #300
Added Bucket Sort #300
Conversation
Codecov Report
@@ Coverage Diff @@
## master #300 +/- ##
=============================================
+ Coverage 98.827% 98.845% +0.017%
=============================================
Files 25 25
Lines 2901 2944 +43
=============================================
+ Hits 2867 2910 +43
Misses 34 34
|
''' | ||
Find maximum value in the list and use length of the list to determine which value in the list | ||
goes into which bucket | ||
''' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
''' | |
Find maximum value in the list and use length of the list to determine which value in the list | |
goes into which bucket | |
''' | |
# Find maximum value in the list and use length of the list to determine which value in the list goes into which bucket |
max_value = array[i] | ||
|
||
|
||
size = max_value / count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size = max_value / count | |
size = max_value // count |
# Put list elements into different buckets based on the size | ||
for i in range(len(array)): | ||
if array[i] is not None: | ||
j = int(array[i] / size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
j = int(array[i] / size) | |
j = array[i] // size |
if j is not count: | ||
buckets_list[j].append(array[i]) | ||
else: | ||
buckets_list[count - 1].append(array[i]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buckets_list[count - 1].append(array[i]) | |
buckets_list[count-1].append(array[i]) |
final_output = [] | ||
for x in range(count): | ||
final_output = final_output + buckets_list[x] | ||
final_output = DynamicOneDimensionalArray(int, final_output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the return type of final_output
should be same as array
i.e., type(array) == type(final_output)
should be True
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think this would be correct here? final_output = type(array)(int, final_output)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to sort the array in-place using bucket sort algorithm? Please do so if that's possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, Bucketsort cannot be implemented in-place. This is because the buckets should be of variable sizes and these sizes cannot be known priorly.
|
||
def test_bucket_sort(): | ||
random.seed(1000) | ||
|
||
n = random.randint(10, 20) | ||
arr = DynamicOneDimensionalArray(int, 0) | ||
for _ in range(n): | ||
arr.append(random.randint(1, 1000)) | ||
for _ in range(n // 3): | ||
arr.delete(random.randint(0, n // 2)) | ||
|
||
expected_arr = [102, 134, 228, 247, 362, 373, 448, | ||
480, 548, 686, 688, 696, 779] | ||
assert bucket_sort(arr)._data == expected_arr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def test_bucket_sort(): | |
random.seed(1000) | |
n = random.randint(10, 20) | |
arr = DynamicOneDimensionalArray(int, 0) | |
for _ in range(n): | |
arr.append(random.randint(1, 1000)) | |
for _ in range(n // 3): | |
arr.delete(random.randint(0, n // 2)) | |
expected_arr = [102, 134, 228, 247, 362, 373, 448, | |
480, 548, 686, 688, 696, 779] | |
assert bucket_sort(arr)._data == expected_arr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of the above add one line below the following,
pydatastructs/pydatastructs/linear_data_structures/tests/test_algorithms.py
Lines 53 to 54 in 87c7a80
def test_heapsort(): | |
_test_common_sort(heapsort) |
def test_bucket_sort():
_test_common_sort(bucket_sort)
Thanks for your contributions. |
References to other Issues or PRs or Relevant literature
Fixes #291
Brief description of what is fixed or changed
Bucket Sort is added with the buckets sorter as insertion sort