Insertion sort

hip-hop, educational · 2:42

Listen on 93

Lyrics

[Verse 1]
Start with the second element, that's the way we begin
Compare it to the left side, see where it fits in
If it's smaller than the neighbor, time to make a swap
Shift the bigger one over, let the smaller one drop
Building sorted portions from the left side growing strong
Each insertion finds its place where it belongs

[Chorus]
Insert and compare, shift if you dare
Building order one by one with care
Left side sorted, right side wild
Insertion sort keeps it compiled
Insert and compare, shift if you dare
Linear search through sorted pairs

[Verse 2]
Take the current element, hold it in your hand
Search the sorted portion to understand
Where this new piece belongs in the grand plan
Shift the greater elements, make room if you can
Starting from position one, not zero we begin
'Cause single element's already sorted within

[Chorus]
Insert and compare, shift if you dare
Building order one by one with care
Left side sorted, right side wild
Insertion sort keeps it compiled
Insert and compare, shift if you dare
Linear search through sorted pairs

[Bridge]
Best case scenario when it's already neat
Big O of n makes it complete
But worst case reversed takes n squared time
Still stable and in-place, working fine
Small datasets love this algorithm's flow
Adaptive nature helps performance grow

[Verse 3]
Outer loop controls which element we select
Inner loop finds where it should connect
Backwards through the sorted section we probe
Moving larger values, making space to load
When we find the spot or hit the start
That's where our current element gets its part

[Chorus]
Insert and compare, shift if you dare
Building order one by one with care
Left side sorted, right side wild
Insertion sort keeps it compiled
Insert and compare, shift if you dare
Linear search through sorted pairs

[Outro]
From chaos comes order, one step at a time
Insertion sort's rhythm, steady and prime
Remember the pattern: select, compare, shift
Simple but powerful, that's insertion's gift

← Heapsort | Bubble sort →