Sorting - PowerPoint PPT Presentation

About This Presentation
Title:

Sorting

Description:

Implementing a Heap. Use an array or vector, why? Number the ... Basic Heap Operations. Delete max item. Max item is the root, replace with last node in tree ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 28
Provided by: yingw1
Category:
Tags: heaps | sorting

less

Transcript and Presenter's Notes

Title: Sorting


1
Sorting
  • Dr. Yingwu Zhu

2
Heaps
  • A heap is a binary tree with properties
  • It is complete
  • Each level of tree completely filled
  • Except possibly bottom level (nodes in left most
    positions)
  • It satisfies heap-order property
  • Data in each node gt data in children

3
Heaps
  • Which of the following are heaps?

C
B
A
4
Heaps
  • Maxheap? by default
  • Minheap?

5
Implementing a Heap
  • What data structure is good for its
    implementation?

6
Implementing a Heap
  • Use an array or vector, why?
  • Number the nodes from top to bottom
  • Number nodes on each row from left to right
  • Store data in ith node in ith location of array
    (vector)

7
Implementing a Heap
  • Note the placement of the nodes in the array

8
Implementing a Heap
  • In an array implementation children of ith node
    are at myArray2i and myArray2i1
  • Parent of the ith node is at mayArrayi/2
  • How about in C, position starting from 0?

9
Basic Heap Operations
  • Constructor
  • Set mySize to 0, allocate array
  • Empty
  • Check value of mySize
  • Retrieve max item
  • Return root of the binary tree, myArray1

10
Basic Heap Operations
  • Delete max item
  • Max item is the root, replace with last node in
    tree
  • Then interchange root with larger of two children
  • Continue this with the resulting sub-tree(s)
  • Semiheap 1 complete 2 both subtrees are heaps

11
Implementing Heap
  • define CAP 10000
  • template lttypename Tgt
  • class Heap private T myArrayCAP
  • int mySize
  • private
  • void percolate_down(int pos) //percolate
    down
  • void percolate_up(int pos) //percolate up
  • public Heap()mySize(0) Heap()
  • void deleteMax() //remove the max element
  • void insert(const T item) //insert an
    item

12
Percolate Down Algorithm
  • 1. Set c 2 r 1
  • 2. While c lt n do following //what does this
    mean?
  • a. If c lt n-1 and myArrayc lt myArrayc
    1 Increment c by 1b. If myArrayr lt
    myArrayc i. Swap myArrayr and
    myArrayc ii. set r c iii. Set c 2 c
    1else Terminate repetitionEnd while

13
Basic Heap Operations
  • Insert an item
  • Amounts to a percolate up routine
  • Place new item at end of array
  • Interchange with parent so long as it is greater
    than its parent

14
Percolate Up Algorithm
  • Why percolate up?
  • When to terminate the up process?
  • void HeapltTgtpercolate_up()
  • void HeapltTgtinsert(const T item)

15
How to do remove?
  • Remove an item from the heap?
  • Question A leaf node must be less or equal than
    any internal node in a heap?

16
Heapsort
  • Given a list of numbers in an array
  • Stored in a complete binary tree
  • Convert to a heap
  • Begin at last node not a leaf pos (size-2)/2?
  • Apply percolated down to this subtree
  • Continue

17
Heapsort
  • Algorithm for converting a complete binary tree
    to a heap called "heapify"For r (n-1-1)/2
    down to 0
  • apply percolate_down to the subtree in
    myArrayr , myArrayn-1End for
  • Puts largest element at root
  • Do you understand it? Think why?

18
Heapsort
  • Why?
  • Heap is a recursive ADT
  • Semiheap ? heap from bottom to up
  • Percolate down for this conversion

19
Heapsort
  • Now swap element 1 (root of tree) with last
    element
  • This puts largest element in correct location
  • Use percolate down on remaining sublist
  • Converts from semi-heap to heap

20
Heapsort
  • Again swap root with rightmost leaf
  • Continue this process with shrinking sublist

21
Summary of HeapSort
  • Step 1 put the data items into an array
  • Step 2 Heapify this array into a heap
  • Step 3 Exchange the root node with the last
    element and shrink the list by pruning the last
    element.
  • Now it is a semi-heap
  • Apply percolate-down algorithm
  • Go back step 3

22
Heapsort Algorithm
  • 1. Consider x as a complete binary tree, use
    heapify to convert this tree to a heap
  • 2. for i n-1 down to 1a. Interchange x0 and
    xi (puts largest element at end)b. Apply
    percolate_down to convert binary tree
    corresponding to sublist in x0 .. xi-1

23
Heapsort
  • Fully understand how heapsort works!
  • T(n) O(nlogn)
  • Why?

24
Heap Algorithms in STL
  • Found in the ltalgorithmgt library
  • make_heap() heapify
  • push_heap() insert
  • pop_heap() delete
  • sort_heap() heapsort
  • Note program which illustrates these operations,
    Fig. 13.1

25
Priority Queue
  • A collection of data elements
  • Items stored in order by priority
  • Higher priority items removed ahead of lower
  • Operations
  • Constructor
  • Insert
  • Find, remove smallest/largest (priority) element
  • Replace
  • Change priority
  • Delete an item
  • Join two priority queues into a larger one

26
Priority Queue
  • Implementation possibilities
  • As a list (array, vector, linked list)
  • T(n) for search, removeMax, insert operations
  • As an ordered list
  • T(n) for search, removeMax, insert operations
  • Best is to use a heap
  • T(n) for basic operations
  • Why?

27
Priority Queue
  • STL priority queue adapter uses heap
  • Note operations in table of Fig. 13.2 in text,
    page 751
Write a Comment
User Comments (0)
About PowerShow.com