When is O(n lg n) Really O(n lg n)? A Comparison of the Quicksort and Heapsort Algorithms Gerald Kruse Juniata College Huntingdon, PA.

Slides:



Advertisements
Similar presentations
Heapsort O(n lg n) worst case Another design paradigm –Use of a data structure (heap) to manage information during execution of algorithm Comparision-based.
Advertisements

Analysis of Algorithms
Heapsort O(n lg n) worst case Another design paradigm –Use of a data structure (heap) to manage information during execution of algorithm Comparision-based.
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.
Analysis of Algorithms
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
David Luebke 1 5/20/2015 CS 332: Algorithms Quicksort.
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
20-Jun-15 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Lecture 5: Master Theorem and Linear Time Sorting
CS2420: Lecture 4 Vladimir Kulyukin Computer Science Department Utah State University.
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
Elementary Data Structures and Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
The Amazing Year: Münster (Germany), Covington (KY), Bethlehem (PA), and Huntingdon Gerald Kruse, Ph.D. Associate Professor of Computer Science and Mathematics.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
CSE 5311 DESIGN AND ANALYSIS OF ALGORITHMS. Definitions of Algorithm A mathematical relation between an observed quantity and a variable used in a step-by-step.
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Basic Concepts 2014, Fall Pusan National University Ki-Joune Li.
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
CS 3343: Analysis of Algorithms
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
A Lecture /24/2015 COSC3101A: Design and Analysis of Algorithms Tianying Ji Lecture 1.
1Computer Sciences Department. Book: Introduction to Algorithms, by: Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Clifford Stein Electronic:
David Luebke 1 6/3/2016 CS 332: Algorithms Heapsort Priority Queues Quicksort.
Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Analysis of Algorithms Algorithm Input Output © 2010 Goodrich, Tamassia1Analysis of Algorithms.
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
1 Analysis of Algorithms Chapter - 03 Sorting Algorithms.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
Asymptotic Notations By Er. Devdutt Baresary. Introduction In mathematics, computer science, and related fields, big O notation describes the limiting.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
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.
Data Structures Haim Kaplan & Uri Zwick December 2013 Sorting 1.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
21-Feb-16 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Heaps, Heapsort, and Priority Queues
Introduction to Algorithms
Big-O notation.
CS 583 Fall 2006 Analysis of Algorithms
CS 3343: Analysis of Algorithms
Introduction to Algorithms
Analysis of Algorithms
CS 583 Analysis of Algorithms
Heaps, Heapsort, and Priority Queues
Data Structures Sorting Haim Kaplan & Uri Zwick December 2014.
Design and Analysis of Algorithms
Topic 5: Heap data structure heap sort Priority queue
Heapsort Sorting in place
Solving Recurrences Continued The Master Theorem
CPSC 411 Design and Analysis of Algorithms
Presentation transcript:

When is O(n lg n) Really O(n lg n)? A Comparison of the Quicksort and Heapsort Algorithms Gerald Kruse Juniata College Huntingdon, PA

Outline  Analyzing Sorting Algorithms  Quicksort  Heapsort  Experimental Results  Observations (this is a fun, open-ended, student project)

How Fast is my Sorting Algorithm? “A nice blend of Math and CS”  The Sorting Problem, from Cormen et. al. Input: A sequence of n numbers, (a 1, a 2, … a n ) Output: A permutation (reordering) (a 1 ’, a 2 ’, … a n ’) of the input sequence such that a 1 ’ ≤ a 2 ’ ≤ … ≤ a n ’ Note: This definition can be expanded to include sorting primitive data such as characters or strings, alpha-numeric data, and data records with key values.  Sorting algorithms are analyzed using many different metrics: expected run-time, memory usage, communication bandwidth, implementation complexity, …  Expected running time is given using “Big-O” notation O( g(n) ) = { f(n): pos. constants c and n 0 s.t. 0 ≤ f(n) ≤ c*g(n) n ≥ n 0 }. While O-notation describes an asymptotic upper bound on a function, it is frequently used to describe asymptotically tight bounds.

