notes89

# notes89 - Heap Sort Heap Sort is a sorting algorithm that...

This preview shows pages 1–3. Sign up to view the full content.

Heap Sort Heap Sort is a sorting algorithm that runs in O(n log n) time. Unlike a binary tree sort, Heap Sort does not require any additional memory. Heap Sort makes a heap out of the input, then takes the root (the highest value in the heap), adds it to the sorted sequence, fixes the heap, and repeats. Download heapsort.c for a complete implementation. (Example: a.out 5 3 8 1) // heap sort array a[n] void heapSort( int a[], int n ) { int i, child, parent, temp; // Phase 1: Build heap. for ( i= 0 ; i<n; i++ ) { // insert element i into heap. // fix the heap from element i up to the root. child = i; parent = (child- 1 )/ 2 ; while ( child> 0 ) { // check if fixing must occur if ( a[child] <= a[parent] ) { break; } // promote the child temp = a[child]; a[child] = a[parent]; a[parent] = temp; // continue up the heap child = parent; parent = (child- 1 )/ 2 ;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
} } // Phase 2: Remove maximum element from heap. for
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

### Page1 / 4

notes89 - Heap Sort Heap Sort is a sorting algorithm that...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online