Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO.

Similar presentations


Presentation on theme: "Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO."— Presentation transcript:

1 Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO

2 Recitation 1.Bubble Sort : O(n^2) 2.Insertion Sort : O(n^2) 3.Selection Sort: O(n^2) 4.Merge Sort: O(nlgn) 2

3 Importance of Sorting Why don’t CS profs ever stop talking about sorting? 1.Computers spend more time sorting than anything else, historically 25% on mainframes. 2.Sorting is the best studied problem in computer science, with a variety of different algorithms known. 3.Most of the interesting ideas we will encounter in the course can be taught in the context of sorting, such as divide-and-conquer, randomized algorithms, and lower bounds. You should have seen most of the algorithms - we will concentrate on the analysis. 3

4 Efficiency of Sorting Sorting is important because that once a set of items is sorted, many other problems become easy. Large-scale data processing would be impossible if sorting took Ω(n^2) time. 4

5 Heap Sort Running time – roughly nlog(n) – like Merge Sort – unlike Insertion Sort In place – like Insertion Sort – unlike Merge Sort Uses a heap 5

6 Binary Tree 6 : a node whose subtrees are empty depth of a node: # of edges on path to the root

7 Implementing Binary Trees 7 Relationships (left). Finding the minimum (center) & maximum (right) elements

8 Complete Binary Trees 8

9 A Binary Tree is complete if every internal node has exactly two children and all leaves are at the same depth: 9

10 Almost Complete Binary Trees An almost complete binary tree is a complete tree possibly missing some nodes on the right side of the bottom level: 10

11 Almost Complete Binary Trees 11

12 (Binary) Heaps - ADT An almost complete binary tree each node contains a key Keys satisfy the heap property: each node’s key >= its children’s keys 12

13 13 Max heap Min heap

14 Implementing Heaps by Arrays 14

15 Heapify Example Heapify(A,i) – fix Heap properties given a violation at position i 15

16 Heapify Example 16

17 Heapify Example 17

18 Heapify Example 18

19 Heapify 19 Heapify on a node of height h takes roughly dh Steps Height of the tree is logn, so Heapify on the root node takes: dlogn steps.

20 Build-Heap 20 (After BuildHeap – A[1] stores max element) We have about n/2 calls to Heapify Cost of <= dlogn - for each call to Heapify  TOTAL: d(n/2)logn But we can do better and show a cost of cn to achieve a total running time linear in n.

21 Build-Heap - Running Time 21 N=n

22 Heap-Sort Running Time: at most dnlgn for some d>0 22 // O(n) // O(lgn)

23 Several Sort Algorithms 23 http://www.sorting-algorithms.com

24 Heapsort Heapsort is an excellent algorithm, but a good implementation of quicksort, usually beats it in practice. Nevertheless, the heap data structure itself has many uses: – Priority queue (most popular) 24

25 Review - What is a Heap? 1.a almost complete tree-like structure 2.usually based on an array 3.fast access to the largest (or smallest) data item. 25

26 Priority Queue ADT Priority Queue – a set of elements S, each with a key Operations: insert(S,x) - insert element x into S S <- S U {x} max(S) - return element of S with largest key extract-max(S) - remove and return element of S with largest key 26

27 Implementing PQs by Heaps Heap-Maximum(A) 1 if heap-size[A] >= 1 2 return( A[1] ) => Running Time: constant 27

28 Heap Extract-Max Heap-Extract-Max(A) 1 if heap-size[A] < 1 2 error “heap underflow” 3 max <- A[1] 4 A[1] <- A[heap-size[A]] 5 heap-size[A] <- heap-size[A]-1 6 Heapify(A,1) 7 return max Running Time: dlgn + c = d’lgn when heap-size[A] = n 28

29 Heap Insert 29

30 Heap-Insert Heap-Insert(A,key) 1 heap-size[A] <- heap-size[A]+1 2 i <- heap-size[A] 3 while i>0 and A[parent(i)]<key 4 A[i] <- A[parent(i)] 5 i <- parent(i) 6 A[i] <- key Running Time: dlgn when heap-size[A] = n 30

31 Priority Queue Sorting PQ-Sort(A) 1 S <- Φ 2 for i <- 1 to n 3 Heap-Insert(S,A[i]) //O(lgn), O(lg(S.size)) 4 for i <- n downto 1 // O(n) 5 SortedA[i] <- Extract-Max(S) //O(lgn) 31 Running Time: at most dnlgn for some d>0 // O(n) // O(lgn) // O(n)

32 Comparison of Special-Purpose Structures 32 Use max-priority queues to schedule jobs on a shared computer. 1.It keeps track of the jobs to be performed and their relative priorities. 2.When a job is finished or interrupted, the scheduler selects the highest- priority job from among those pending by calling EXTRACT-MAX. 3.The scheduler can add a new job to the queue at any time by calling INSERT.

33 Homework 4 Hw04-GuiQtScribble Deadline: 22:00, Oct. ?, 2011 33


Download ppt "Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO."

Similar presentations


Ads by Google