### Preview lessons, content and tests

#### Computer Science & Programming solved. All in one platform.

1. To trial the platform and take tests, please take a few seconds to SIGN UP and SET UP FREE.

2. Searching for something specific? See our text overview of all tests. Scroll right for levels, and lists.

3. Student and Teacher User Guides |  Schemes of Work |   Real Teacher use Videos |

Join 36000+ teachers and students using TTIO.

### Bubble Sort

Bubble sort is a comparison?-based algorithm that compares each pair of elements in an array and swaps them if they are out of order until the entire array is sorted.

### Challenge

#1 Write a Python program to sort a list of elements using the bubble sort algorithm

#2 Comment each line of the python solution below to show your understanding of the algorithm

### Solution

```def bubbleSort(nlist):
for passnum in range(len(nlist)-1,0,-1):
for i in range(passnum):
if nlist[i]>nlist[i+1]:
temp = nlist[i]
nlist[i] = nlist[i+1]
nlist[i+1] = temp

nlist = [14,46,43,27,57,41,45,21,70]
bubbleSort(nlist)
print(nlist)```

### Flowchart

www.teachyourselfpython.com