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

Simplify the code #821

Merged
merged 1 commit into from
May 19, 2019
Merged
Changes from all 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
64 changes: 27 additions & 37 deletions sorts/bucket_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,35 @@
# Time Complexity of Solution:
# Best Case O(n); Average Case O(n); Worst Case O(n)

from __future__ import print_function
from insertion_sort import insertion_sort
import math

DEFAULT_BUCKET_SIZE = 5

def bucketSort(myList, bucketSize=DEFAULT_BUCKET_SIZE):
if(len(myList) == 0):
print('You don\'t have any elements in array!')

minValue = myList[0]
maxValue = myList[0]

# For finding minimum and maximum values
for i in range(0, len(myList)):
if myList[i] < minValue:
minValue = myList[i]
elif myList[i] > maxValue:
maxValue = myList[i]

# Initialize buckets
bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1
buckets = []
for i in range(0, bucketCount):
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!")


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

# For putting values in buckets
for i in range(0, len(myList)):
buckets[math.floor((myList[i] - minValue) / bucketSize)].append(myList[i])

# Sort buckets and place back into input array
sortedArray = []
for i in range(0, len(buckets)):
insertion_sort(buckets[i])
for j in range(0, len(buckets[i])):
sortedArray.append(buckets[i][j])

return sortedArray

if __name__ == '__main__':
sortedArray = bucketSort([12, 23, 4, 5, 3, 2, 12, 81, 56, 95])
print(sortedArray)
#test
#besd 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))