notes89

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

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

View Full Document Right Arrow Icon
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 ;
Background image of page 1

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

View Full DocumentRight Arrow Icon
} } // Phase 2: Remove maximum element from heap. for
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online