def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(n - 1 - i):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
Variables
n- length of the input array
Explanation
Two nested loops over `n` elements - each pass bubbles the largest unsorted value to the end. Comparisons sum to about n²/2, so **O(n²)** time. Swaps happen in place with no extra storage, **O(1)** space.