diff --git a/algorithms/2_BubbleSort/bubble_sort_exercise_solution.py b/algorithms/2_BubbleSort/bubble_sort_exercise_solution.py index df838f7..6e8c7f9 100644 --- a/algorithms/2_BubbleSort/bubble_sort_exercise_solution.py +++ b/algorithms/2_BubbleSort/bubble_sort_exercise_solution.py @@ -3,8 +3,15 @@ def bubble_sort(elements, key=None): size = len(elements) for i in range(size-1): + if key is None: + print("please enter a key to start search.") + break swapped = False + breakloop =False for j in range(size-1-i): + if key not in elements[j]: + breakloop = True + break a = elements[j][key] b = elements[j+1][key] if a > b: @@ -12,6 +19,10 @@ def bubble_sort(elements, key=None): elements[j] = elements[j+1] elements[j+1] = tmp swapped = True + + if breakloop: + print("key is not correct") + break if not swapped: break @@ -25,4 +36,4 @@ def bubble_sort(elements, key=None): ] bubble_sort(elements, key='transaction_amount') - print(elements) \ No newline at end of file + print(elements)