Bubble sort

Learn Algorithms · 4:20

Listen on 93

Lyrics

[Verse 1]
Started with a list that's all mixed up and wrong
Numbers out of order, gotta move along
Take the first two elements, compare them side by side
If the left is bigger, make them switch and slide
Keep on moving rightward through the entire array
Largest bubble floats up by the end of day
Simple but inefficient, that's the bubble way
O of n squared complexity, that's the price we pay

[Chorus]
Bubble up, bubble up, largest to the right
Compare and swap, compare and swap, till everything's in sight
Bubble up, bubble up, repeat until it's done
Adjacent pairs, adjacent pairs, sorted one by one
When no swaps happen, then you know you've won
Bubble sort complete, every element in place and spun

[Verse 2]
Outer loop controls how many passes that we make
Inner loop does comparisons for the array's sake
Each pass guarantees one element finds its home
Largest unsorted value no longer needs to roam
Optimization tip, reduce the inner bound
Last i elements already sorted, safe and sound
Flag variable tracks if any swaps occurred
If none happened, early exit, that's the magic word

[Chorus]
Bubble up, bubble up, largest to the right
Compare and swap, compare and swap, till everything's in sight
Bubble up, bubble up, repeat until it's done
Adjacent pairs, adjacent pairs, sorted one by one
When no swaps happen, then you know you've won
Bubble sort complete, every element in place and spun

[Bridge]
Stable sort algorithm, equal elements stay
In their relative positions from the original array
In-place sorting method, no extra memory cost
But time complexity high, efficiency is lost
Best case linear when the list is already clean
Worst case quadratic, slowest sort you've seen

[Verse 3]
Educational value though you shouldn't use in prod
Understanding fundamentals, give this method a nod
Teaches loop mechanics and the swapping concept clear
Foundation for more complex algorithms we hold dear
From bubble sort basics to merge sort mastery
Each algorithm teaches computational artistry

[Chorus]
Bubble up, bubble up, largest to the right
Compare and swap, compare and swap, till everything's in sight
Bubble up, bubble up, repeat until it's done
Adjacent pairs, adjacent pairs, sorted one by one
When no swaps happen, then you know you've won
Bubble sort complete, every element in place and spun

[Outro]
Remember bubble sort when you're learning algo flow
Simple concepts first, then watch your knowledge grow
Compare adjacent elements, swap when out of place
Bubble sorting fundamentals, master at your pace

← Insertion sort | Timsort →