CS 280 Data Structures Professor John Peterson. Test #1 We’ll do a test next week on Wednesday. It will take the entire period. You can have 1 page of.

Slides:



Advertisements
Similar presentations
Chapter 9 continued: Quicksort
Advertisements

Introduction to Algorithms Quicksort
Order Statistics Sorted
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
CSC 213 – Large Scale Programming or. Today’s Goals  Begin by discussing basic approach of quick sort  Divide-and-conquer used, but how does this help?
Order Statistics(Selection Problem) A more interesting problem is selection:  finding the i th smallest element of a set We will show: –A practical randomized.
Introduction to Algorithms Jiafen Liu Sept
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Introduction to Algorithms Chapter 7: Quick Sort.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
CS 171: Introduction to Computer Science II Quicksort.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
CS 280 Data Structures Professor John Peterson. Project “Tree 1” Questions? Let’s look at my test cases.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last.
CS 280 Data Structures Professor John Peterson. Project Questions? /CIS280/f07/project5http://wiki.western.edu/mcis/index.php.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
CS 280 Data Structures Professor John Peterson. Test #1 We’ll do a test next week on Wednesday. It will take the entire period. You can have 1 page of.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
CS 280 Data Structures Professor John Peterson. Project Questions?
Quicksort.
CS 280 Data Structures Professor John Peterson. Goals Understand “Programming in the small” Java programming Know what’s under the hood in complex libraries.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
CS 300 – Lecture 20 Intro to Computer Architecture / Assembly Language Caches.
CS 280 Data Structures Professor John Peterson. Test #1 We’ll do a test next week on Wednesday. It will take the entire period. You can have 1 page of.
CS 280 Data Structures Professor John Peterson. Goals Understand “Programming in the small” Java programming Know what’s under the hood in complex libraries.
Sorting Chapter 10.
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
Quicksort
CS 280 Data Structures Professor John Peterson. Project Questions? /CIS280/f07/project5http://wiki.western.edu/mcis/index.php.
S: Application of quicksort on an array of ints: partitioning.
Sorting Rearrange n elements into ascending order. 7, 3, 6, 2, 1  1, 2, 3, 6, 7.
CS 280 Data Structures Professor John Peterson. Test #1 We’ll do a test next week on Wednesday. It will take the entire period. You can have 1 page of.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
CS 280 Data Structures Professor John Peterson. Grading the Projects Boo-boos: Not allowing bubble sort to exit soon enough Mistakes in the heapify, especially.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
Divide-And-Conquer Sorting Small instance.  n
1 More Sorting radix sort bucket sort in-place sorting how fast can we sort?
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
Chapter 9: Selection Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Order Statistics David Kauchak cs302 Spring 2012.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Order Statistics(Selection Problem)
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
Quicksort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
Independent Reading Day #1 (Sad, but true.). Let’s do an experiment: Figure out a starting page number for today’s reading – It might not be page one.
QuickSort Choosing a Good Pivot Design and Analysis of Algorithms I.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
Sorting Lesson CS1313 Spring Sorting Lesson Outline 1.Sorting Lesson Outline 2.How to Sort an Array? 3.Many Sorting Algorithms 4.Bubble Sort #1.
Data Structure & Algorithm Lecture 6 – QuickSort & Randomization JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
CS 1114: Sorting and selection (part two)
Presentation transcript:

CS 280 Data Structures Professor John Peterson

Test #1 We’ll do a test next week on Wednesday. It will take the entire period. You can have 1 page of notes. Think about all of the homework / projects / quizzes we’ve had. I’ll be doing something similar. There will be a little programming. I have graded homework! I’m slowly getting projects graded – watch your .

Call Trees A “call tree” shows the different calls to methods and their arguments. For example, in sorting {2,4,3,7,1,8}, if the pivot is 4, this will generate calls to qs {2,3,1} and qs {7, 8}. I’m not worried about the ordering of elements coming out of partition – I could have said {2,1,3} as well. Let’s draw a full tree on the board.

Complexity What’s the time complexity? partition takes O(s) time, where n = size of subarray (this is bounded by O(n), why?) So how many recursive calls are there and how long does each take? That depends on the shape of the call tree.

A Good Call Tree Suppose the partition always divides the array in half. What does the call tree look like in terms of size? What is the complexity in this case?

A Bad Call Tree Suppose the pivot happens to divide the array into unequal parts – the pivot is always the highest thing in the subarray. What does the call tree look like? What is the complexity? How might we get a “bad” tree?

Preventing Bad Trees a)Choose the pivot in the middle. This is the right choice for sorted data and it’s unlikely that this would be bad for any common data ordering b)Choose the pivot randomly – this defeats any pattern in the incoming data. Middle pivots are usually “good enough”. Note that a bad strategy has to be bad most of the time – a few bad picks don’t mess up the complexity.