Skip to content

Commit 678738e

Browse files
author
codebasics
committed
bubble sort
1 parent 4257fa6 commit 678738e

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# you can use this to sort strings too
3+
def bubble_sort(elements):
4+
size = len(elements)
5+
6+
for i in range(size-1):
7+
swapped = False
8+
for j in range(size-1-i):
9+
if elements[j] > elements[j+1]:
10+
tmp = elements[j]
11+
elements[j] = elements[j+1]
12+
elements[j+1] = tmp
13+
swapped = True
14+
15+
if not swapped:
16+
break
17+
18+
19+
if __name__ == '__main__':
20+
elements = [5,9,2,1,67,34,88,34]
21+
elements = [1,2,3,4,2]
22+
elements = ["mona", "dhaval", "aamir", "tina", "chang"]
23+
24+
bubble_sort(elements)
25+
print(elements)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
### Bubble Sort Exercise
2+
3+
Modify [bubble_sort function](https://github.com/codebasics/py/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+
[Solution](https://github.com/codebasics/py/blob/master/Algorithms/2_BubbleSort/bubble_sort_exercise_solution.py)
40+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# you can use this to sort strings too
2+
def bubble_sort(elements, key=None):
3+
size = len(elements)
4+
5+
for i in range(size-1):
6+
swapped = False
7+
for j in range(size-1-i):
8+
a = elements[j][key]
9+
b = elements[j+1][key]
10+
if a > b:
11+
tmp = elements[j]
12+
elements[j] = elements[j+1]
13+
elements[j+1] = tmp
14+
swapped = True
15+
16+
if not swapped:
17+
break
18+
19+
if __name__ == '__main__':
20+
elements = [
21+
{ 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'},
22+
{ 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'},
23+
{ 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'},
24+
{ 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'},
25+
]
26+
27+
bubble_sort(elements, key='transaction_amount')
28+
print(elements)

0 commit comments

Comments
 (0)