Skip to content

Commit 325c246

Browse files
adding 2nd exercise my solution
1 parent 001fd46 commit 325c246

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

2.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# ### Bubble Sort Exercise
2+
3+
# Modify [bubble_sort function](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/2_BubbleSort/bubble_sort.py) such that it can sort following list of transactions happening in an electronic store,
4+
# ```
5+
# elements = [
6+
# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
7+
# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
8+
# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
9+
# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
10+
# ]
11+
# ```
12+
# bubble_sort function should take key from a transaction record and sort the list as per that key. For example,
13+
# ```
14+
# bubble_sort(elements, key='transaction_amount')
15+
# ```
16+
# This will sort elements by transaction_amount and your sorted list will look like,
17+
# ```
18+
# elements = [
19+
# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
20+
# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
21+
# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
22+
# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
23+
# ]
24+
# ```
25+
# But if you call it like this,
26+
# ```
27+
# bubble_sort(elements, key='name')
28+
# ```
29+
# output will be,
30+
# ```
31+
# elements = [
32+
# { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
33+
# { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
34+
# { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
35+
# { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
36+
# ]
37+
# ```
38+
39+
# base bubble_sort. you can use this to sort strings too
40+
def bubble_sort(elements):
41+
size = len(elements)
42+
43+
for i in range(size-1):
44+
swapped = False
45+
for j in range(size-1-i):
46+
if elements[j] > elements[j+1]:
47+
tmp = elements[j]
48+
elements[j] = elements[j+1]
49+
elements[j+1] = tmp
50+
swapped = True
51+
52+
if not swapped:
53+
break
54+
55+
def bubble_sort_by_key(elements, key):
56+
size = len(elements)
57+
58+
for i in range(size-1):
59+
swapped = False
60+
for j in range(size-1-i):
61+
if elements[j][key] > elements[j+1][key]:
62+
tmp = elements[j]
63+
elements[j] = elements[j+1]
64+
elements[j+1] = tmp
65+
swapped = True
66+
67+
if not swapped:
68+
break
69+
70+
71+
elements = [5,9,2,1,67,34,88,34]
72+
elements = [1,2,3,4,2]
73+
elements = ["mona", "dhaval", "aamir", "tina", "chang"]
74+
75+
bubble_sort(elements)
76+
print(elements)
77+
78+
elements2 = [ { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
79+
{ 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
80+
{ 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
81+
{ 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
82+
]
83+
bubble_sort_by_key(elements2,key='transaction_amount')
84+
print(elements2)

0 commit comments

Comments
 (0)