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
1def bubbleSort(arr):
2 n = len(arr)
3
4 # Traverse through all array elements
5 for i in range(n-1):
6 # range(n) also work but outer loop will repeat one time more than needed.
7
8 # Last i elements are already in place
9 for j in range(0, n-i-1):
10
11 # traverse the array from 0 to n-i-1
12 # Swap if the element found is greater
13 # than the next element
14 if arr[j] > arr[j+1] :
15 arr[j], arr[j+1] = arr[j+1], arr[j]
16
17# Driver code to test above
18arr = [64, 34, 25, 12, 22, 11, 90]
19
20bubbleSort(arr)
1def bubbleSort(arr):
2 n = len(arr)
3
4 # Traverse through all array elements
5 for i in range(n-1):
6 # range(n) also work but outer loop will repeat one time more than needed.
7
8 # Last i elements are already in place
9 for j in range(0, n-i-1):
10
11 # traverse the array from 0 to n-i-1
12 # Swap if the element found is greater
13 # than the next element
14 if arr[j] > arr[j+1] :
15 arr[j], arr[j+1] = arr[j+1], arr[j]
16
17# Driver code to test above
18arr = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
19
20bubbleSort(arr)
21print ("Sorted array is:")
22for i in range(len(arr)):
23 print ("%d" %arr[i]),
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