Skip to content
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

Merged
merged 9 commits into from
Sep 30, 2020
Merged

Conversation

seshasaisuhashdesu
Copy link
Contributor

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

@codecov
Copy link

codecov bot commented Sep 28, 2020

Codecov Report

Merging #300 into master will increase coverage by 0.017%.
The diff coverage is 100.000%.

@@              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               
Impacted Files Coverage Δ
pydatastructs/linear_data_structures/__init__.py 100.000% <ø> (ø)
pydatastructs/linear_data_structures/algorithms.py 99.447% <100.000%> (+0.172%) ⬆️

Impacted file tree graph

Comment on lines 495 to 498
'''
Find maximum value in the list and use length of the list to determine which value in the list
goes into which bucket
'''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'''
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Member

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.

Copy link
Contributor Author

@seshasaisuhashdesu seshasaisuhashdesu Sep 29, 2020

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)

Copy link
Member

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.

Copy link
Contributor Author

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.

Comment on lines 93 to 106

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Member

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,

def test_heapsort():
_test_common_sort(heapsort)

def test_bucket_sort():
    _test_common_sort(bucket_sort)

@czgdp1807 czgdp1807 merged commit 7138dc4 into codezonediitj:master Sep 30, 2020
@czgdp1807
Copy link
Member

Thanks for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Bucket Sort
2 participants