From e9a9813ffdfc40a3519a818b34b801e5fe7270b7 Mon Sep 17 00:00:00 2001 From: DannyBoy3310 <78155486+DannyBoy3310@users.noreply.github.com> Date: Fri, 19 Aug 2022 18:10:17 +0530 Subject: [PATCH] Updated bubble sort solution --- .../bubble_sort_exercise_solution.py | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/algorithms/2_BubbleSort/bubble_sort_exercise_solution.py b/algorithms/2_BubbleSort/bubble_sort_exercise_solution.py index df838f7..82d7b13 100644 --- a/algorithms/2_BubbleSort/bubble_sort_exercise_solution.py +++ b/algorithms/2_BubbleSort/bubble_sort_exercise_solution.py @@ -1,28 +1,20 @@ -# you can use this to sort strings too -def bubble_sort(elements, key=None): - size = len(elements) - - for i in range(size-1): - swapped = False - for j in range(size-1-i): - a = elements[j][key] - b = elements[j+1][key] - if a > b: - tmp = elements[j] - elements[j] = elements[j+1] - elements[j+1] = tmp +def bubblesort(arr,param): + n = len(arr) + swapped = False + for i in range(n-1): + for j in range(n-i-1): + if arr[j][param] > arr[j+1][param]: + arr[j],arr[j+1] = arr[j+1],arr[j] swapped = True - - if not swapped: + if swapped is False: break -if __name__ == '__main__': - elements = [ - { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'}, - { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'}, + +elements = [ { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'}, + { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'}, { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'}, + { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'}, ] - - bubble_sort(elements, key='transaction_amount') - print(elements) \ No newline at end of file +bubblesort(elements,param="transaction_amount") +print(elements)