CS 280 Data Structures Professor John Peterson. Homework Answers boolean m(int a[]) { for (int i = 0; i < a.length; i++) { for (int j = i+1; j < a.length;

Slides:



Advertisements
Similar presentations
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Advertisements

Analysis of Algorithms
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
David Luebke 1 5/20/2015 CS 332: Algorithms Quicksort.
Heapsort.
Binary Heap viewed as an array viewed as a binary tree Left(i) = 2*i Right(i) = 2*i + 1 Parent(i) = i.
CS 280 Data Structures Professor John Peterson. Quiz Wrapup interface Comparable { public boolean gtr(T x, T y); } class Student { public int studentNumber;
CS 280 Data Structures Professor John Peterson. Homework Solutions for (int i = 0; i < n; i++) for (int j = i; j < n; j++) System.out.print(i+j); for.
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
CS 280 Data Structures Professor John Peterson. Invariants Back to Invariants! Recall the insertion sort invariant – how can we turn this into debugging.
CS 280 Data Structures Professor John Peterson. Homework #1 Remember: this is due by class Wednesday! Ask questions now or by /wiki
CS 280 Data Structures Professor John Peterson. Example: log(N) This is where things get hairy! How would you compute Log 10 (N) in a very approximate.
CS 280 Data Structures Professor John Peterson. Log Complexity int j = n; while (j > 0) { System.out.println(j); j = j / 2; /* Integer division! */ }
Professor John Peterson
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
CS 280 Data Structures Professor John Peterson. Statics Understanding how and when to use statics is a really tough part of Java. What situations does.
CS 280 Data Structures Professor John Peterson. heapify void heapify(int i, Sortable s, int n) { // n is the limit of s int l = left(i); // 2*i+1 int.
CS 280 Data Structures Professor John Peterson. Homework / Project Note the homework isn’t due till tomorrow. Plus it’s not too hard. The project IS due.
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
RemoveMin We must take the value from the root node and return it to the user. Then we must remove the node. Easy array implementation: –Take the last.
2IL50 Data Structures Spring 2015 Lecture 3: Heaps.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
A Introduction to Computing II Lecture 10: Heaps Fall Session 2000.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
data ordered along paths from root to leaf
COSC 1030 Lecture 11 Sorting. Topics Sorting Themes – Low bound of sorting Priority queue methods – Selection sort – Heap sort Divide-and-conquer methods.
David Luebke 1 6/3/2016 CS 332: Algorithms Heapsort Priority Queues Quicksort.
Some comments on lab4. Hi Philippe! Can you tell me if my code works? Thanks! I’ll show you what works…
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
HEAPSORT The array A[1].. A[n] is sorted by treating the sub-array A[1].. A[p] as a heap: 1. Build A[1].. A[p] into a heap. 2. Exchange A[1] and A[p],
ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap”
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
I hope that you get along well with your teammate. If not… You can always change. A quick answer about teams.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
Lecture: Priority Queue. Questions Is array a data structure? What is a data structure? What data structures are implemented by array? Priority queue.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
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.
1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Lecture 2 Sorting.
"Teachers open the door, but you must enter by yourself. "
Lecture: Priority Queue
Heaps, Heapsort, and Priority Queues
Heaps, Heap Sort and Priority Queues
Heapsort.
Heap Sort Example Qamar Abbas.
Introduction to Algorithms
Design and Analysis of Algorithms Heapsort
CS 583 Analysis of Algorithms
Heaps, Heapsort, and Priority Queues
Heapsort.
Lecture 3 / 4 Algorithm Analysis
"Teachers open the door, but you must enter by yourself. "
Heap Sort.
Solving Recurrences Continued The Master Theorem
Heaps.
Computer Algorithms CISC4080 CIS, Fordham Univ.
Algorithms CSCI 235, Spring 2019 Lecture 14 Heap Sort Read: Ch. 6
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
Presentation transcript:

CS 280 Data Structures Professor John Peterson

Homework Answers boolean m(int a[]) { for (int i = 0; i < a.length; i++) { for (int j = i+1; j < a.length; j++) { int s = 0; for (int k = j+1; k < a.length; k++) s = s + a[k]; if (a[i] + a[j] == s) return true; }}} return false; } Best case? Worst case?

Homework Answers boolean sumIn(int s, int a[]) { return (sumIn1(s,0,a)); } boolean sumIn1(int s, int which, int a[]) { if (which >= a.length) return (s == 0); if (sumIn1(s, which+1, a)) return true; if (sumIn1(s-a[which], which+1, a) return true; return false; } Best case? Worst case? General worst case data?

Heapsort Definition: a heap is a tree in which the heap condition holds at all points: heap(n) = a[n] >= a[left(n)] && a[n] >= a[right(n)] (this ignores the case where left / right are outside the heap – we could add a condition to catch this) This is essentially an invariant – something that we want to always be true.

Arrays as a Heap Suppose we have an array {4, 2, 7, 1, 3, 6, 9, 0, 5} Draw this as a tree. Is it a heap or not?

More Heaps How about these? What do these look like in array notation?

Creating a Heap We’ll start by turning a non-heap into a heap using exchanges. We’ll do this using recursion – a method which calls itself. Let’s start with a simple problem – a “headless” heap – one in which the heap condition holds everywhere except the root of the tree.

heapify So – what should we do with the 4?

heapify So – what should we do with the 4? Swap it with the larger of its two descendents. But that means that the subtree no longer satisfies the heap condition!

heapify void heapify(int i, Sortable s, int n) { // n is the limit of s int l = left(i); // 2*i+1 int r = right(i); // 2*i+2 if (right(i) < n) { if (s.gtr(l, i) || s.gtr(r, i)) { if (s.gtr(l,r)) { s.swap(i,l); heapify(l); } else { s.swap(i,r); heapify(r); }} else if (left(i) < n) { if (s.gtr(i,l)) swap(i,l); }