-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesort.py
23 lines (15 loc) · 924 Bytes
/
bubblesort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#The bubble sort uses a straightforward logic that works by repeating swapping the adjacent elements if they are not in the right order.
#It compares one pair at a time and swaps if the first element is greater than the second element; otherwise, move further to the next pair of elements for comparison.
def bubble_sort(list1):
for i in range(0,len(list1)-1):
for j in range(len(list1)-1):
#we have defined two for loop - first for loop iterates the complete list and
#the second for loop iterates the list and the compare the two elements in every outer loop iterations.
if(list1[j]>list1[j+1]):
temp = list1[j]
list1[j] = list1[j+1]
list1[j+1] = temp
return list1
list1 = [5, 3, 8, 6, 7, 2]
print("The unsorted list is: ", list1)
print("The sorted list is: ", bubble_sort(list1))