# Heaps / Heap Sort

### The Heap Sort Algorithm

The central task in the heapsort algorithm is restoring the heap after each removal of the root element. This reheaping takes O(log(n)) time, for a total of O(nlog(n)) time since there are n elements. It seem counterintuitive that heap sort would be so efficient since building the heap often increases the number of inversions in the array. In fact it is not only O(nlogn) in the average case, but it is O(nlog(n)) in all cases, unlike quick sort, which is quadratic in the worst case.

To walk through the process again, moving an element up from node 1 takes on the order of log(n) steps since there are log(n) levels in the tree that the value may have to move through. Therefore heapsort will take O(nlog(n)) time, sifting up to log(n) levels for each element sorted. Even though the heap gets smaller as the array is sorted, it doesn't get smaller very fast. Half the elements in the initial heap are in the leaves, and after being exchanged with the root, each of them can be expected to move about log(n) levels back.

## Take a Study Break

### Star Trek gets SEXY

Chris Pine and Zoe Saldana heat up the red carpet!

### Are you afraid of relationships?

Auntie SparkNotes can help!

### Sexy starlet style

See every single look from the Met Gala!

### Geeky Actors: Then and Now

Travel back in time!

### 10 Movies Better Than Their Books

What do you think?

### How To Look Like J-Law...

When you don't look like J-Law.

### 12 Scientific Inaccuracies in Into Darkness

What did Star Trek get wrong?

### Villains We Want These Actresses to Play

From super cute to super bad!

## The Book

### Read What You Love, Anywhere You Like

Get Our FREE NOOK Reading Apps