CS 280 Data Structures Professor John Peterson. Project Questions?

Slides:



Advertisements
Similar presentations
Chapter 9 continued: Quicksort
Advertisements

Introduction to Algorithms Quicksort
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CPS120: Introduction to Computer Science Searching and Sorting.
CSCE 3110 Data Structures & Algorithm Analysis
Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT Gerard Harrison Divide and Conquer Reduce the problem by reducing the data set. The.
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
QuickSort The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Sorting Algorithms and Average Case Time Complexity
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
CMPS1371 Introduction to Computing for Engineers SORTING.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
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.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
Sorting21 Recursive sorting algorithms Oh no, not again!
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.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
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.
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.
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. Project Questions? /CIS280/f07/project5http://wiki.western.edu/mcis/index.php.
Analysis of Algorithms CS 477/677
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.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
(c) , University of Washington
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Computer Science Searching & Sorting.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting part 2 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
Review 1 Insertion Sort Insertion Sort Algorithm Time Complexity Best case Average case Worst case Examples.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2010.
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.
Quicksort Dr. Yingwu Zhu. 2 Quicksort A more efficient exchange sorting scheme than bubble sort – A typical exchange involves elements that are far apart.
Sorting part 2 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
CPS120: Introduction to Computer Science Sorting.
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 
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Sorting.
Warmup What is an abstract class?
Section 10.3a Merge Sort.
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Algorithm Design Methods
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
CO 303 Algorithm Analysis And Design Quicksort
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
CS 1114: Sorting and selection (part two)
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Presentation transcript:

CS 280 Data Structures Professor John Peterson

Project Questions?

Test #1 We’ll do a test next week on Friday. 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 everything!

Yet Another Sort of Sort What’s interesting about the sorts we’ve seen so far? Insertion sort? Selection sort? Bubble sort? Heapsort? Why would you choose any of these in a real program?

Algorithmic Paradigms One of the key ideas in this class is to understand basic algorithm designs. There aren’t that many! With the O(n^2) sorts, the basic idea is similar to mathematical induction: Base case + step from size n to n+1 Each of these takes n steps and the work needed to take a step is O(n).

Divide and Conquer One of the big ideas is to decompose a problem “more evenly” – that is, instead of taking a problem of size n and solving the n-1 case then adding one more number, cut the problem into equal sized chunks. This is called “divide and conquer” and this the basis for many fast algorithms. Questions: How to split the problem up How to glue solutions together Recursion is used to solve the smaller sub- problems.

Quicksort To divide: pick a piece of data and divide the array into two pieces: one less than the selected element, the other greater than or equal to it. To unite divided array chunks: NOTHING! That is, everything ends up in the right place after the recursions. That’s EASY!

Example Start with the following array: {5, 3, 7, 2, 4, 8, 1, 9, 6} Using the first element to separate the halves, create the following: {3, 2, 1, 4, 5, 7, 8, 9, 6} The ordering of the low and high part might change. The 5 is called the pivot. Note that the 5 is “locked” in place.

Representations How do we represent the cutting of the array (the Sortable) in half without creating new objects?

Representations We’ll use a triple: the original sortable and upper and lower bounds (integers). Thus we never need to deal with sub- arrays directly. We need to “prime the pump” as follows: quicksort(s) = qs(s, 0, s.size()-1) The “qs” method is the real sort algorithm – this just initializes the range.

Base Case Since we’re using recursion, we need to worry about a “base case”, one in which there’s nothing to do. When is qs(s, low, high) trivial to compute? high <= low

Partitioning The goal of partitioning is to separate the range into two chunks, one below and one above the pivot. Let’s postulate another method: partition. This will take a range and return the location of the pivot in the partitioned array. int partition(Sortable s, int low, int high)

Quicksort void qs(Sortable s, int low, int high) { if (high – low > 0) { int p = partition(s, low, high); qs(s, low, p-1); // sort low side qs(s, p+1, high); // sort high side }}

Partitioning There are lots of ways to do partitioning – we’ll choose one of the more efficient ones. We’ll arbitrarily select the last element in the range to be the pivot. How could we choose something different? We’ll use two pointers (array indices) to indicate the lower and upper bound of the unpartitioned area. Initially, lo = low and hi = high – 1

Example { 3, 8, 4, 2, 5, 7, 6 } lo hi Strategy: slide lo up and hi down until something out of place appears. If they haven’t crossed, swap the elements at lo and hi, push them one closer to the middle, and repeat.

Example { 3, 8, 4, 2, 5, 7, 6 } lo hi After the slide, lo points to something that is too big and hi points to something that is too low (compared to pivot)

Example { 3, 5, 4, 2, 8, 7, 6 } lo hi After the swap

Example { 3, 5, 4, 2, 8, 7, 6 } lo hi Then push them one closer …

Example { 3, 5, 4, 2, 8, 7, 6 } hi lo And slide again … This time they have crossed so we’re done (almost). To finish, swap lo and the pivot, then return “lo”. { 3, 5, 4, 2, 6, 7, 8 }

As Code … int partition(Sortable s, int low, int high) { int lo = low; int hi = high – 1; while (lo <= hi) { while (lo < high && s.gtr(high, lo)) lo++; while (hi >= low && !s.gtr(high, hi)) hi--; if (lo <= hi) { // in case they crossed … s.swap(lo, hi); lo++;hi--;}} s.swap(lo, high); // put pivot in middle return lo; }

Quicksort void qs(Sortable s, int low, int high) { if (high – low > 0) { int p = partition(s, low, high); qs(s, low, p-1); // sort low side qs(s, p+1, high); // sort high side }}