Chapter 6 Heapsort - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Chapter 6 Heapsort

Description:

Heap ... The largest element in a max-heap is stored at the root ... than i. By the loop invariant, therefore, they are both roots of max-heaps. ... – PowerPoint PPT presentation

Number of Views:155
Avg rating:3.0/5.0
Slides: 31
Provided by: ccNct
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6 Heapsort


1
Chapter 6 Heapsort
2
Introduction
  • Heapsort
  • Running time O(n lg n)
  • Like merge sort
  • Sort in place only a constant number of array
    elements are stored outside the input array at
    any time
  • Like insertion sort
  • Heap
  • A data structure used by Heapsort to manage
    information during the execution of the algorithm
  • Can be used as an efficient priority queue

3
Heaps
4
Binary Heap
  • An array object that can be viewed as a nearly
    complete binary tree (see Section B.5.3)
  • Each tree node corresponds to an array element
    that stores the value in the tree node
  • The tree is completely filled on all levels
    except possibly the lowest, which is filled from
    the left up to a point
  • A has two attributes
  • lengthA of elements in the array
  • heap-sizeA of elements in the heap stored
    within A
  • heap-sizeA ? lengthA
  • No element past Aheap-sizeA is an element of
    the heap
  • max-heap and min-heap

5
A Max-Heap
6
Length and Heap-Size
7
11
11
7
Length 10 Heap-Size 7
7
Heap Computation
  • Given the index i of a node, the indices of its
    parent, left child, and right child can be
    computed simply

8
Heap Property
  • Heap property the property that the values in
    the node must satisfy
  • Max-heap property for every node i other than
    the root
  • APARENT(i) ? Ai
  • The value of a node is at most the value of its
    parent
  • The largest element in a max-heap is stored at
    the root
  • The subtree rooted at a node contains values on
    larger than that contained at the node itself
  • Min-heap property for every node i other than
    the root
  • APARENT(i) ? Ai

9
Heap Height
  • The height of a node in a heap is the number of
    edges on the longest simple downward path from
    the node to a leaf
  • The height of a heap is the height of its root
  • The height of a heap of n elements is ?(lg n)

10
Heap Procedures
  • MAX-HEAPIFY maintain the max-heap property
  • O(lg n)
  • BUILD-MAX-HEAP produces a max-heap from an
    unordered input array
  • O(n)
  • HEAPSORT sorts an array in place
  • O(n lg n)
  • MAX-HEAP-INSERT, HEAP-EXTRACT, HEAP-INCREASE-KEY,
    HEAP-MAXIMUM allow the heap data structure to be
    used as a priority queue
  • O(lg n)

11
Maintaining the Heap Property
  • MAX-HEAPIFY
  • Inputs an array A and an index i into the array
  • Assume the binary tree rooted at LEFT(i) and
    RIGHT(i) are max-heaps, but Ai may be smaller
    than its children (violate the max-heap property)
  • MAX-HEAPIFY let the value at Ai floats down in
    the max-heap

12
Example of MAX-HEAPIFY
13
MAX-HEAPIFY
Extract the indices of LEFT and RIGHT children
of i
Choose the largest of Ai, Al, Ar
Float down Ai recursively
14
Running time of MAX-HEAPIFY
  • ?(1) to find out the largest among Ai,
    ALEFT(i), and ARIGHT(i)
  • Plus the time to run MAX-HEAPIFY on a subtree
    rooted at one of the children of node i
  • The childrens subtrees each have size at most
    2n/3 the worst case occurs when the last row of
    the tree is exactly half full
  • T(n) ? T(2n/3) ?(1)
  • By case 2 of the master theorem T(n) O(lg n)

7/11
15
Building A Heap
16
Build Max Heap
  • Observation A(?n/2?1)..n are all leaves of
    the tree
  • Each is a 1-element heap to begin with
  • Upper bound on the running time
  • O(lg n) for each call to MAX-HEAPIFY, and call n
    times ? O(n lg n)
  • Not tight

17
(No Transcript)
18
Loop Invariant
  • At the start of each iteration of the for loop of
    lines 2-3, each node i1, i2, .., n is the root
    of a max-heap
  • Initialization Prior to the first iteration of
    the loop, i ?n/2?. Each node ?n/2?1,
    ?n/2?2,.., n is a leaf and the root of a trivial
    max-heap.
  • Maintenance Observe that the children of node i
    are numbered higher than i. By the loop
    invariant, therefore, they are both roots of
    max-heaps. This is precisely the condition
    required for the call MAX-HEAPIFY(A, i) to make
    node i a max-heap root. Moreover, the MAX-HEAPIFY
    call preserves the property that nodes i1, i2,
    , n are all roots of max-heaps. Decrementing i
    in the for loop update reestablishes the loop
    invariant for the next iteration
  • Termination At termination, i0. By the loop
    invariant, each node 1, 2, , n is the root of a
    max-heap. In particular, node 1 is.

19
Cost for Build-MAX-HEAP
  • Heap-properties of an n-element heap
  • Height ?lg n?
  • At most ?n/2h1? nodes of any height h

Ignore the constant ½
(for x lt 1)
20
The HeapSort Algorithm
21
Idea
  • Using BUILD-MAX-HEAP to build a max-heap on the
    input array A1..n, where nlengthA
  • Put the maximum element, A1, to An
  • Then discard node n from the heap by decrementing
    heap-size(A)
  • A2..n-1 remain max-heaps, but A1 may violate
  • call MAX-HEAPIFY(A, 1) to restore the max-heap
    property for A1..n-1
  • Repeat the above process from n down to 2
  • Cost O(n lg n)
  • BUILD-MAX-HEAP O(n)
  • Each of the n-1 calls to MAX-HEAPIFY takes time
    O(lg n)

22
Next Slide
Example of HeapSort
23
Example of HeapSort (Cont.)
1
10
1
14
14
10
9
1
14
24
Algorithm
25
Priority Queues
26
Definition
  • A priority queue is a data structure for
    maintaining a set S of elements, each with an
    associated value called a key. A max-priority
    queue supports the following operations
  • INSERT(S, x) inserts the element x into the set S
  • MAXIMUM(S) returns the element of S with the
    largest key
  • EXTRACT-MAX(S) removes and returns the element of
    S with the largest key
  • INCREASE-KEY(S, x, k) increases the value of
    element xs key to the new value k, which is
    assumed to be at least as largest as xs current
    key value
  • Application of max-priority queue Job scheduling
    in computer

27
HEAP-MAXIMUM and HEAP-EXTRACT-MAX
?(1)
O(lg n)
28
HEAP-INCREASE-KEY
  • Steps
  • Update the key of Ai to its new value
  • May violate the max-heap property
  • Traverse a path from Ai toward the root to find
    a proper place for the newly increased key

O(lg n)
29
Example of HEAP-INCREASE-KEY
30
MAX-HEAP-INSERT
O(lg n)
Write a Comment
User Comments (0)
About PowerShow.com