1def bubbleSort(lis):
2 length = len(lis)
3 for i in range(length):
4 for j in range(length - i):
5 a = lis[j]
6 if a != lis[-1]:
7 b = lis[j + 1]
8 if a > b:
9 lis[j] = b
10 lis[j + 1] = a
11 return lis
12
1# Python program for implementation of Bubble Sort
2
3def bubbleSort(arr):
4 n = len(arr)
5
6 # Traverse through all array elements
7 for i in range(n-1):
8 # range(n) also work but outer loop will repeat one time more than needed.
9
10 # Last i elements are already in place
11 for j in range(0, n-i-1):
12
13 # traverse the array from 0 to n-i-1
14 # Swap if the element found is greater
15 # than the next element
16 if arr[j] > arr[j+1] :
17 arr[j], arr[j+1] = arr[j+1], arr[j]
18
19# Driver code to test above
20arr = [64, 34, 25, 12, 22, 11, 90]
21
22bubbleSort(arr)
23
24print ("Sorted array is:")
25for i in range(len(arr)):
26 print ("%d" %arr[i]),
27