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 bubble_sort(arr):
2 x=-1
3 n=len(arr)#length of array 6
4 for i in range (0,n):
5 for j in range(1,n-i):
6 if arr[j-1]>arr[j]:
7 arr[j-1],arr[j]=arr[j],arr[j-1]
8 if (n-i)<=1:
9 break
10 return arr
11
12if "__main__"==__name__:
13 arr=[7,1,2,6,9,3,8,4]
14 result=bubble_sort(arr)
15 print(result)
16
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 bubble_sort(arr):
2 def swap(i, j):
3 arr[i], arr[j] = arr[j], arr[i]
4
5 n = len(arr)
6 swapped = True
7
8 x = -1
9 while swapped:
10 swapped = False
11 x = x + 1
12 for i in range(1, n-x):
13 if arr[i - 1] > arr[i]:
14 swap(i - 1, i)
15 swapped = True
16
17 return arr
1def bubble(st):
2 for i in range(len(st),1,-1):
3 for j in range(0,i-1):
4 if st[j]>st[j+1]:
5 st[j],st[j+1]=st[j+1],st[j]
6 else:
7 pass
8 print(st)
9bubble([64, 34, 25, 12, 22, 11, 90] )
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Created on Sun Mar 10 18:05:51 2019
5
6@note: Exchanging sort - Bubble Sort algorithm
7@source: http://interactivepython.org/runestone/static/pythonds/SortSearch/TheBubbleSort.html
8
9"""
10
11def bubbleSort(alist):
12 for passnum in range(len(alist)-1,0,-1):
13 for i in range(passnum):
14 if alist[i]>alist[i+1]:
15 temp = alist[i]
16 alist[i] = alist[i+1]
17 alist[i+1] = temp
18