Skip to Content

Coding

Sorting

Bubble Sort

Write a function that takes a list of numbers and sorts them in ascending order.

sort.py
numbers = [50, 10, 20, 40, 30] def bubble_sort(arr: list) -> list: n = len(arr) for i in range(n - 1): for j in range(0, n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr sorted_numbers = bubble_sort(numbers) print(sorted_numbers) # Output: [10, 20, 30, 40, 50]
Last updated on