Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Updated bubble sort solution
  • Loading branch information
DannyBoy3310 authored Aug 19, 2022
commit e9a9813ffdfc40a3519a818b34b801e5fe7270b7
36 changes: 14 additions & 22 deletions algorithms/2_BubbleSort/bubble_sort_exercise_solution.py
Original file line number Diff line number Diff line change
@@ -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)
bubblesort(elements,param="transaction_amount")
print(elements)