mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
fix empty list validation and code data structures (#826)
* fix empty list validation and code data structures * Update bucket_sort.py https://github.com/TheAlgorithms/Python/pull/826#pullrequestreview-240357549
This commit is contained in:
parent
b5667e5ee9
commit
023f5e092d
|
@ -14,34 +14,22 @@
|
||||||
# Best Case O(n); Average Case O(n); Worst Case O(n)
|
# Best Case O(n); Average Case O(n); Worst Case O(n)
|
||||||
|
|
||||||
DEFAULT_BUCKET_SIZE=5
|
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("Please add some elements in the array.")
|
||||||
|
|
||||||
min_value=min(my_list)
|
min_value, max_value = (min(my_list), max(my_list))
|
||||||
max_value=max(my_list)
|
bucket_count = ((max_value - min_value) // bucket_size + 1)
|
||||||
|
buckets = [[] for _ in range(int(bucket_count))]
|
||||||
|
|
||||||
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)):
|
for i in range(len(my_list)):
|
||||||
buckets[(my_list[i]-min_value)//bucket_size].append(my_list[i])
|
buckets[int((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]))])
|
||||||
|
|
||||||
sorted_array=[]
|
if __name__ == "__main__":
|
||||||
for i in range(len(buckets)):
|
user_input = input('Enter numbers separated by a comma:').strip()
|
||||||
buckets[i].sort()
|
unsorted = [float(n) for n in user_input.split(',') if len(user_input) > 0]
|
||||||
for j in range(len(buckets[i])):
|
print(bucket_sort(unsorted))
|
||||||
sorted_array.append(buckets[i][j])
|
|
||||||
return sorted_array
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#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))
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user