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

fix empty list validation and code data structures #826

Merged
merged 2 commits into from
May 22, 2019
Merged
Changes from 1 commit
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
43 changes: 15 additions & 28 deletions sorts/bucket_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,25 @@
# and therefore can also be considered a comparison sort algorithm. The computational complexity estimates
# involve the number of buckets.

# Time Complexity of Solution:
# Best Case O(n); Average Case O(n); Worst Case O(n)
# Time Complexity of Solution:
# Best Case O(n); Average Case O(n); Worst Case O(n)

DEFAULT_BUCKET_SIZE=5
def bucket_sort(my_list,bucket_size=DEFAULT_BUCKET_SIZE):
if(my_list==0):
print("you don't have any elements in array!")
def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE):
if len(my_list) == 0:
raise Exception("you don't have any elements in array!")
Copy link
Member

Choose a reason for hiding this comment

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

Add more intuitive message in the raised Exception:

Please add some elements in the array.


min_value, max_value = (min(my_list), max(my_list))
bucket_count = ((max_value-min_value) // bucket_size+1)
buckets = [[] for _ in range(bucket_count)]

min_value=min(my_list)
max_value=max(my_list)

bucket_count=(max_value-min_value)//bucket_size+1
buckets=[]
for i in range(bucket_count):
buckets.append([])
for i in range(len(my_list)):
buckets[(my_list[i]-min_value)//bucket_size].append(my_list[i])


sorted_array=[]
for i in range(len(buckets)):
buckets[i].sort()
for j in range(len(buckets[i])):
sorted_array.append(buckets[i][j])
return sorted_array


buckets[(my_list[i] - min_value) // bucket_size].append(my_list[i])

return sorted([buckets[i][j] for i in range(len(buckets))
for j in range(len(buckets[i]))])

#test
#best on python 3.7.3
user_input =input('Enter numbers separated by a comma:').strip()
unsorted =[int(item) for item in user_input.split(',')]
print(bucket_sort(unsorted))
if __name__ == "__main__":
user_input = input('Enter numbers separated by a comma:').strip()
unsorted = [int(n) for n in user_input.split(',') if len(user_input) > 0]
Copy link
Member

Choose a reason for hiding this comment

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

Should we add support for float numbers ? In my opinion, I think that we should ! 👍 💯

print(bucket_sort(unsorted))