Compsci 101.2, Fall 2015 21.1 Plan for WBTB l APT Quiz 3 – due tonight l Solving problems in the wild  How can you change how things are sorted Other.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 Recursive Algorithms.
Advertisements

Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Lucene Part3‏. Lucene High Level Infrastructure When you look at building your search solution, you often find that the process is split into two main.
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
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 171: Introduction to Computer Science II Quicksort.
Recitation 9 Programming for Engineers in Python.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
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.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
External Sorting Problem: Sorting data sets too large to fit into main memory. –Assume data are stored on disk drive. To sort, portions of the data must.
CIS 068 Welcome to CIS 068 ! Lesson 9: Sorting. CIS 068 Overview Algorithmic Description and Analysis of Selection Sort Bubble Sort Insertion Sort Merge.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Recursion, Complexity, and Sorting By Andrew Zeng.
Chapter 12 Recursion, Complexity, and Searching and Sorting
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
CompSci 100e 11.1 Sorting: From Theory to Practice l Why do we study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
CompSci 100e Program Design and Analysis II April 26, 2011 Prof. Rodger CompSci 100e, Spring20111.
Compsci 6/101, Spring More on Python, Tools, Compsci 101 l APTs, Assignments, Tools  APT: Algorithmic Problem-solving and Testing  How to get.
Lecture 11 Data Structures, Algorithms & Complexity Introduction Dr Kevin Casey BSc, MSc, PhD GRIFFITH COLLEGE DUBLIN.
Computer Science 101 A Survey of Computer Science Timing Problems.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Announcements: Project 5 Everything we have been learning thus far will enable us to solve interesting problems Project 5 will focus on applying the skills.
Modularity Computer Science 3. What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
Compsci 101.2, Fall Plan for TDAFB (after fall break) l Hear via about plans to catch-up and stay-ahead  Profs. Rodger and Astrachan to.
Compsci 101.2, Fall Plan for FWON l Review current assignments and APTs  Review Dictionaries and how to use them  Code and APT walk-through.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Compsci 101.2, Fall Plan for October 29 l Review dictionaries and their use  Very efficient, easy to use  Efficiency doesn't matter much for.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Compsci 06/101, Fall Programming Equations l Algorithms + Data Structures = Programs  Niklaus Wirth, old view of programming and compsci l How.
Compsci 101.2, Fall Plan for WEFWOO l Review for exam, studying and practicing for exams and APTs in Compsci 101  Why memorization doesn't help.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Compsci 06/101, Spring Compsci 6/101: Re[gex|cursion] l Regular expressions and grammars  Recognizing and validating text based on rules  Generating.
Exam 2 Review CS 3358 Data Structures. 90 Total Points – 50 Points Writing Programs – 25 Points Tracing Algorithms, determining results, and drawing pictures.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
CompSci 6 Introduction to Computer Science September 27, 2011 Prof. Rodger CompSci 6 Fall
Compsci 6/101, Spring Why is sorting data important? l Guess a number in [1,100] --- I'll tell you yes or no  How many guesses needed worst-case?
CSC 241: Introduction to Computer Science I
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems."
Teach A level Computing: Algorithms and Data Structures
Exam 2 Review CS 3358 Data Structures.
Exam 2 Review CS 3358 Data Structures.
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
Welcome to CIS 068 ! Lesson 9: Sorting CIS 068.
Exam 2 Review CS 3358 Data Structures.
IT 4043 Data Structures and Algorithms
slides adapted from Marty Stepp
Hank Childs, University of Oregon
New sorting algorithms happen …
CSCE 206 Lab Structured Programming in C
CSC 241: Introduction to Computer Science I
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Compsci 101.2, Fall Plan for WBTB l APT Quiz 3 – due tonight l Solving problems in the wild  How can you change how things are sorted Other than ordering and re-ordering tuples How do Python.sort and sorted() stack up?  How do you access directories? And all the files in a directory, and the …  How do you access web-based files? How to parse HTML? Other formats?

Compsci 101.2, Fall Playing go-fish, spades, or … l Finding right card?  What helps?  Issues here? l Describe algorithm:  First do this  Then do this  Substeps ok  When are you done?

Compsci 101.2, Fall Problem Solving with Algorithms l Top 100 songs of all time, top 2 artists?  Most songs in top 100  Wrong answers heavily penalized  You did this in lab, you could do this with a spreadsheet l What about top 1,000 songs, top 10 artists?  How is this problem the same?  How is this problem different

Compsci 101.2, Fall Scale l As the size of the problem grows …  The algorithm continues to work  A new algorithm is needed  New engineering for old algorithm l Search  Making Google search results work  Making SoundHound search results work  Making Content ID work on YouTube

