Hi Robert,
Thanks for pointing this out.
A further optimisation can be made by checking is any saps were made in the inner loop, if no swaps were made then we can terminate the loop to prevent further iterations.
Here is a python implementation of the further optimised code:
unsortedData = [20, 33, 12, 53, 24, 65, 23, 4, 53, 1]
n = len(unsortedData)
def bubbleSort(data):
for i in range(n):
swapped = False
for j in range(0, n-i-1):
if data[j] > data[j+1]:
data[j], data[j+1] = data[j+1], data[j]
swapped = True
if not swapped:
break
return data
sortedData = bubbleSort(unsortedData)
print(sortedData)
Do share any further optimisation you are aware of.
Thanks,
Richmond Alake