CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Back to Sorting – More efficient sorting algorithms.
The Efficiency of Algorithms
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
HST 952 Computing for Biomedical Scientists Lecture 9.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching/Sorting Introduction to Computing Science and Programming I.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Searching and Sorting Gary Wong.
CS223 Advanced Data Structures and Algorithms 1 Sorting and Master Method Neil Tang 01/21/2009.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Bucket & Radix Sorts. Efficient Sorts QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence?
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Sorting.
Programming Abstractions Cynthia Lee CS106X. Today’s Topics Sorting! 1.The warm-ups  Selection sort  Insertion sort 2.Let’s use a data structure! 
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Algorithms Searching and Sorting Algorithm Efficiency.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Algorithms.
Searching and Sorting Algorithms
Programming Abstractions
COP 3503 FALL 2012 Shayan Javed Lecture 15
Merging Merge. Keep track of smallest element in each sorted half.
Sorting by Tammy Bailey
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
CS 3343: Analysis of Algorithms
Searching.
CSC212 Data Structure - Section RS
Unit-2 Divide and Conquer
Searching CLRS, Sections 9.1 – 9.3.
CS200: Algorithms Analysis
A G L O R H I M S T A Merging Merge.
CSE 12 – Basic Data Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
David Kauchak cs161 Summer 2009
A G L O R H I M S T A Merging Merge.
A G L O R H I M S T A Merging Merge.
Algorithm Efficiency and Sorting
Presentation transcript:

CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics: Recursion! 1. Question from Wednesday: What is the Traveling Salesman Problem? 2. Recursive sorting  Merge sort  Quick sort 2

Traveling Salesperson Problem

Map vs Matrix, Eye vs Algorithm  The human eye/brain is pretty good at taking in a map picture and, considering many points in “parallel,” perceive routes that take advantage of nearby points  Much harder for a computer to do when it has no idea where a point is without examining it individually

Map vs Matrix, Eye vs Algorithm  The human eye/brain is pretty good at taking in a map picture and, considering many points in “parallel,” perceive routes that take advantage of nearby points  Much harder for a computer to do when it has no idea where a point is without examining it individually  Like how we can see “that one should go over there!!” in sorting:  parisonSort.html parisonSort.html

Speaking of sorting… Sorting

Write code (or pseudocode) for Selection Sort

Aside: Worst case vs. Best case  Remember Binary Search  Look at middle element: if too big, rule out right half and recursively search left; if too small, rule out left half and recursively search right; if correct element, WIN! If 0 elements left, LOSE!

Aside: Worst case vs. Best case  Remember Binary Search  Best case: O(1) Search for 29—there it is! #winning  Worst case: (a) O(1), (b) O(logn), (c) O(nlogn), (d) O(n), (e) Other/none/more

Analysis of Selection Sort  What is the worst-case BigO running time for Selection Sort?  Is the best case different?  What makes a best/worst case for Selection Sort?

Sorting The Professor’s Sorting Algorithm

Professor’s sorting algorithm: 1. Find two grad students, give each half of the unsorted midterms 2. Tell the grad students to sort their own pile, then give it back 3. Combine the two piles into one sorted pile, using our simple combine algorithm 4. Done!

Grad student’s sorting algorithm: 1. Find two SLs, give each half of the unsorted midterms 2. Tell the SLs to sort their own pile, then give it back to you 3. Combine the two piles into one sorted pile, using our simple combine algorithm 4. Done! (give your sorted pile to professor)

SL’s sorting algorithm: 1. Find two class members, give each half of the unsorted midterms 2. Tell the floormates to sort their own pile, then give it back to you 3. Combine the two piles into one sorted pile, using our simple combine algorithm 4. Done! (SL gives sorted pile to grad student)

Class member’s sorting algorithm: 1. Pile only has one exam in it 2. Done! (class member gives sorted pile to SL)

Class member’s sorting algorithm: 1. Pile only has one exam in it 2. Done! (class member gives sorted pile to SL) Question: Why did we stop at one exam? Isn’t the first trivial case we encounter the case of 2 exams?

Recursive Sorting MergeSort

Combine two sorted piles algorithm  Start: you have two piles, each of which is sorted  Take the smallest element of the two piles and add it to the combined-sorted pile  Repeat until the two starting piles are empty and the combined-sorted pile is complete  If one pile is empty and other is not, move from non-empty pile into sorted pile

I would like to move one paper from the two sorted sub-piles to the combined sorted pile. How many papers do I have to examine, at most, to identify the current smallest element of both sorted sub-piles? A. 2 elements B. 4 elements C. Size of the first pile D. Size of the second pile E. Other/none/more than one

How many steps does it take to fully merge two sorted sub-piles, A and B? (best/tight answer) A. O(log(A+B)) steps B. O(A+B) steps C. O(A+B) 2 steps D. O(A 2 + B 2 )steps E. Other/none/more than one

Consider an arbitrarily chosen (generic) particular exam and mentally track its progress throughout the algorithm. How many times does your exam pass through the merge step? A. 1 time B. 2 times C. Θ(logn) times D. Θ(n) times E. Other/none/more than one (Recall Θ means the same as O but where the time is a best match, not a distant upper bound.)

BigO Analysis of Mergesort  Every paper is merged log(n) times  This is the number of times we can divide the stack of n papers by 2 before we can’t divide anymore  There are n papers  O(nlogn)