CS 261 Data Structures - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CS 261 Data Structures

Description:

Builds heap from initial (unsorted) data. Iteratively swaps the smallest element (at ... Adjust the heap after each swap, but only considers the unsorted data ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 13
Provided by: ericnmo
Category:

less

Transcript and Presenter's Notes

Title: CS 261 Data Structures


1
CS 261 Data Structures
  • BuildHeap and Heap Sort

2
Heap Implementation Constructors
  • void buildHeap(struct vector data)
  • int max vectorSize(data)
  • // All nodes greater than max/2 - 1 are leaves
    and thus adhere to the heap property!
  • for (int i max / 2 - 1 i gt 0 i--)
  • adjustHeap(data, max, i) // Make subtree
    rooted at i a heap.
  • At the beginning, only the leaves are proper
    heaps
  • Leaves are all nodes with indices greater than
    max / 2
  • At each step, the subtree rooted at index i
    becomes a heap

3
Heap Implementation Build heap
  • void buildHeap(struct vecctor data)
  • int max vectorSize(data)
  • // All nodes greater than max/2 - 1 are
    leaves and thus adhere to the heap property!
  • for (int i max / 2 - 1 i gt 0 i--)
  • adjustHeap(data, max, i) // Make
    subtree rooted at i a heap.
  • For all subtrees that are are not already heaps
    (initially, all inner, or non-leaf, nodes)
  • Call adjustHeap with the largest node index that
    is not already guaranteed to be a heap
  • Iterate until the root node becomes a heap
  • Why call adjustHeap with the largest non-heap
    node?
  • Because its children, having larger indices, are
    already guaranteed to be heaps

4
Heap Implementation adjustHeap
12
First iteration adjust largest non-leaf node
(index 4)
5
7
adjustHeap
4
7
8
3
i (max/2-1)
max
9
11
10
2
3 8
11
5 7
7 9
8 11
9 10
10 2
1 7
2 5
4 3
0 12
6 4
Already heaps (leaf nodes)
5
Heap Implementation adjustHeap (cont.)
12
Second iteration adjust largest non-heap node
(index 3)
5
7
adjustHeap
4
7
8
2
max
i (no adjustment needed)
9
11
10
3
3 8
11
5 7
7 9
8 11
9 10
10 3
1 7
2 5
4 2
0 12
6 4
Already heaps
6
Heap Implementation adjustHeap (cont.)
12
Third iteration adjust largest non-heap node
(index 2)
5
7
adjustHeap
4
7
8
2
i
max
9
11
10
3
3 8
11
5 7
7 9
8 11
9 10
10 3
1 7
2 5
4 2
0 12
6 4
Already heaps
7
Heap Implementation adjustHeap (cont.)
12
Fourth iteration adjust largest non-heap node
(index 1)
adjustHeap
4
7
5
7
8
2
max
9
11
10
3
3 8
11
5 7
7 9
8 11
9 10
10 3
1 7
2 4
4 2
0 12
6 5
Already heaps
i
8
Heap Implementation adjustHeap (cont.)
12
Fifth iteration adjust largest non-heap node
(index 0 ? root)
adjustHeap
4
2
5
7
8
3
max
9
11
10
7
3 8
11
5 7
7 9
8 11
9 10
10 7
1 2
2 4
4 3
0 12
6 5
Already heaps
i
9
Heap Implementation adjustHeap (cont.)
2
4
3
5
7
8
7
9
11
10
12
3 8
5 7
7 9
8 11
9 10
10 12
1 3
2 4
4 7
0 2
6 5
Already heaps (entire tree)
10
Heap Implementation sort
  • void heapSort(Vector data)
  • buildHeap(data)
    // Build initial heap.
  • for (int i vectorSize(data)1 i gt 0 i--)
    // For each of the n elements
  • EleType tmp vectorGet(data,i)
    // Swap last element with
  • vectorPut(data, I vectorGet(data, 0))
    // the first (smallest) element.
  • vectorPut(data, 0, temp) //
    So, sorts data in reverse order.
  • adjustHeap(data, i, 0)
    // Rebuild heap property.
  • Sorts the data in descending order (from largest
    to smallest)
  • Builds heap from initial (unsorted) data
  • Iteratively swaps the smallest element (at index
    0) with last unsorted element
  • Adjust the heap after each swap, but only
    considers the unsorted data

11
View from Middle of Execution
12
Heap Analysis sort
  • Execution time
  • Build heap n calls to adjustHeap n log n
  • Loop n calls to adjustHeap n log n
  • Total 2n log n O(n log n)
  • Advantages/disadvantages
  • Same average as merge sort and quick sort
  • Doesnt require extra space as the merge sort
    does
  • Doesnt suffer if data is already sorted or
    mostly sorted
Write a Comment
User Comments (0)
About PowerShow.com