Oh my gosh, like, let me show you how to totally sort a list with, like, Bubble Sort! It's, like, so easy-peasy. But first, let's make sure you get the deets. Bubble Sort is, like, a sorting algorithm that compares each pair of adjacent items and swaps them if they're in the wrong order. This totally continues until the list is, like, all sorted and cute. Sooo, here's how you do it in Python, okay?
def totally_bubble_sort(arr):
# Like, the length of the array, duh!
n = len(arr)
# We're gonna loop through the array and, like, do our thing
for i in range(n):
# I'll start this as False, but like, if we do a swap, I'll totes make it True
done_swapping = False
# Okay, let's loop through the array but, like, minus the last i elements 'cause they're already sorted, obvi!
for j in range(0, n-i-1):
# Now we're like, gonna check if the current item is totes bigger than the next one
if arr[j] > arr[j+1]:
# Ugh, this is not cool, so we have to swap them to make it all nice
arr[j], arr[j+1] = arr[j+1], arr[j]
# Okay, so we did swap, let's remember that!
done_swapping = True
# Like, if we didn't swap anything, we're done! The list is all sorted and gorge!
if not done_swapping:
break
# Yay! We're done! Let's show off our beautifully sorted list!
return arr
# Let's, like, try it out!
my_super_cute_list = [4, 3, 2, 1, 5]
print("List before sort: ", my_super_cute_list)
my_super_cute_list = totally_bubble_sort(my_super_cute_list)
print("List after, like, a totally awesome sort: ", my_super_cute_list)
Okay, so when you run this code, it will, like, take your hot mess of a list and make it all neat and pretty, in ascending order, 'cause that's how we roll. Bubble Sort might not be, like, the most efficient way to sort a list (it's kinda slow for big lists, TBH), but it's totally perfect for sorting your small list of fave lip gloss shades or something. Happy sorting, babe! 💁♀️✨