Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27

Similar presentations


Presentation on theme: "COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27"— Presentation transcript:

1 COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
Lindsay Groves, Marcus Frean , Peter Andreae, and Thomas Kuehne, VUW Thomas Kuehne School of Engineering and Computer Science, Victoria University of Wellington 2013-T1 Lecture 27

2 RECAP-TODAY RECAP TODAY Priority Queue Heap HeapSort Array-based Heap
TreeSort is a fast InsertionSort HeapSort is a fast SelectionSort

3 TreeSort is like InsertionSort with a fast structure to insert into
Remember: Tree Sort TreeSort is like InsertionSort with a fast structure to insert into To sort a list: Insert each item into a BST Traverse the BST, reading out elements one by one Note: not an "in-place" sort Cost is O(n log n), if the tree is kept balanced, otherwise O(n2) worst-case Sorted list is the worst for it! While insertion sort is the best for sorted list. 3

4 Priority Queue Based Sort
If we can use a binary search tree for sorting, can we use a priority queue for sorting? Yes  High-level idea: enqueue all items into a priority queue dequeue items from priority queue one by one Priority Queue Sort is like SelectionSort with a fast structure to select items from 4

5 Priority Queue Based Sort: Complexity
Add an element to a priority queue: O(log(n)) Remove an element from a priority queue: O(log(n)) Do it n times  O(n log n) No O(n2) worst-case scenario! This used to say the following, but I don’t understand it. (LG) Doing it in place is a little more tricky: For each non-leaf node (n/2..0) Compare to its children and pushdown if needed. 5

6 Heapsort: In-Place Sorting
Use an array-based Heap in-place sorting algorithm! turn the array into a heap “remove” top element, and restore heap property again repeat step 2. n-1 times in-place dequeueing This used to say the following, but I don’t understand it. (LG) Doing it in place is a little more tricky: For each non-leaf node (n/2..0) Compare to its children and pushdown if needed. Sorted → 35 19 26 13 14 23 4 9 7 1 1 2 3 4 5 6 7 8 9 and so on! 6

7 Heapsort: In-Place Sorting
How to turn the array into a heap? Heapify for i = lastParent down to 0 sinkdown(i) (n-2)/2 heap property installed 13 9 23 7 14 26 4 19 35 1 1 2 3 4 5 6 7 8 9 7

8 HeapSort: Algorithm (a) Turn data into a heap
(b) Repeatedly swap root with last item and push down public void heapSort(E[] data, int size, Comparator<E> comp) { for (int i = (size-2)/2; i >= 0; i--) sinkDown(i, data, size, comp); while (size > 0) { size--; swap(data, size, 0); sinkDown(0, data, size, comp); } "heapify" in-place dequeueing

9 Cost of “heapify” (n/2log (n+1)-1)  (log(n+1)-1) n/82 n/41 n/20
Cost = n [1/4 + 2/8 + 3/16 + 4/32 + ⋯ (log(n+1)-1)/n] swaps =

10 Cost of “heapify” Cost = n  [ 1/4 + 2/8 + 3/16 + 4/32 + ⋯]
≈ n  [1] = n swaps We can turn an unordered list into a heap in linear time!!! 1st row 2nd row 3rd row 1st row 2nd row 3rd row

11 HeapSort: Summary Cost of heapify = O(n)
n  Cost of remove = O(n log n) Total cost = O(n log n) True for worst-case and average case! unlike QuickSort and TreeSort Can be done in-place Unlike MergeSort, doesn’t need extra memory to be fast Not stable 


Download ppt "COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27"

Similar presentations


Ads by Google