+ 1
Can we make this code more efficient? It's Time Complexity is O(n)
# Author: Mohd Shadman # Topic: Bubble Sort arr = [3,1,5,4,9,2,6,8,7] l=len(arr)-1 swap=-1 # To Make Time complexity O(n) for i in range(l): for j in range(l-i): if arr[j] > arr[j+1]: temp=arr[j] arr[j]=arr[j+1] arr[j+1]=temp swap=1 if swap==-1: break print(arr)
3 Respostas
+ 1
Justice What are you saying?
0
Jay Matthews Mine's best case
0
Mohammad Shadman Khan This is not the best case because best case requires the array to be sorted already, which yours is not.