Heapsort
Chapter 6
CPTR 430 Algorithms Heapsort
Heapsort Chapter 6 1 CPTR 430 Algorithms Heapsort - - PowerPoint PPT Presentation
Heapsort Chapter 6 1 CPTR 430 Algorithms Heapsort
CPTR 430 Algorithms Heapsort
■ Input: a sequence of n numbers
1
☎■ Output: a permutation (reordering)
1
✁ ✂ ✂ ✂ ✁n
✄1
☎1
✝ ✞ ✞ ✞ ✝n
✄1 ■ Instance of problem:
CPTR 430 Algorithms Heapsort
■ Numbers all by themselves are rarely sorted ■ Usually, records are sorted by numeric keys ❚ The rest of the record is satellite data and goes along with the keys
❚ If records are large (lots of satellite data), then an array of pointers to
■ An algorithm can concentrate on sorting plain numbers; sorting records
CPTR 430 Algorithms Heapsort
■ Many applications need to sort data ■ Many algorithms sort data via a subroutine in the course of their actions ■ There are many different sorting algorithms,
■ Bounds can be easily determined for sorting algorithms; this experience
■ The implementations of sorting algorithms demonsrate many software
CPTR 430 Algorithms Heapsort
■ Insertion sort (we saw this in Chapter 2) ■ Merge sort (we saw this in Chapter 2) ■ Heapsort (this chapter) ■ Quicksort (Chapter 7)
CPTR 430 Algorithms Heapsort
■ Like merge sort, heapsort runs in O
■ Like insertion sort, heapsort sorts in place (merge sort requires extra
■ Heapsort uses a specialized data structure to manage the sort ❚ The heap ❚ Not to be confused with the “heap” used for dynamic memory
❚ Also makes a nice priority queue
CPTR 430 Algorithms Heapsort
■ The heap is an array that can be viewed as a nearly complete binary tree ❚ The tree is completely filled at all levels except the lowest ❚ The lowest level is filled from the left 7 9 3 1 4 2 8 14 10 16 16 9 3 1 4 2 7 8 10 14 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
CPTR 430 Algorithms Heapsort
7 9 3 1 4 2 8 14 10 16 16 9 3 1 4 2 7 8 10 14 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
CPTR 430 Algorithms Heapsort
■ In a max-heap, A[parent
❚ The root holds the largest value ❚ Max-heaps are used for sorting ■ In a min-heap, A[parent
❚ The root holds the smallest value ❚ Min-heaps are often used for priority queues
CPTR 430 Algorithms Heapsort
■ The height of a node in a heap is the number of edges on the longest
■ The height of a heap is the height of its root ■ Heap is based on a complete binary tree
■ Basic operations on a heap run in time proportional to the height of the
CPTR 430 Algorithms Heapsort
■ max-heapify: maintains the max-heap property ■ build-max-heap: produces a max-heap from an unsorted array ■ heapsort: sorts an array in place (using a heap) ■ max-heap-insert, heap-extract-max, heap-increase-key, heap-maximum:
CPTR 430 Algorithms Heapsort
■ a is an array ■ heapSize is the size of the heap ■ i is an index into the array
CPTR 430 Algorithms Heapsort
■ left(i) and right(i) must be roots of max-heaps ■ a[i] may be smaller than either of its children (thus violating the max-
■ maxHeapify() moves a[i] down into the heap so that the subtree
CPTR 430 Algorithms Heapsort
1 2 3 4 5 6 7 8 9 7 9 3 1 2 10 16 4 14 8 i 1 2 3 4 5 6 7 8 9 7 9 3 1 2 14 10 16 8 4 i 1 2 3 4 5 6 7 8 9 7 9 3 1 2 14 10 16 8 4 i
CPTR 430 Algorithms Heapsort
■ The time to fix up the relationships is constant ■ Each subtree of i has size at most 2n
CPTR 430 Algorithms Heapsort
CPTR 430 Algorithms Heapsort
■ To build a max-heap from an arbitrary array, apply maxHeapify() in a
■ All the elements in the range
❚ Each of the leaves is a subtree of size 1 ❚ n
✁CPTR 430 Algorithms Heapsort
1 2 3 4 5 6 7 8 9 9 i 2 10 3 7 1 4 14 8 16 1 2 3 4 5 6 7 8 9 9 8 4 14 2 10 3 7 1 16 1 2 3 4 5 6 7 8 9 9 8 7 16 1 4 3 10 14 2 1 2 3 4 5 6 7 8 9 9 8 i 7 16 1 4 14 2 10 3 1 2 3 4 5 6 7 8 9 9 8 14 2 7 16 1 4 3 10 1 2 3 4 5 6 7 8 9 9 8 14 2 7 16 1 4 3 10 i i i i
CPTR 430 Algorithms Heapsort
CPTR 430 Algorithms Heapsort
CPTR 430 Algorithms Heapsort
■ Each call to maxHeapify() takes O
■ maxHeapify() is called O
■ T
■ maxHeapify()’s run time depends on the height of the node within the
■ Most nodes are not very high (more nodes are on lower levels than
CPTR 430 Algorithms Heapsort
■ An n-element heap has height
■ An n-element heap has at most
1
✂■ The time for maxHeapify() to run on a heap of height h is O
■ The running time of buildMaxHeap() is thus
✄lgn
☎h
✆1 O
CPTR 430 Algorithms Heapsort
lgn
☎h
✆1 O
lgn
☎h
✆CPTR 430 Algorithms Heapsort
lgn
☎h
✆∞
k
✆2
∞
h
✆∞
h
✆h
✁2
✁CPTR 430 Algorithms Heapsort
lgn
☎h
✆∞
h
✆CPTR 430 Algorithms Heapsort
■ The largest value is now at the front (top of the heap)
■ The largest element is now in its proper place in the sorted array ■ If the last element is ignored, the children of the root remain max-heaps, but the
■ This effectively cuts the previous root off from the rest of the heap
CPTR 430 Algorithms Heapsort
1 2 3 4 5 6 7 8 9 9 2 10 3 7 i 16 14 8 4 1 1 2 3 4 5 6 7 8 9 2 3 7 16 8 4 i 14 10 9 1 1 2 3 4 5 6 7 8 9 7 16 8 4 14 1 10 9 3 2 i 1 2 3 4 5 6 7 8 9 9 2 10 3 7 1 4 14 8 16
CPTR 430 Algorithms Heapsort
1 2 3 4 5 6 7 8 9 16 4 14 1 10 3 9 i 8 7 2 1 2 3 4 5 6 7 8 9 16 14 10 3 9 2 8 7 4 1 i 1 2 3 4 5 6 7 8 9 16 14 10 3 9 8 1 7 4 2 i 1 2 3 4 5 6 7 8 9 16 14 10 9 8 7 2 4 3 1 i
CPTR 430 Algorithms Heapsort
1 2 3 4 5 6 7 8 9 16 14 10 9 8 7 4 3 2 1 i 1 2 3 4 5 6 7 8 9 16 14 10 9 8 7 4 3 1 2 i
CPTR 430 Algorithms Heapsort
■ buildMaxHeap() is called once—O
■ maxHeapify() is called n
✂■ T
CPTR 430 Algorithms Heapsort
■ Quicksort (Chapter 7) is generally faster than heapsort ■ The
■ Applications of priority queues: ❚ Job scheduling on a multiprocess system (max-heap) ❚ Event-driven simulations (min-heap—time stamp) ■ For applications, a heap element normally consists of a reference
CPTR 430 Algorithms Heapsort
■ maximum(S): returns the element in S with the largest key ■ extractMax(S): removes and returns the element in S with the largest
■ insert(S
✁■ increaseKey(S
✁CPTR 430 Algorithms Heapsort
public class MaxHeap { private int size; private int[] heap; public MaxHeap(int max) { heap = new int[max]; size = 0; } public MaxHeap(int max, int[] elements) { this(max); for ( int i = 0; i < elements.length; i++ ) { insert(elements[i]); } } public void insert(int newValue) { . . . } public int maximum() { . . . } public int extractMaximum() { . . . } private static int left(int i) { . . . } private static int right(int i) { . . . } private static int parent(int i) { . . . } public void maxHeapify(int heapSize, int i) { . . . } public void buildMaxHeap() { . . . } }
CPTR 430 Algorithms Heapsort
public class MaxHeap { private int size; private int[] heap; public MaxHeap(int max) { . . . } public MaxHeap(int max, int[] elements) { . . . } public void insert(int newValue) { . . . } public int maximum() { return heap[0]; } public int extractMaximum() { . . . } private static int left(int i) { . . . } private static int right(int i) { . . . } private static int parent(int i) { . . . } public void maxHeapify(int heapSize, int i) { . . . } public void buildMaxHeap() { . . . } }
CPTR 430 Algorithms Heapsort
public class MaxHeap { private int size; private int[] heap; public MaxHeap(int max) { . . . } public MaxHeap(int max, int[] elements) { . . . } public void insert(int newValue) { . . . } public int maximum() { . . . } public int extractMaximum() { if ( size < 1 ) { System.out.println("Heap underflow"); } int max = heap[0]; heap[0] = heap[--size]; maxHeapify(size, 0); return max; } private static int left(int i) { . . . } private static int right(int i) { . . . } private static int parent(int i) { . . . } public void maxHeapify(int heapSize, int i) { . . . } public void buildMaxHeap() { . . . } }
■ First part: constant time ■ maxHeapify(): O
CPTR 430 Algorithms Heapsort
public class MaxHeap { private int size; private int[] heap; public MaxHeap(int max) { . . . } public MaxHeap(int max, int[] elements) { . . . } public void insert(int newValue) { if ( size < heap.length ) { heap[size++] = newValue; int i = size - 1; while ( i > 0 && heap[parent(i)] < heap[i] ) { int temp = heap[parent(i)]; heap[parent(i)] = heap[i]; heap[i] = temp; i = parent(i); } } else { System.out.println("Heap overflow"); } } public int maximum() { . . . } public int extractMaximum() { . . . } private static int left(int i) { . . . } private static int right(int i) { . . . } private static int parent(int i) { . . . } public void maxHeapify(int heapSize, int i) { . . . } public void buildMaxHeap() { . . . } }
■ The path from size - 1
CPTR 430 Algorithms Heapsort