From 3d7135c571104d317e9cf90943eccf375055202c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADkolas=20Vargas?= Date: Mon, 20 May 2019 23:09:49 -0300 Subject: [PATCH 1/2] fix empty list validation and code data structures --- sorts/bucket_sort.py | 43 +++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/sorts/bucket_sort.py b/sorts/bucket_sort.py index aba0124ad909..37dbe3e13f1d 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -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!") + 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] + print(bucket_sort(unsorted)) From 62dfba0cdb4b78ebbc676dd1158f2edb1ae9566f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Garc=C3=ADa=20Cu=C3=A9llar?= Date: Wed, 22 May 2019 02:18:11 +0200 Subject: [PATCH 2/2] Update bucket_sort.py https://github.com/TheAlgorithms/Python/pull/826#pullrequestreview-240357549 --- sorts/bucket_sort.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/sorts/bucket_sort.py b/sorts/bucket_sort.py index 37dbe3e13f1d..cca913328e40 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -10,25 +10,26 @@ # 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 len(my_list) == 0: - raise Exception("you don't have any elements in array!") + raise 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)] + bucket_count = ((max_value - min_value) // bucket_size + 1) + buckets = [[] for _ in range(int(bucket_count))] 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]))]) 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] + unsorted = [float(n) for n in user_input.split(',') if len(user_input) > 0] print(bucket_sort(unsorted))