Heapsort

hip-hop, educational · 2:36

Listen on 93

Lyrics

[Verse 1]
Start with an array, unsorted and wild
Heapsort gonna tame it, structured and styled
First we build a heap, max property strong
Parent beats the children, nothing can go wrong
Bottom up we go, heapify the base
Bubble up the largest, put them in their place
Array transformation, heap property true
Left child at two i, right at two i plus two

[Chorus]
Build it up, tear it down, that's the heapsort way
Max heap to the top, sorted array
Extract the root, put it at the end
Heapify again, let the process blend
Build it up, tear it down, order from the mess
Log n operations, efficiency blessed

[Verse 2]
Phase one is construction, make that heap complete
Every parent stronger than children they meet
Start from last parent, work your way up high
Sift down the violators, let the big ones fly
Index parent minus one divided by two
Mathematics guides us in what we gotta do
Now we got a max heap, largest at the top
Time for phase two, the extraction won't stop

[Chorus]
Build it up, tear it down, that's the heapsort way
Max heap to the top, sorted array
Extract the root, put it at the end
Heapify again, let the process blend
Build it up, tear it down, order from the mess
Log n operations, efficiency blessed

[Bridge]
Swap the root with last, shrink the heap size down
Restore the heap property, largest wears the crown
Repeat until we're done, one by one they fall
Sorted in ascending, we conquered them all
In place algorithm, memory stays tight
N log n complexity, performance feels right

[Verse 3]
Heapify function, the heart of our game
Compare with children, largest takes the name
If parent is smaller, then we gotta swap
Recursively downward until violations stop
Complete binary tree, packed in array form
No pointers needed, efficiency transform
Unstable sorting, equal elements shift
But guaranteed performance is heapsort's gift

[Outro]
From chaos to order, heap shows the way
Build it up, tear it down, sorted array
Remember the process, build then extract
Heapsort delivers, that's algorithm fact

← Mergesort | Insertion sort →