Iterates through the list, swapping the items if valid (i.e., ascending and the first number is larger).

This must be performed multiple times to fully sort a list, to decide whether to perform again you have to check if any changes have been made.

Implementation

from random import randint
data = [randint(1, 100) for x in range(10)]
 
# utility functions
def swap(from_pos: int, to_pos: int, arr: list[int]) -> None:
	arr[from_pos], arr[to_pos] = arr[to_pos], arr[from_pos]
 
# recursive implementation
def bubble_sort(arr: list[int]) -> None:
	changed = False
	for i in range(len(arr) - 1):
		curr = arr[i]
		next = arr[i + 1]
		if curr > next: # ascending
			swap(i, i + 1, arr)
			changed = True
	if changed:
		bubble_sort(arr)
 
bubble_sort(data)
print(data)