Compsci 101.2, Fall Python to the rescue? Top1000.py import csv, operator f = open('top1000.csv','rbU') data = {} for d in csv.reader(f,delimiter=',',quotechar='"'): artist = d[2] song = d[1] if not artist in data: data[artist] = 0 data[artist] += 1 itemlist = data.items() dds = sorted(itemlist,key=operator.itemgetter(1),reverse=True) print dds[:30]

Compsci 101.2, Fall Understanding sorting API How API works for sorted( ) or.sort()  Alternative to changing order in tuples and then changing back x = sorted([(t[1],t[0]) for t in dict.items()]) x = [(t[1],t[0]) for t in x] x = sorted(dict.items(),key=operator.itemgetter(1)) l Sorted argument is key to be sorted on, specify which element of tuple. Must import library operator for this

Compsci 101.2, Fall Sorting from an API/Client perspective l API is Application Programming Interface, what is this for sorted(..) and.sort() in Python?  Sorting algorithm is efficient, stable: part of API?  sorted returns a list, doesn't change argument  sorted(list,reverse=True), part of API  foo.sort( ) modifies foo, same algorithm, API l How can you change how sorting works?  Change order in tuples being sorted, [(t[1],t[0]) for t in …]  Alternatively: key=operator.itemgetter(1)

Compsci 101.2, Fall Beyond the API, how do you sort? l Beyond the API, how do you sort in practice?  Leveraging the stable part of API specification?  If you want to sort by number first, largest first, breaking ties alphabetically, how can you do that? l Idiom:  Sort by two criteria: use a two-pass sort, first is secondary criteria (e.g., break ties) [("ant",5),("bat", 4),("cat",5),("dog",4)] [("ant",5),("cat", 5),("bat",4),("dog",4)]

Compsci 101.2, Fall Two-pass (or more) sorting l Because sort is stable sort first on tie- breaker, then that order is fixed since stable a0 = sorted(data,key=operator.itemgetter(0)) a1 = sorted(a0,key=operator.itemgetter(2)) a2 = sorted(a1,key=operator.itemgetter(1)) data [('f', 2, 0), ('c', 2, 5), ('b', 3, 0), ('e', 1, 4), ('a', 2, 0), ('d', 2, 4)] a0 [('a', 2, 0), ('b', 3, 0), ('c', 2, 5), ('d', 2, 4), ('e', 1, 4), ('f', 2, 0)]

Compsci 101.2, Fall Two-pass (or more) sorting a0 = sorted(data,key=operator.itemgetter(0)) a1 = sorted(a0,key=operator.itemgetter(2)) a2 = sorted(a1,key=operator.itemgetter(1)) a0 [('a', 2, 0), ('b', 3, 0), ('c', 2, 5), ('d', 2, 4), ('e', 1, 4), ('f', 2, 0)] a1 [('a', 2, 0), ('b', 3, 0), ('f', 2, 0), ('d', 2, 4), ('e', 1, 4), ('c', 2, 5)] a2 [('e', 1, 4), ('a', 2, 0), ('f', 2, 0), ('d', 2, 4), ('c', 2, 5), ('b', 3, 0)]

Compsci 101.2, Fall Answer Questions

Compsci 101.2, Fall Timingsorts.py, what sort to call? l Simple to understand, hard to do fast and at-scale  Scaling is what makes computer science … Efficient algorithms don't matter on lists of 100 or 1000  Named algorithms in 201 and other courses bubble sort, selection sort, merge, quick, … See next slide and TimingSorts.py l Basics of algorithm analysis: theory and practice  We can look at empirical results, would also like to be able to look at code and analyze mathemetically! How does algorithm scale?

Compsci 101.2, Fall New sorting algorithms happen … l timsort is standard on…  Python as of version 2.3, Android, Java 7  According to Adaptive, stable, natural mergesort with supernatural performance l What is mergesort? Fast and Stable  What does this mean?  Which is most important?  Nothing is faster, what does that mean?  Quicksort is faster, what does that mean?

Compsci 101.2, Fall TimingSorts.py sizecreatebubbleselecttimsort

Compsci 101.2, Fall Stable, Stability l What does the search query 'stable sort' show us?  Image search explained  First shape, then color: for equal colors?

Compsci 101.2, Fall l Women before men …  First sort by height, then sort by gender Stable sorting: respect re-order

Compsci 101.2, Fall How to import: in general and sorting l We can write: import operator  Then use key=operator.itemgetter(…) l We can write: from operator import itemgetter  Then use key=itemgetter(…) l From math import pow, From cannon import pow  Oops, better not to do that, use dot-qualified names like math.sqrt and operator.itemgetter

Compsci 101.2, Fall TimingSorts.py Questions