Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Sorting Gordon College 13.1 Some O(n2) Sorting Schemes
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Foundation of Computing Systems Lecture 6 Trees: Part III.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
Sorting Dr. Yingwu Zhu. Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order Ascending or descending Some O(n.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
2 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)
Basic Sorting Algorithms Dr. Yingwu Zhu. Sorting Problem Consider list x 1, x 2, x 3, … x n Goal: arrange the elements of the list in order Ascending.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
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.
Sorting Dr. Yingwu Zhu.
Heap Chapter 9 Objectives Define and implement heap structures
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
Heap Sort Example Qamar Abbas.
Heapsort.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Priority Queues (Heaps)
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
ADT Heap data structure
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Description Given a linear collection of items x1, x2, x3,….,xn
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Heapsort Heap & Priority Queue.
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
CMSC 341 Lecture 14 Priority Queues & Heaps
A Kind of Binary Tree Usually Stored in an Array
Tree Representation Heap.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
A Robust Data Structure
Sorting Dr. Yingwu Zhu.
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.
Ch. 12 Tables and Priority Queues
Final Review Dr. Yingwu Zhu.
Adapted from instructor resource slides
Sorting Dr. Yingwu Zhu.
Heapsort.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps By JJ Shepherd.
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.
Sorting Dr. Yingwu Zhu.
Computer Algorithms CISC4080 CIS, Fordham Univ.
Heaps and priority queues
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 36: Exam 3 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
Instructor: Dr. Michael Geiger Spring 2017 Lecture 33: Hash tables
EE 312 Software Design and Implementation I
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps

Data Structures: Lecture 30 Lecture outline Announcements/reminders Program 4 due 4/14 Program 5 to be posted; due 4/21 Today’s lecture Review: Binary trees insert/delete Sorting algorithms Heaps 7/24/2019 Data Structures: Lecture 30

Review: BST iterative search bool BST::search(const DataType &item) { BNode *p = root; bool found = false; while (!found && p != NULL) { if (item < p->data) p = p->left; else if (item > p->data) p = p->right; else found = true; } return found; 7/24/2019 Data Structures: Lecture 30

Review: BST recursive search template <typename DataType> bool BST<DataType>::search(const DataType &item) { return searchSub(root, item); } bool BST<DataType>::searchSub(BNode *subTree, const DataType &item) { if (subTree == NULL) return false; else if (item == subTree->data) return true; else if (item < subTree->data) return searchSub(subTree->left, item); else return searchSub(subTree->right, item); 7/24/2019 Data Structures: Lecture 30

Review: BST insert/delete BST insertion Modified search—find correct position for new node, then insert New nodes always inserted as leaf node (in non-balanced BST) BST deletion: 3 cases to consider Leaf node: simply set parent->leaf ptr = NULL Node w/1 child: set parent->node ptr = parent->node->child Node w/2 children: copy immediate successor to node to be deleted, then delete immediate successor Immediate successor: go R once, then L as far as possible 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Sorting Consider list x1, x2, x3, … xn We seek to arrange the elements of the list in order Ascending or descending Some O(n2) schemes easy to understand and implement inefficient for large data sets 7/24/2019 Data Structures: Lecture 30

Categories of Sorting Algorithms Selection sort Make passes through a list On each pass reposition correctly some element 7/24/2019 Data Structures: Lecture 30

Categories of Sorting Algorithms Exchange sort Systematically interchange pairs of elements which are out of order Bubble sort does this Out of order, exchange In order, do not exchange 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Bubble Sort Algorithm 1. Initialize numCompares to n - 1 2. While numCompares != 0, do following a. Set last = 1 // location of last element in a swap b. For i = 1 to numPairs if xi > xi + 1 Swap xi and xi + 1 and set last = i c. Set numCompares = last – 1 End while 7/24/2019 Data Structures: Lecture 30

Categories of Sorting Algorithms Insertion sort Repeatedly insert a new element into an already sorted list Note this works well with a linked list implementation 7/24/2019 Data Structures: Lecture 30

Algorithm for Linear Insertion Sort For i = 2 to n do the following a. set nextElement = x[i] and x[0] = nextElement b. set j = i c. While nextElement < x[j – 1] do following set x[j] equal to x[j – 1] increment j by 1 End while d. set x[j] equal to nextElement End for 7/24/2019 Data Structures: Lecture 30

Example of Insertion Sort Given list to be sorted 67, 33, 21, 84, 49, 50, 75 Note sequence of steps carried out 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Improved Schemes We seek improved computing times for sorts of large data sets Will continue with schemes which can be proven to have average computing time O( n log2n ) Must be said, no such thing as a universally good sorting scheme Results may depend just how out of order list is 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Indirect Sorts Possible that the items being sorted are large structures Data transfer/swapping time unacceptable Alternative is indirect sort Uses index table to store positions of the objects Manipulate the index table for ordering 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 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 >= data in children 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Heaps Which of the following are heaps? A B C 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Implementing a Heap Use an array or vector 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/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Implementing a Heap Note the placement of the nodes in the array Entry 0 in array kept empty to allow space in array to copy element for sorting 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Implementing a Heap In an array implementation children of ith node are at myArray[2*i] and myArray[2*i+1] Parent of the ith node is at myArray[i/2] 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Basic Heap Operations Constructor Set mySize to 0, allocate array Empty Check value of mySize Retrieve max item Return root of the binary tree, myArray[1] 7/24/2019 Data Structures: Lecture 30

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) Result called a semiheap 7/24/2019 Data Structures: Lecture 30

Percolate Down Algorithm 1. Set c = 2 * r // r = current node, // c = left child 2. While r <= n do following a. If c < n and myArray[c] < myArray[c + 1] Increment c by 1 b. If myArray[r] < myArray[c] i. Swap myArray[r] and myArray[c] ii. set r = c iii. Set c = 2 * c else Terminate repetition End while 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 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 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 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 Apply percolated down to this subtree Continue 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Heapsort Algorithm for converting a complete binary tree to a heap – called "heapify" For r = n/2 down to 1: Apply percolate_down to the subtree in myArray[r] , … myArray[n] End for Puts largest element at root 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 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 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Heapsort Again swap root with rightmost leaf Continue this process with shrinking sublist 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Heapsort Algorithm 1. Consider x as a complete binary tree, use heapify to convert this tree to a heap 2. for i = n down to 2: a. Interchange x[1] and x[i] (puts largest element at end) b. Apply percolate_down to convert binary tree corresponding to sublist in x[1] .. x[i-1] 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Final notes Next time: Priority queues Quicksort Mergesort Radix sort Reminders: Will likely post Program 4 (stacks) and Program 5 (queues) together P4 to be due next Friday (4/14), P5 due 4/21 7/24/2019 Data Structures: Lecture 30

Data Structures: Lecture 30 Acknowledgements These slides are adapted from slides provided with the course textbook: Larry Nyhoff, ADTs, Data Structures, and Problem Solving with C++, 2nd edition, 2005, Pearson/Prentice Hall. ISBN: 0-13-140909-3 7/24/2019 Data Structures: Lecture 30