Algorithm analysis also requires a model of the implementation technology to be used The most commonly used model is RAM, the Random- Access Machine. This should NOT be confused with Random-Access Memory.  Each instruction requires an equal amount of processing time  Memory hierarchy (cache, virtual memory) is NOT modeled The RAM model is relatively straightforward and “usually an excellent predictor of performance on actual machines.”

Quicksort “Good” partitioning means the partitions are usually equally sized After a partition, the element partitioned around will be in the correct position There are n compares per level, and log(n) levels, resulting in an algorithm that should run proportionally to n * lg n, taking the assumptions of the RAM model

Quicksort Pathological data leads to “bad” or unbalanced partitions and the worst- case for Quicksort The element partitioned around will be in sorted position This data will be sorted in O(n 2 ) time, since there are still n compares per level, but now there are n -1 levels.

A heap can be seen as a complete binary tree: Heaps In practice, heaps are usually implemented as arrays A =

Heaps, continued Heaps satisfy the heap property: A[Parent(i)]  A[i]for all nodes i > 1 In other words, the value of a node is at most the value of its parent. By the way, e-Bay uses a “heap-like” data structure to track bids.

Heapsort Heapsort(A){BuildHeap(A); for (i = length(A) downto 2) { Swap(A[1], A[i]); heap_size(A) -= 1; Heapify(A, 1); }} When the heap property is violated at just one node (which has sub- trees which are valid heaps), Heapify “floats down” the parent node to fix the heap. Remembering the tree structure of the heap, each Heapify call takes O(lg n) time. Since there are n – 1 calls to Heapify, Heapsort’s expected execution time is O(n lg n), just like Quicksort.

Counting Comparisons

Timing Results

Observations Implementation Implementation Run on Windows and Unix based machines, implemented in C, C++, and Java, and based on psuedo-code from: Cormen et. al., Sedgewick, and Joyce et. al. Heapsort does not run in O(n lg n) time even for the relatively small values of n tested Heapsort does not run in O(n lg n) time even for the relatively small values of n tested Quicksort does exhibit O(n lg n) behavior Quicksort does exhibit O(n lg n) behavior Consider the memory access patterns For very large n, we would expect a slowdown for ANY algorithm as the data no longer fits in memory For the size n run here, the partitions in Quicksort consist of elements which are contiguous in memory, while “floating down” a Heap requires accessing elements which are not close in memory Consider the memory access patterns For very large n, we would expect a slowdown for ANY algorithm as the data no longer fits in memory For the size n run here, the partitions in Quicksort consist of elements which are contiguous in memory, while “floating down” a Heap requires accessing elements which are not close in memory This is a fun exploration for students, appealing to those with an interest in the mathematics or computer science

Bibliography T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, “Introduction to Algorithms, Second Edition,” Cambridge, MA/London, England: The MIT Press/McGraw-Hill, N. Dale, C. Weems, D. T. Joyce, “Object-Oriented Data Structures Using Java,” Boston, MA: Jones and Bartlett, M. T. Goodrich and R. Tamassia, “Algorithm Design: Foundation, Analysis, and Internet Examples,” Wiley: New York: D. E. Knuth, “The Art of Computer Programming, Volume 3: (Second Edition) Sorting and Searching,” Addison-Wesley-Longman: Redwood City, CA, C. C. McGeoch, “Analyzing algorithms by simulation: Variance reduction techniques and simulation speedups,” ACM Computing Surveys, vol. 24, no. 2, pp. 195 – 212, C. C. McGeoch, D. Precup, and P. R. Cohen, “How to find the Big-Oh of your data set (and how not to),” Advances in Intelligent Data Analysis, vol of Lecture Notes in Computer Science, pp. 41 – 52, Springer-Verlag, R. Sedgewick, “Algorithms in C, Parts 1-4: Fundamentals, Data Structures, Sorting, Searching, Third Edition,” Addison-Wesley: Boston, MA, 1997