Post Optimize Bubble Sort

neupane parlad
Data Science Street
Mar 12, 2022

--

Bubble sort requires is O(N²) time in non optimize state, but we can optimize it to be at O(N²)/2 by reducing the number of iteration needed. Here is python code sample:

def bubble_sort(arr):iteration_count = 0for i in range(len(arr)):for idx in range(len(arr) - i - 1):iteration_count += 1if arr[idx] > arr[idx + 1]:arr[idx], arr[idx + 1] = arr[idx + 1], arr[idx]

--

--

neupane parlad
Data Science Street

Follow me for Data Science, Data Engineering and Data Analysis Articles. Follow me on twitter at @parladN