Heapsort - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Heapsort

Description:

Based off of a 'heap', which has several uses ... Max-heaps are used for sorting. Min-heaps are used for priority queues (later) ... log n time? ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 40
Provided by: jeffch8
Category:

less

Transcript and Presenter's Notes

Title: Heapsort


1
Heapsort
  • A minimalist's approach

2
Heapsort
  • Like MERGESORT, it runs in O(n lg n)
  • Unlike MERGESORT, it sorts in place
  • Based off of a heap, which has several uses
  • The word heap doesnt refer to memory management

3
The Heap
  • A binary heap is a nearly complete binary tree
  • Implemented as an array A
  • Two similar attributes
  • lengthA is the size (number of slots) in A
  • heap-sizeA is the number of elements in A
  • Thus, heap-sizeA ? lengthA
  • Also, no element past Aheap-sizeA is an
    element

4
The Heap
  • Can be a min-heap or a max-heap

87
51
67
55
43
41
25
87
21
17
33
35
87
51
67
25
41
55
43
21
17
33
35
5
Simple Functions
  • PARENT(i)
  • return (i/2)
  • LEFT(i)
  • return (2i)
  • RIGHT(i)
  • return (2i 1)

6
Properties
  • Max-heap property
  • APARENT(i) ? Ai
  • Min-heap property
  • APARENT(i) ? Ai
  • Max-heaps are used for sorting
  • Min-heaps are used for priority queues (later)
  • We define the height of a node to be the longest
    path from the node to a leaf.
  • The height of the tree is ?(lg n)

7
MAX-HEAPIFY
  • This is the heart of the algorithm
  • Determines if an individual node is smaller than
    its children
  • Parent swaps with largest child if that child is
    larger
  • Calls itself recursively
  • Runs in O(lg n) or O(h)

8
HEAPIFY
  • MAX-HEAPIFY (A, i)
  • l ? LEFT (i)
  • r ? RIGHT(i)
  • if l heap-sizeA and Al gt Ai
  • then largest ? l
  • else largest ? i
  • if r heap-sizeA and ArgtAlargest
  • then largest ? r
  • if largest ? i
  • then exchange Ai with Alargest
  • MAX-HEAPIFY (A, largest)

9
16
4
10
9
3
14
7
2
8
1
10
16
14
10
9
3
4
7
2
8
1
11
16
14
10
9
3
8
7
2
4
1
12
Of Note
  • The childrens subtrees each have size at most
    2n/3 when the last row is exactly ½ full
  • Therefore, the running time is
  • T (n) T(2n/3) ?(1) O(lg n)

13
BUILD-HEAP
  • Use MAX-HEAPIFY in bottom up manner
  • Why does the loop start at lengthA/2?
  • At the start of each loop, each node i is the
    root of a max-heap!
  • BUILD-HEAP (A)
  • heap-sizeA ? lengthA
  • for i ? lengthA/2 downto 1
  • do MAX-HEAPIFY(A, i)

14
Analysis of Building a Heap
  • Since each call to MAX-HEAPIFY costs O(lg n) and
    there are O(n) calls, this is O(n lg n)...
  • Can derive a tighter bound do all nodes take
    log n time?
  • Has at most n/2h1 nodes at any height (the more
    the height, the less nodes there are)
  • It takes O(h) time to insert a node of height h.

15
  • Thus, the running time is 2n O(n)

The number of nodes at height h
Multiplied by their height
16
HEAPSORT
  • HEAPSORT (A)
  • BUILD-HEAP(A)
  • for i ? lengthA downto 2
  • do exchange A1 with Ai
  • heap-sizeA ? heap-sizeA - 1
  • MAX-HEAPIFY(A, 1)

17
16
14
10
9
3
8
7
2
4
1
18
1
14
10
9
3
8
7
2
4
16
Swap A1 ? Ai
19
14
8
10
9
3
4
7
2
1
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
20
1
8
10
9
3
4
7
2
14
16
Swap A1 ? Ai
21
10
8
9
1
3
4
7
2
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
22
2
8
9
1
3
4
7
10
14
16
Swap A1 ? Ai
23
9
8
3
1
2
4
7
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
24
2
8
3
1
9
4
7
10
14
16
Swap A1 ? Ai
25
8
7
3
1
9
4
2
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
26
1
7
3
8
9
4
2
10
14
16
Swap A1 ? Ai
27
7
4
3
8
9
1
2
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
28
2
4
3
8
9
1
7
10
14
16
Swap A1 ? Ai
29
4
2
3
8
9
1
7
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
30
1
2
3
8
9
4
7
10
14
16
Swap A1 ? Ai
31
3
2
1
8
9
4
7
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
32
1
2
3
8
9
4
7
10
14
16
Swap A1 ? Ai
33
2
1
3
8
9
4
7
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
34
1
2
3
8
9
4
7
10
14
16
Swap A1 ? Ai
35
1
2
3
8
9
4
7
10
14
16
heap-size ? heap-size 1 MAX-HEAPIFY(A, 1)
36
Priority Queues
  • A priority queue is a heap that uses a key
  • Common in operating systems (processes)
  • Supports HEAP-MAXIMUM, EXTRACT-MAX, INCREASE-KEY,
    INSERT
  • HEAP-MAXIMUM (A)
  • 1 return A1

37
  • HEAP-EXTRACT-MAX (A)
  • 1 if heap-sizeA lt 1
  • 2 then error heap underflow
  • 3 max ? A1
  • 4 A1 ? Aheap-sizeA
  • 5 heap-sizeA ? heap-sizeA 1
  • 6 MAX-HEAPIFY (A, 1)
  • 7 return max

38
  • HEAP-INCREASE-KEY(A, i, key)
  • 1 if key lt Ai
  • 2 then error new key smaller than current
  • 3 Ai ? key
  • 4 while i gt 1 and APARENT(i) lt Ai
  • 5 do exchange Ai ? APARENT(i)
  • 6 i ? PARENT(i)

Note runs in O(lg n)
39
  • MAX-HEAP-INSERT (A, key)
  • 1 heap-sizeA ? heap-sizeA 1
  • 2 Aheap-sizeA ? -?
  • 3 HEAP-INCREASE-KEY(A, heap-sizeA, key)
Write a Comment
User Comments (0)
About PowerShow.com