Algorithms and OOD (CSC 207 2013F) : EBoards

CSC207.01 2013F, Class 49: Heaps


Overview

Preliminaries

Admin

Priority queues, revisited

ADTs

Priority queues

Implementation:

Recent implementation techniques

Heaps

Adding elements to heaps

Removing elements from heaps

Outstanding problems that we've relied on magic to resolve

Heap sort

Here's the code:

// Turn the array into a heap
for (int i = 1; i < values.length; i++) {
   swapUp(i);
} // for
// Grab the largest element out of the heap and put them at
// the end.
for (int pos = values.length - 1; pos > 0; pos--) {
   swap(pos, 0);
   swapDown(0);
} // for

Analysis:

Yay! Another O(nlogn) algorithm

Copyright (c) 2013 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.