+ 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)

29th Jun 2022, 7:35 PM
Mohammad Shadman Khan
Mohammad Shadman Khan - avatar
3 Réponses
+ 1
Justice What are you saying?
1st Jul 2022, 3:17 PM
Mohammad Shadman Khan
Mohammad Shadman Khan - avatar
0
Jay Matthews Mine's best case
29th Jun 2022, 8:19 PM
Mohammad Shadman Khan
Mohammad Shadman Khan - avatar
0
Mohammad Shadman Khan This is not the best case because best case requires the array to be sorted already, which yours is not.
29th Jun 2022, 8:36 PM
Justice
Justice - avatar