Compute time complexity of Bubble Sort algorithm using Python language implementation. Explain your work
Compute time complexity of Bubble Sort
begin BubbleSortAlgorithm(array)
For all the elements of the array
if array[i] > array [i + 1]
switch ( array[i] , array[i+!])
end if
end for
return array
end BubbleSortAlgorithm
Step by step
Solved in 3 steps with 2 images
What is time complexity for this Bubble Sort
def bubbleSort(array):
length = len(array)
for i in range(length-1):
for j in range(0, length-i-1):
if array[j] > array[j+1] :
array[j], array[j+1] = array[j+1], array[j]
arr = [56,7,1,9,0,4,34,100,67]
print ("Elements of array before sorting:")
for i in range(len(arr)):
print ("%d" %arr[i]),
bubbleSort(arr)
print ("Elements of array after sorting:")
for i in range(len(arr)):
print ("%d" %arr[i])