Six sorting algorithms with animated demos, Python code, and kid-friendly explanations
Like bubbles rising to the surface ā big numbers float to the end!
Imagine you have a row of numbered cards. You look at two cards next to each other. If the bigger number is on the left, you swap them. Then you move one step to the right and check again. After one full pass, the BIGGEST number has "bubbled" all the way to the end! You keep doing this over and over, and each pass, the next biggest number finds its home.
š” Bubble sort is so slow that computer scientists use it as the classic example of what NOT to do ā but it's great for learning!
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr