Week 14 - Monday.  What did we talk about last time?  Heaps  Priority queues  Heapsort.

Slides:



Advertisements
Similar presentations
Sorting in Linear Time Introduction to Algorithms Sorting in Linear Time CSE 680 Prof. Roger Crawfis.
Advertisements

Analysis of Algorithms
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Bucket Sort and Radix Sort CS /02/05 BucketSort Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
Data Structures and Algorithms (60-254)
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CSCE 3110 Data Structures & Algorithm Analysis
COSC 3100 Transform and Conquer
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Sorting HKOI Training Team (Advanced)
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Binary Heap.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2012.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Chapter 21 Binary Heap.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
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.
Sorting CS /02/05 L12: Sorting Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved The.
Sorting CS 105 See Chapter 14 of Horstmann text. Sorting Slide 2 The Sorting problem Input: a collection S of n elements that can be ordered Output: the.
Survey of Sorting Ananda Gunawardena. Naïve sorting algorithms Bubble sort: scan for flips, until all are fixed Etc...
Sorting CS 110: Data Structures and Algorithms First Semester,
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Bucket Sort and Radix Sort
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
1 Radix Sort. 2 Classification of Sorting algorithms Sorting algorithms are often classified using different metrics:  Computational complexity: classification.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Heaps & Priority Queues
Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
Week 15 – Friday.  What did we talk about last time?  Student questions  Review up to Exam 2  Recursion  Binary trees  Heaps  Tries  B-trees.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Week 13: Searching and Sorting
Week 15 – Friday CS221.
Week 12 - Wednesday CS221.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Analysis of Algorithms
Amortized Analysis and Heaps Intro
Heaps & Multi-way Search Trees
Week 13 - Wednesday CS221.
Presentation transcript:

Week 14 - Monday

 What did we talk about last time?  Heaps  Priority queues  Heapsort

 Pros:  Best, worst, and average case running time of O(n log n)  In-place  Good for arrays  Cons:  Not adaptive  Not stable

 Make the array have the heap property: 1. Let i be the index of the parent of the last two nodes 2. Bubble the value at index i down if needed 3. Decrement i 4. If i is not less than zero, go to Step 2 1. Let pos be the index of the last element in the array 2. Swap index 0 with index pos 3. Bubble down index 0 4. Decrement pos 5. If pos is greater than zero, go to Step 2

 Heap sort is a clever algorithm that uses part of the array to store the heap and the rest to store the (growing) sorted array  Even though a priority queue uses both bubble up and bubble down methods to manage the heap, heap sort only needs bubble down  You don't need bubble up because nothing is added to the heap, only removed

 Timsort is a recently developed sorting algorithm used as the default sort in Python  It is also used to sort non-primitive arrays in Java  It's a hybrid sort, combining elements of merge sort and insertion sort  Features  Worst case and average case running time: O(n log n)  Best case running time: O(n)  Stable  Adaptive  Not in-place

 It is similar to when we insertion sorted arrays of length 10 or smaller  We also want to find "runs" of data of two kinds:  Non-decreasing:34, 45, 58, 58, 91  Strictly decreasing:85, 67, 24, 18, 7  These runs are already sorted (or only need a reversal)  If runs are not as long as a minimum run length determined by the algorithm, the next few values are added in and sorted  Finally, the sorted runs are merged together  The algorithm can use a specially tuned galloping mode when merging from two lists  Essentially copying in bulk from one list when it knows that it won't need something from the other for a while

 It might be useful to implement Timsort in class, but it has a lot of special cases  It was developed from both a theoretical perspective but also with a lot of testing  If you want to know more, read here:  Development/Algorithms/Timsort-Sorting- Algorithm/ Development/Algorithms/Timsort-Sorting- Algorithm/

 Understanding how sorts work can be challenging  Understanding how running time is affected by various algorithms and data sets is not obvious  To help, there are many good visualizations of sorting algorithms in action:   risonSort.html risonSort.html

 Lets focus on an unusual sort that lets us (potentially) get better performance than O(n log n)  But, I thought O(n log n) was the theoretical maximum!

 You use counting sort when you know that your data is in a narrow range, like, the numbers between 1 and 10 or even 1 and 100  As long as the range of possible values is in the neighborhood of the length of your list, counting sort can do well  Example: 150 students with integer grades between 1 and 100  Doesn’t work for sorting double or String values

 Make an array with enough elements to hold every possible value in your range of values  If you need 1 – 100, make an array with length 100  Sweep through your original list of numbers, when you see a particular value, increment the corresponding index in the value array  To get your final sorted list, sweep through your value array and, for every entry with value k > 0, print its index k times

 We know our values will be in the range [1,10]  Our example array:  Our values array:  The result:

 It takes O(n) time to scan through the original array  But, now we have to take into account the number of values we expect  So, let’s say we have m possible values  It takes O(m) time to scan back through the value array, with O(n) additional updates to the original array  Time: O(n + m)

 We can “generalize” counting sort somewhat  Instead of looking at the value as a whole, we can look at individual digits (or even individual characters)  For decimal numbers, we would only need 10 buckets (0 – 9)  First, we bucket everything based on the least significant digits, then the second least, etc.  The book discusses MSD and LSD string sorts, which are similar

 Pros:  Best, worst, and average case running time of O(nk) where k is the number of digits  Stable for least significant digit (LSD) version  Simple implementation  Cons:  Requires a fixed number of digits to be checked  Unstable for most significant digit (MSD) version  Works poorly for floating point and non-digit based keys  Not in-place

 For integers, make 10 buckets (0-9)  Each bucket contains a queue  Starting with the 1’s place and going up a place each time:  Enqueue each item into the bucket whose value matches the value of the item at a particular place  Starting with bucket 0, and going up to bucket 9, dequeue all the items into the original array

 Tries

 Work on Project 4  Work on Assignment 7  Due when you return from Thanksgiving  Read section 5.2  Office hours are canceled today because of a visiting faculty candidate