Presentation is loading. Please wait.

Presentation is loading. Please wait.

Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

Similar presentations


Presentation on theme: "Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a."— Presentation transcript:

1 Heapsort A minimalist's approach Jeff Chastine

2 Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a “heap”, which has several uses The word “heap” doesn’t refer to memory management Jeff Chastine

3 The Heap A binary heap is a nearly complete binary tree Implemented as an array A Two similar attributes: – length[A] is the size (number of slots) in A – heap-size[A] is the number of elements in A – Thus, heap-size[A]  length[A] – Also, no element past A[heap-size[A]] is an element Jeff Chastine

4 The Heap Can be a min-heap or a max-heap 87 5167 55434125 21173335 87 51672541554321173335 Jeff Chastine

5 Simple Functions P ARENT (i) return (i/2) L EFT (i) return (2i) R IGHT (i) return (2i + 1) Jeff Chastine

6 Properties Max-heap property: – A[P ARENT (i)]  A[i] Min-heap property: – A[P ARENT (i)]  A[i] 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) Jeff Chastine

7 M AX -H EAPIFY 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) Jeff Chastine

8 H EAPIFY M AX -H EAPIFY (A, i) l ← LEFT (i) r ← RIGHT(i) if l ≤ heap-size[A] and A[l] > A[i] then largest ← l else largest ← i if r ≤ heap-size[A] and A[r]>A[largest] then largest ← r if largest ≠ i then exchange A[i] with A[largest] M AX -H EAPIFY (A, largest) Jeff Chastine

9 16 410 93147 281 Jeff Chastine

10 16 1410 9347 281 Jeff Chastine

11 16 1410 9387 241 Jeff Chastine

12 Of Note The children’s 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) Jeff Chastine

13 B UILD -H EAP Use M AX -H EAPIFY in bottom up manner Why does the loop start at length[A]/2 ? At the start of each loop, each node i is the root of a max-heap! B UILD-HEAP (A) heap-size[A] ← length[A] for i ← length[A]/2 downto 1 do M AX -H EAPIFY (A, i) Jeff Chastine

14 Analysis of Building a Heap Since each call to M AX -H EAPIFY 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/2 h+1 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. Jeff Chastine

15 Thus, the running time is 2n = O(n) Sum up the work at each level The number of nodes at height h Multiplied by their height Jeff Chastine Height h is logarithmic

16 HEAPSORT H EAPSORT (A) B UILD- H EAP (A) for i ← length[A] downto 2 do exchange A[1] with A[i] heap-size[A] ← heap-size[A] - 1 M AX -H EAPIFY (A, 1) Jeff Chastine

17 16 1410 9387 241 Jeff Chastine

18 1 1410 9387 2416 Swap A[1]  A[i] Jeff Chastine

19 14 810 9347 2116 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

20 1 810 9347 21416 Swap A[1]  A[i] Jeff Chastine

21 10 89 1347 21416 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

22 2 89 1347 101416 Swap A[1]  A[i] Jeff Chastine

23 9 83 1247 101416 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

24 2 83 1947 101416 Swap A[1]  A[i] Jeff Chastine

25 8 73 1942 101416 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

26 1 73 8942 101416 Swap A[1]  A[i] Jeff Chastine

27 7 43 8912 101416 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

28 2 43 8917 101416 Swap A[1]  A[i] Jeff Chastine

29 4 23 8917 101416 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

30 1 23 8947 101416 Swap A[1]  A[i] Jeff Chastine

31 3 21 8947 101416 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

32 1 23 8947 101416 Swap A[1]  A[i] Jeff Chastine

33 2 13 8947 101416 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

34 1 23 8947 101416 Swap A[1]  A[i] Jeff Chastine

35 1 23 8947 101416 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

36 Priority Queues A priority queue is a heap that uses a key Common in operating systems (processes) Supports H EAP -M AXIMUM, E XTRACT -M AX, I NCREASE -K EY, I NSERT H EAP -M AXIMUM (A) 1return A[1] Jeff Chastine

37 H EAP -E XTRACT -M AX (A) 1if heap-size[A] < 1 2then error “heap underflow” 3max  A[1] 4A[1]  A[heap-size[A]] 5heap-size[A]  heap-size[A] – 1 6 M AX -H EAPIFY (A, 1) 7return max Jeff Chastine

38 H EAP -I NCREASE -K EY (A, i, key) 1if key < A[i] 2 then error “new key smaller than current” 3A[i]  key 4while i > 1 and A[P ARENT (i)] < A[i] 5do exchange A[i]  A[P ARENT (i)] 6i  P ARENT (i) Note: runs in O(lg n) Jeff Chastine

39 M AX -H EAP -I NSERT (A, key) 1heap-size[A]  heap-size[A] + 1 2A[heap-size[A]]  -  3H EAP -I NCREASE -K EY (A, heap-size[A], key) Jeff Chastine


Download ppt "Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a."

Similar presentations


Ads by Google