Bubble Sort Implementation
Welcome to the world of sorting! Today, you're going to learn and implement a simple and famous algorithm called Bubble Sort. Bubble Sort is a process where you repeatedly compare adjacent elements in a list and swap them if they are in the wrong order. This "bubbling" process is repeated until no more swaps are needed, and the list becomes sorted.
Imagine you have a list of numbers like a list of hopping frogs, and you want them to line up in ascending order. Each frog (number) will compare itself with the neighbor on the right and hop over it if it's bigger, letting smaller frogs move forward.
Your task:
Write a function bubble_sort that takes a list of integers and sorts it in ascending order using the Bubble Sort technique. Don’t change the original list; instead, return a new sorted list.
Examples:
print(bubble_sort([34, 1, 5, 12, 7])) # Output: [1, 5, 7, 12, 34] print(bubble_sort([3, 2, 1])) # Output: [1, 2, 3] print(bubble_sort([])) # Output: [] (empty list remains empty)
Are you ready to get started? Let’s bubble up and hop into coding!
Inputs
Arg 1:
[34,1,5,12,7]Expected Output
[1, 5, 7, 12, 34]Actual Output