Sorting and Heaps Eugene Weinstein New York University Computer Science Department Data Structures Fall 2008.

Slides:



Advertisements
Similar presentations
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Advertisements

Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Recursion & Merge Sort Introduction to Algorithms Recursion & Merge Sort CSE 680 Prof. Roger Crawfis.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
Sorting Algorithms and Average Case Time Complexity
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
COMP 171 Data Structures and Algorithms Tutorial 4 In-Class Exercises: Algorithm Design.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
CS2420: Lecture 9 Vladimir Kulyukin Computer Science Department Utah State University.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
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.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
Sorting CSIT 402 Data Structures II. 2 Sorting (Ascending Order) Input ›an array A of data records ›a key value in each data record ›a comparison function.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
HEAPS Amihood Amir Bar Ilan University Sorting Bubblesort: Until no exchanges: For i=1 to n-1 if A[i]>A[i+1] then exchange their values end Time.
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Foundations II: Data Structures and Algorithms
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node.
Introduction to Complexity Analysis. Computer Science, Silpakorn University 2 Complexity of Algorithm algorithm คือ ขั้นตอนการคำนวณ ที่ถูกนิยามไว้อย่างชัดเจนโดยจะ.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000.
Recitation Nov. 15. HW5: Huffman Encoding/Decoding Task: –Read a text file (i.e. “message.text”) and figure out character frequencies. Don’t forget ‘\n’
CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
CS 201 Data Structures and Algorithms
More on Merge Sort CS 244 This presentation is not given in class
CS 3343: Analysis of Algorithms
Binary Heaps Text Binary Heap Building a Binary Heap
Fundamental Structures of Computer Science
CE 221 Data Structures and Algorithms
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Topic 5: Heap data structure heap sort Priority queue
Presentation transcript:

Sorting and Heaps Eugene Weinstein New York University Computer Science Department Data Structures Fall 2008

2 Conceptual Merge Sort Let A[1..n] be the array to be sorted Merge(A1, A2) takes two sorted arrays and merges them into one sorted array MergeSort(A[1..n]) if (n > 1) A1 ← MergeSort(A[1..(n/2)]) A2 ← MergeSort(A[(n/2+1)..n]) return Merge(A1, A2)

3 More Real Implementation Merge sort in place in array A Merge(A, tmpA, i, j, k) // Uses tmpA to merge A[i,j] and A[j+1, k] into A[i, k] MergeSort(A, left, right) center ← (left + right)/2 MergeSort(A, tmpA, left, center) MergeSort(A, tmpA, center+1, right) Merge(A, tmpA, left, center+1, right)

4 Merge Sort Analysis Let us assume that n is a power of 2 Assumption is without loss of generality (WLOG) - i.e., even if n is an arbitrary positive integer, this still works At each level of the recursion tree, we deal with n numbers For a MergeSort call of size n, we make two calls of size n/2 A Merge of size n takes linear time, say n MergeSort of size 1 takes constant time

5 Recurrence Relation The running time for a MergeSort of size n can be written recursively. This is called a recurrence relation: ; Divide through by n:,,... Now sum together: Cancel terms and collect: Worst-case MergeSort runtime:

6 Heap Review We are concerned with min-heaps Complete binary trees obeying the heap property: key(parent) ≤ key(child) Heaps can be implemented efficiently with an array Array encoding of a binary tree: index(root) = 1 (not 0) index(left child) = 2 × index(parent) index(right child) = 2 × index(parent) + 1

7 PercolateDown percolateDown(A, hole) tmp ← A[hole] while (hole*2 ≤ currentSize) child ← hole * 2 if (child ≠ currentSize) & (A[child+1] < A[child]) child ← child + 1 if (A[child] < tmp) A[hole] ← A[child] else done hole ← child

8 PercolateUp percolateUp(A, hole) tmp ← A[hole] while (hold > 1) parent ← hole / 2 if (A[child] > tmp) A[hole] ← A[parent] else done hole ← parent

9 Heap Building Given n numbers, can we come up with an efficient algorithm to produce a heap of these n numbers? How about just making a new heap and calling insert n times? Each insert is O(log n), so total complexity is O(n log n) Can we do better? Perhaps O(n)? What if the n numbers are already given an array, can we efficiently rearrange the array to put it in heap order?

10 Array to Heap Here’s an idea: for i = 1..n percolateUp(i) Let’s think about algorithm invariant of above algorithm Invariant: Array A[1..i] is a heap n percolateUp operations, each takes O(log n) time So complexity is still O(n log n) How can we improve on this?

11 Better Efficiency Now, consider reversing the loop direction: for i = n to 1 Think of the array as a collection of mini-heaps A[n/2..n] are single-item heaps, no need to percolateDown for i = n/2 to 1 percolateDown(i) Big realization: loop in percolateDown() only runs once because for any i, all the trees in A[i+1..n] are valid heaps 11

12 Heap Building Analysis Invariant: for any i, all the trees in A[i+1..n] are valid heaps percolateDown(i) makes at most h(i) comparisons h(i) is the height of ith node from the bottom of the tree Runtime on tree of size i is bounded by T(i), sum of the heights of all nodes in complete binary tree of height i Tree of height i consists of two trees of height i-1 and a root: Solution to this recurrence: Proof credit: U. Manber, Introduction to Algorithms, 1988

13 Heap Building Analysis The runtime complexity of heap building of a heap of height h is at most In a complete binary tree of height h, there are nodes So the runtime is O(n) Proof credit: U. Manber, Introduction to Algorithms, 1988