"""
Bubble sort sorts a list by repeatedly
swapping adjacent out-of-order values.
The process continues until the list
becomes sorted.
"""defbubble_sort(array):
isSorted =False
passes =0
length =len(array)whilenot isSorted:
isSorted =True# perform a pass through the array# excluding already sorted positionsfor i inrange(length-1-passes):if array[i]> array[i+1]:
swap(i, i+1, array)# array is not sorted yet
isSorted =False
passes +=1return array
defswap(i, j, array):# Swap values at indexes i and j
array[i], array[j]= array[j], array[i]
arr =[1,9,3,2]print(bubble_sort(arr))# [1, 2, 3, 9]
defbubble_sort(li_to_sort):# Looping from size of array from last index[-1] to index [0]for n inrange(len(li_to_sort)-1,0,-1):for i inrange(n):if li_to_sort[i]> li_to_sort[i +1]:# swapping data if the element is less than next element in the array
li_to_sort[i], li_to_sort[i +1]= li_to_sort[i +1], li_to_sort[i]
li =[39,12,18,85,72,10,2,18]print("Unsorted list: ", li)
bubble_sort(li)print("Sorted List: ", li)
arr =[12,1,6,23,234,456,2,35,687,34]# arry consists of 9 elements
n =len(arr)#conts the element of the arry for j inrange(n-1):#this is for list to get shorted properly && you dont need to go full as it may take more steps to exicutefor i inrange(n-j-1):#if left with n the there will be array error because it will try to add 1 to n in below leading to array being bigger if arr[i]>arr[i+1]:
arr[i],arr[i+1]=arr[i+1],arr[i]else:pass# arry starts from last and eventually decrease the number lower and lower which leads to lesser steps# #above took 125 steps to eully exicute################################################################# # #this takes 217 steps to run and end code# for i in range(n):# if arr[i]>arr[i+1]:# arr[i],arr[i+1]=arr[i+1],arr[i] # else:# pass################################################################# print("the sorted array is : "arr)
defbubbleSort(lis):
length =len(lis)for i inrange(length):for j inrange(length - i):
a = lis[j]if a != lis[-1]:
b = lis[j +1]if a > b:
lis[j]= b
lis[j +1]= a
return lis
"""Bubblesort
"""## Un-optimised--------------------------------------------------------------defbubble_1(lst):
n =len(lst)-1for i inrange(n):# Within the unsorted portionfor j inrange(n - i):# If curr > next, swapif lst[j]> lst[j+1]:
lst[j], lst[j+1]= lst[j+1], lst[j]return lst # for easy testingdefbubble_2(lst):
n =len(lst)-1# Within the unsorted portion, except the last numberfor unsorted inrange(n,0,-1):for i inrange(unsorted):# If curr > next, swapif lst[i]> lst[i+1]:
lst[i], lst[i+1]= lst[i+1], lst[i]return lst # for easy testing## Optimised-----------------------------------------------------------------defbubble_3(lst):
n =len(lst)-1# Within the unsorted portion, except the last numberfor unsorted inrange(n,0,-1):
swapped =Falsefor i inrange(unsorted):# If curr > next, swapif lst[i]> lst[i+1]:
lst[i], lst[i+1]= lst[i+1], lst[i]
swapped =True# Check if its sorted by this timeifnot swapped:breakreturn lst # for easy testing
# Python3 Optimized implementation# of Bubble sort# An optimized version of Bubble SortdefbubbleSort(arr):
n =len(arr)# Traverse through all array elementsfor i inrange(n):
swapped =False# Last i elements are already# in placefor j inrange(0, n-i-1):# traverse the array from 0 to# n-i-1. Swap if the element# found is greater than the# next elementif arr[j]> arr[j+1]:
arr[j], arr[j+1]= arr[j+1], arr[j]
swapped =True# IF no two elements were swapped# by inner loop, then breakif swapped ==False:break# Driver code to test above
arr =[64,34,25,12,22,11,90]
bubbleSort(arr)print("Sorted array :")for i inrange(len(arr)):print("%d"%arr[i],end=" ")# This code is contributed by Shreyanshi Arun
defbubble_sort(our_list):# We go through the list as many times as there are elementsfor i inrange(len(our_list)):# We want the last pair of adjacent elements to be (n-2, n-1)for j inrange(len(our_list)-1):if our_list[j]> our_list[j+1]:# Swap
our_list[j], our_list[j+1]= our_list[j+1], our_list[j]