mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
parent
b6c3fa8992
commit
f5abc04176
@ -13,45 +13,35 @@
|
|||||||
# Time Complexity of Solution:
|
# Time Complexity of Solution:
|
||||||
# Best Case O(n); Average Case O(n); Worst Case O(n)
|
# Best Case O(n); Average Case O(n); Worst Case O(n)
|
||||||
|
|
||||||
from __future__ import print_function
|
DEFAULT_BUCKET_SIZE=5
|
||||||
from insertion_sort import insertion_sort
|
def bucket_sort(my_list,bucket_size=DEFAULT_BUCKET_SIZE):
|
||||||
import math
|
if(my_list==0):
|
||||||
|
print("you don't have any elements in array!")
|
||||||
|
|
||||||
DEFAULT_BUCKET_SIZE = 5
|
|
||||||
|
|
||||||
def bucketSort(myList, bucketSize=DEFAULT_BUCKET_SIZE):
|
min_value=min(my_list)
|
||||||
if(len(myList) == 0):
|
max_value=max(my_list)
|
||||||
print('You don\'t have any elements in array!')
|
|
||||||
|
|
||||||
minValue = myList[0]
|
bucket_count=(max_value-min_value)//bucket_size+1
|
||||||
maxValue = myList[0]
|
buckets=[]
|
||||||
|
for i in range(bucket_count):
|
||||||
# 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):
|
|
||||||
buckets.append([])
|
buckets.append([])
|
||||||
|
for i in range(len(my_list)):
|
||||||
|
buckets[(my_list[i]-min_value)//bucket_size].append(my_list[i])
|
||||||
|
|
||||||
# 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
|
sorted_array=[]
|
||||||
sortedArray = []
|
for i in range(len(buckets)):
|
||||||
for i in range(0, len(buckets)):
|
buckets[i].sort()
|
||||||
insertion_sort(buckets[i])
|
for j in range(len(buckets[i])):
|
||||||
for j in range(0, len(buckets[i])):
|
sorted_array.append(buckets[i][j])
|
||||||
sortedArray.append(buckets[i][j])
|
return sorted_array
|
||||||
|
|
||||||
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))
|
||||||
|
Loading…
Reference in New Issue
Block a user