Heaps / Heap Sort


Heap Sort

Heap sort relies on the properties of a heap data structure to sort a data set. A heap is a partially ordered binary tree where every node is greater than or equal to both of its children, hence the largest value in the tree is always in the root node. A binary tree is mapped onto an array so that the node in position i is parent of the nodes in positions 2*i and (2*i + 1) , if of course they exist.

Heapsort sorts an array by first converting the array into a heap so that it has the relational property described above. It then sorts the data in reverse by repeatedly placing the largest unsorted element into its correct place. It does so by repeatedly (1) removing the maximum value in the heap (the value in the root node), (2) putting that value into the sorted array, and (3) rebuilding the heap with one fewer elements. Note that heapsort does not need two separate arrays; it can use use the same array for the heap and the sorted array.

Take a Study Break

SparkLife

Star Trek gets SEXY

Chris Pine and Zoe Saldana heat up the red carpet!

SparkLife

Are you afraid of relationships?

Auntie SparkNotes can help!

SparkLife

Wanna get JLaw's gorgeous glow?

Click here for simple, sexy makeup tricks!

SparkLife

Sexy starlet style

See every single look from the Met Gala!

SparkLife

Who'd be on your zombie-apocalypse crew?

We already dib'sed Genghis Khan.

Geek out!

The MindHut

Geeky Actors: Then and Now

Travel back in time!

The MindHut

Villains We Want These Actresses to Play

From super cute to super bad!

The MindHut

10 Movies Better Than Their Books

What do you think?

The MindHut

How To Look Like J-Law...

When you don't look like J-Law.

The MindHut

12 Scientific Inaccuracies in Into Darkness

What did Star Trek get wrong?

The Book

Cover image

Read What You Love, Anywhere You Like

Get Our FREE NOOK Reading Apps