Bubble sort in python is a technique to sort the set of data in either ascending order or descending order. Bubble sort uses the compare and swap technique while sorting. Out of all the sorts Bubble sort is the slowest algorithm that’s why practically development doesn’t use it. However, the bubble sort is the first step of sorting techniques for learning sorting algorithms.
The logic behind python bubble sort.
Bubble sort has a very simple login. It used the compare and swap technique. That means it compares two adjacent data items, and if they are in the wrong order, bubble sort swaps it. Since it needs to compare each data member, that’s why bubble sort takes a lot of time to execute and give the desired output compared to other sorting techniques.
Bubble sort algorithm in python
- Start.
- Get the number of data items in the data set.
- Perform (n-1) passes of compare and swap technique that means if the first element is greater than other then swap if not keep the sequence as it is.
- Repeat step 3 till (n-1) passes.
- Display sorted data.
- Stop.
Let’s take one example to understand the bubble sort python neatly.
Data = [30,20,40,10,50]
To sort the data, we apply a bubble sort algorithm.
First pass,
[20,30,10,40,50] because 30>20 and 40>10
Second pass,
[20,10,30,40,50] because 30>10
Third pass,
[10,20,30,40,50] because 20>10
So, the sorted form of the data is 10, 20, 30, 40, 50.
Table of Contents
Example of a bubble sort program in python.
There are many ways to implement the bubble sort program login in python. We are going to learn different ways to implement bubble sort.
Bubble sort in python using a list.
Accept the data set of the user and store it into the list. We will apply the bubble sort algorithm to it. Let’s see the code.
Example
n = int(input("Enter number of data items: "))
lst = []
for i in range(n):
x = input("Enter data:") #accepting data from user
lst.append(x)
print("Unsorted data is")
print(lst)
for i in range(0,len(lst)):
for j in range(1,len(lst)-i):
if(lst[j-1] > lst[j]):
tmp = lst[j-1];
lst[j-1] = lst[j]; #logic for bubble sort
lst[j] = tmp;
print("Sorted data is")
print(lst)
Output
Enter number of data items: 10
Enter data:0
Enter data:9
Enter data:8
Enter data:7
Enter data:6
Enter data:5
Enter data:4
Enter data:3
Enter data:2
Enter data:1
Unsorted data is
['0', '9', '8', '7', '6', '5', '4', '3', '2', '1']
Sorted data is
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
Bubble sort using while loop.
We have declared an unsorted list, and we will sort it with bubble sort using a while loop.
Example
n = int(input("Enter number of data items: "))
list1 = []
for i in range(n):
x = input("Enter data:") #accepting data from user
list1.append(x)
swap = True
print("Unsorted data is")
print(list1)
while(swap):
swap = False
for i in range(len(list1) - 1):
if list1[i] > list1[i + 1]:
list1[i], list1[i + 1] = list1[i + 1], list1[i] #logic for bubble sort
swap = True
print("Sorted data is")
print(list1)
Output
Enter number of data items: 10
Enter data:0
Enter data:9
Enter data:8
Enter data:7
Enter data:6
Enter data:5
Enter data:4
Enter data:3
Enter data:2
Enter data:1
Unsorted data is
['0', '9', '8', '7', '6', '5', '4', '3', '2', '1']
Sorted data is
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
Bubble sort in python using functions.
Accept the data set from the user, and pass it to the bubble sort function which will return the data in sorted form.
Example
def bubble_sort(lst):
for i in range(0, len(lst)):
for j in range(1, len(lst) - i):
if (lst[j - 1] > lst[j]):
tmp = lst[j - 1];
lst[j - 1] = lst[j]; #logic for bubble sort
lst[j] = tmp;
n = int(input("Enter number of data items: "))
lst = []
for i in range(n):
x = input("Enter data:") #accepting data from user
lst.append(x)
print("Unsorted data is")
print(lst)
bubble_sort(lst) # call to the function
print("Sorted data is")
print(lst)
Output
Enter number of data items: 5
Enter data:g
Enter data:r
Enter data:t
Enter data:h
Enter data:d
Unsorted data is
['g', 'r', 't', 'h', 'd']
Sorted data is
['d', 'g', 'h', 'r', 't']