CSE Algorithms Quicksort vs Heapsort: the “inside” story or

Slides:



Advertisements
Similar presentations
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Advertisements

COSC 3100 Transform and Conquer
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.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
10/10/2002CSE Memory Hierarchy CSE Algorithms Quicksort vs Heapsort: the “inside” story or A Two-Level Model of Memory.
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
External Sorting Chapter 13.. Why Sort? A classic problem in computer science! Data requested in sorted order  e.g., find students in increasing gpa.
4/10/2003CSE Quick&Heap CSE Algorithms Quicksort vs Heapsort: the “official” story Next time, we’ll get the “inside” story!
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
Sorting with Heaps Observation: Removal of the largest item from a heap can be performed in O(log n) time Another observation: Nodes are removed in order.
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.
1 CSE 326: Data Structures Sorting It All Out Henry Kautz Winter Quarter 2002.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Memory Characteristics Location Capacity Unit of transfer Access method Performance Physical type Physical characteristics Organisation.
HeapSort 25 March HeapSort Heaps or priority queues provide a means of sorting: 1.Construct a heap, 2.Add each item to it (maintaining the heap.
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.
Data Structure and Algorithms
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 External Sorting Chapter 13.
Heap Sort Uses a heap, which is a tree-based data type Steps involved: Turn the array into a heap. Delete the root from the heap and insert into the array,
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
CMSC 611: Advanced Computer Architecture
"Teachers open the door, but you must enter by yourself. "
Memory Management 5/11/2018 9:49 PM
Red Black Trees Colored Nodes Definition Binary search tree.
B-Trees B-Trees.
Memory Management 6/20/ :27 PM
Experimental evaluation of Navigation piles
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries.
CSE 332 Data Abstractions B-Trees
Cache Memory Presentation I
Interval Heaps Complete binary tree.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
B-Trees (continued) Analysis of worst-case and average number of disk accesses for an insert. Delete and analysis. Structure for B-tree node.
Dr. David Matuszek Heapsort Dr. David Matuszek
Ch 6: Heapsort Ming-Te Chi
B- Trees D. Frey with apologies to Tom Anastasio
Lecture 3 / 4 Algorithm Analysis
B- Trees D. Frey with apologies to Tom Anastasio
"Teachers open the door, but you must enter by yourself. "
Lecture 2- Query Processing (continued)
Heapsort.
B- Trees D. Frey with apologies to Tom Anastasio
Linear-Time Sorting Algorithms
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries.
Topic 5: Heap data structure heap sort Priority queue
CSE 373: Data Structures and Algorithms
Heapsort Sorting in place
Binary SearchTrees [CLRS] – Chap 12.
CENG 351 Data Management and File Structures
Lecture 13: Computer Memory
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Heapsort.
Cache - Optimization.
Heapsort.
Heaps & Multi-way Search Trees
B-Trees Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries.
CO 303 Algorithm Analysis and Design
EE 312 Software Design and Implementation I
Presentation transcript:

CSE 202 - Algorithms Quicksort vs Heapsort: the “inside” story or A Two-Level Model of Memory 10/17/2003 CSE 202 - Memory Hierarchy

Where are we Traditional (RAM-model) analysis: Heapsort is better Heapsort worst-case complexity is (n lg n) Quicksort worst-case complexity is (n2). average-case complexity should be ignored. probabilistic analysis of randomized version is (n lg n) Yet Quicksort is popular. Goal: a better model of computation. It should reflect the real-world costs better. Yet should be simple enough to perform asymptotic analysis. CSE 202 - Memory Hierarchy

2-level memory hierarchy model (MH2) Data moves in “blocks” from Main Memory to cache. A block is b contiguous items. It takes time b to move a block into cache. Cache can hold only b blocks. Least recently used block is evicted. Individual items are moved from Cache to CPU. Takes 1 unit of time. Main Memory Cache CPU Note - “b” affects: block size cache capacity (b2) transfer time CSE 202 - Memory Hierarchy

2-level memory hierarchy model (MH2) For asymptotic analysis, we want b to grow with n b = n1/3 or n1/4 are plausible choices block size = b (Bytes) cache size = b2 (Bytes) transfer (cycles) memory = n (Bytes) Memory = DRAM Cache = SRAM 26 - 28 213 - 220 25 -27 226 - 230 b = n1/4 27 214 228 Memory = disk Cache = Dram 212 - 213 215 – 220 233 – 238 b=n1/3 213 226 239 CSE 202 - Memory Hierarchy

Cache lines of heap (b=8, n=511, h=9) 3 4 5 6 7 6 levels, 8 blocks 8 9 10 11 12 13 14 15 16 17 .... 23 24 25 .... 31 32 ... 39 40 ... 47 48 ... 55 56 ... 63 64..71 ... ... ... ... ... ... ... 127 h/3 levels 128.. . . . . . . .255 256 . . 511 CSE 202 - Memory Hierarchy

A worst-case Heapsort instance Each Extract-Max goes all the way to a leaf. Visits to each node alternate between left and right child. Actually, for any sequence of paths from root to leaves, one can create example. Construct starting with 1-node heap 15 14 13 10 12 9 11 2 6 4 8 7 1 5 3 CSE 202 - Memory Hierarchy

MH2 analysis of Heapsort Assume b = n1/3. Similar analysis works for b = na, 0 < a < ½. Effect of LRU replacement: First n2/3 heap elements will “usually” be in cache. Let h = lg n be height of the tree. These elements are all in top (2/3)h of tree. Remaining elements won’t usually be in cache. In worst case example, they will never be in cache when you need them. Intuition: Earlier blocks of heap are more likely to be references than a later one. When we kick out an early block to bring in a later one, we increase misses later. CSE 202 - Memory Hierarchy

MH2 analysis of Heapsort (worst-case) Every access below level (2/3)h is a miss. Each of the first n/2 Extract-max’s “bubbles down” to the leaves. So each has at least (h/3)-1 misses. Each miss takes time b. Thus, T(n) > (n/2) ((h/3)-1) b. Recall: b = n1/3 and h = lg n. Thus, T(n) is (n4/3 lg n). And obviously, T(n) is O(n4/3 lg n) . Each of c n lg n accesses takes time at most b = n1/3. (where c is constant from RAM analysis of Heapsort). CSE 202 - Memory Hierarchy

Quicksort MH2 complexity Accesses in Quicksort are sequential Sometimes increasing, sometimes decreasing When you bring in a block of b elements, you access every element. Not 100% true, but I’ll wave my hands We take b time to get block for b accesses Thus, time in MH2 model is same as RAM. Θ(n lg n) Bottom Line: MH2 analysis shows Quicksort has lower complexity than Heapsort! CSE 202 - Memory Hierarchy