Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 CS 151 Heapsort
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

Heapsort

by admin last modified 2005-05-11 18:12

Heapsort


Heapsort is a sorting method which uses a type of binary tree called a heap.  It sorts an array in place, requiring only a constant amount of extra space, in contrast to the merge sort, where the amount of extra space needed is O(n).

First define a heap:

A heap is a binary tree in which the data item in each node is greater than the data items in its two children.

Heapsort always operates on complete binary trees.

example



Representation of a heap in an array

Heapsort uses an array representation for the tree.  No links or pointers are involved.  The data items are stored in breadth-first order in the array.  For example, the tree above would be stored as

index
data
0
92
1
85
2
73
3
81
4
44
5
59
6
64
7
13
8
23
9
36
10
32
11
18
12
54


Because the tree is complete, it is possible to determine the parent, left child, and right child of every node, just knowing its index, with the following rules:
  • The parent of node i is node (i-1)/2.
  • The left child of node i is node 2*i+1.
  • The right child of node i is node 2*i+2.

Basic operations

Two basic operations are defined for heaps, both of which are used in heapsort.  They are called "sift up" and "sift down".

Sift up

Assume that we are given a complete binary tree.  It has the heap property at every node, except that the rightmost node on the lowest level may be greater than its parent.  The problem is make the tree into a heap.  The algorithm is as follows:
  1. Let node be the rightmost node on the lowest level.
  2. If node is the root of the tree, we are done.
  3. If the data in node is less than the data in its parent, we are done.
  4. If not, swap it with its parent, let node = its parent, and go back to step 2.
Q:  What is the running time of the algorithm?

Sift down

Assume that we are given a complete binary tree.  It has the heap property at every node except possibly the root.  The problem is to make the tree into a heap.  The algorithm is as follows:
  1. Let node be the root of the tree.
  2. If node has no children, we have a heap, so we are done.
  3. If not, compare the data item in node with the data item in its larger child.
  4. If the node data item is greater than its larger child, we have a heap, so we are done.
  5. If not, swap the node data item with the data item in its larger child, let node = the larger child, and go back to step 2.
Q:  What is the running time of the algorithm?

Implementing a priority queue

A heap can be used to implement a priority queue, as follows:

To add an item:

Create a new node containing the item and attach it to the tree so that it is the rightmost node on the lowest level.
Then apply the siftup algorithm.

To remove the largest item:

Make a copy of the root data item, to use as the return value.
Copy the data item from the rightmost node on the lowest level to the root, and delete the node.
Apply the siftdown algorithm.

Heapsort

The algorithm works in two stages.  In the first stage, the tree is made into a heap.  In the second stage, items are removed from the tree one at a time and moved to the end of the array, where the sorted data is accumulated.

Assume that we have a method siftdown(int root, int last), which applies the siftdown algorithm to the heap whose root is given and in which last is the index of the rightmost node on the lowest level.

Stage one:  Make a heap.

Assume that the data is stored in an array in positions 0..(length-1).
Observe that for node i to have a left child, 2*i+1 must be <= length-1, so i <= length/2 - 1.

for(i=length/2-1; i>=0; i--)
    siftdown(i,length-1);

Stage two:  Sort.

for(i=length-1; i>0; i--){
    swap array[0] with array[i];
    siftdown(0,i-1);
}

Analysis:

The running time of the first stage is O(n).

The second stage consists of calls to siftdown on arrays of size 2,3,...,n.  The time for each call is O(array size).  Summing up all the calls, we get O(n log n).

So, heapsort runs in time O(n log n) in the worst case, with space requirement O(1).


 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: