COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin

Slides:



Advertisements
Similar presentations
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Advertisements

Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Outline Priority Queues Binary Heaps Randomized Mergeable Heaps.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW B Trees and B+ Trees COMP 261.
SORTING 2014-T2 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
2015-T2 Lecture 30 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
COMP 103 Course Review. 2 Menu  A final word on hash collisions in Open Addressing / Probing  Course Summary  What we have covered  What you should.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
COMP 103 Binary Search Trees II Marcus Frean 2014-T2 Lecture 26
Sorting With Priority Queue In-place Extra O(N) space
Data Structures Using C++ 2E
Heapsort CSE 373 Data Structures.
FASTER SORT using RECURSION : MERGE SORT
October 30th – Priority QUeues
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
Heap Sort Example Qamar Abbas.
Heapsort.
Binary Search Trees (I)
More complexity analysis & Binary Search
Design and Analysis of Algorithms
ADT Heap data structure
Description Given a linear collection of items x1, x2, x3,….,xn
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Priority Queue & Heap CSCI 3110 Nan Chen.
Data Structures & Algorithms Priority Queues & HeapSort
Heapsort Heap & Priority Queue.
Priority Queues.
BuildHeap & HeapSort.
Ch 6: Heapsort Ming-Te Chi
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
i206: Lecture 14: Heaps, Graphs intro.
CSC212 Data Structure - Section RS
Heap Sort The idea: build a heap containing the elements to be sorted, then remove them in order. Let n be the size of the heap, and m be the number of.
Priority Queues.
ITEC 2620M Introduction to Data Structures
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Hassan Khosravi / Geoffrey Tien
Computer Science 2 Heaps.
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Heapsort CSE 373 Data Structures.
Heapsort.
CSE 373 Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
CO 303 Algorithm Analysis and Design
EE 312 Software Design and Implementation I
Interesting Algorithms for Real World Problems
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin Lindsay Groves, Marcus Frean , Peter Andreae, and Thomas Kuehne, VUW Alex Potanin School of Engineering and Computer Science, Victoria University of Wellington 2016-T2 Lecture 29

RECAP-TODAY RECAP TODAY ANNOUNCEMENTS Slow sorts: selection sort, insertion sort, bubble sort – O(n2) Faster sorts: merge sort, quicksort – O(n log n) TODAY Sorting with binary trees: sorting with a Binary Search Tree – Tree sort sorting with a Partially Ordered Tree/heap – HeapSort ANNOUNCEMENTS No lecture on Friday

Sorting with Binary rees Binary search trees provide an efficient way to insert into an ordered sequence Mmmm – sounds a bit like insertion sort Partially ordered trees provide an efficient way of removing the smallest element in a set, and thus of extracting elements in ascending order Mmmm – sounds a bit like selection sort

Sorting with a BST: Tree Sort Binary search trees provide an efficient way to insert into an ordered sequence Insert each element into a BST Traverse the BST, copying elements one at a time to the output list Cost: O(n log n) to insert all the elements into BST O(n) to traverse the BST = O(n log n) Note: Not an in-place sort.

Sorting with a Priority Queue Put all the items to be sorted into a priority queue, using the item’s value as its priority Remove the items from the priority queue one at a time and add to the output list Output is constructed in the correct order, as in selection sort Cost: Depends on the implementation of the priority queue

Sorting with a Priority Queue What happens if we implement the priority queue as: Unordered list: Making the priority queue is easy: O(1). Selecting/removing the next item is hard: O(n). Ordered list: Making the priority queue is hard: O(n). Selecting/removing the next item is easy: O(1). Partially order tree/heap: Making the priority queue is “quite easy”: O(log n). Selecting/removing the next item is is “quite easy”: O(log n). Can we get an in-pace sort?

recap: Heap the children of node i are at (2i+1) and (2i+2) “Heap” = a complete POT, implemented in an array Bottom right node is last element used We can compute the index of parent and children of a node: the children of node i are at (2i+1) and (2i+2) the parent of node i is at (i-1)/2 note: no gaps! Bee 35 Kea 19 Eel 26 Dog 14 Fox 7 Hen 23 Ant 9 Jay 2 Gnu 13 Cat 4 1 2 3 4 5 6 7 8 9 Bee 35 Kea 19 Eel 26 Dog 14 Fox 7 Hen 23 Ant 9 Jay 2 Gnu 13 Cat 4

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! 8

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 9

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

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 = ⋮

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

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 