Compsci 6/101, Spring 2012 12.1 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?

Slides:



Advertisements
Similar presentations
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Advertisements

Analysis of Algorithms CS Data Structures Section 2.6.
Lower bound for sorting, radix sort COMP171 Fall 2005.
SEARCHING, SORTING, TOPOLOGICAL SORTS Most real world computer applications deal with vast amounts of data. Searching for a particular data item can take.
Lower bound for sorting, radix sort COMP171 Fall 2006.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
1 Advanced Data Structures. 2 Topics Data structures (old) stack, list, array, BST (new) Trees, heaps, union-find, hash tables, spatial, string Algorithm.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Sorting Chapter 6. 2 Algorithms to know Insertion Sort Insertion Sort –O(n 2 ) but very efficient on partially sorted lists. Quicksort Quicksort –O(n.
Searching Arrays Linear search Binary search small arrays
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct.
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
CSC 205 Java Programming II Algorithm Efficiency.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2012.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Sorting Ruth Anderson UW CSE 140 Winter Sorting hamlet = "to be or not to be that is the question whether tis nobler in the mind to suffer".split()
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Compsci 6/101, Spring More on Python, Tools, Compsci 101 l APTs, Assignments, Tools  APT: Algorithmic Problem-solving and Testing  How to get.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Image Comparison Tool Product Proposal Tim La Fond and Peter Beckfield.
Compsci 06/101, Spring Compsci 6/101: PFTW, Feb 28-March 4 l Algorithms and Data Structures  Sets and how they are used in Python (data structure)
Compsci 101.2, Fall Plan for FFWOO l Programming in the small and in the large  Making one function work correctly, especially in the context.
CSCI 51 Introduction to Programming March 12, 2009.
CompSci 101 Introduction to Computer Science November 18, 2014 Prof. Rodger.
Sorting.
CS Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Sorting Ruth Anderson UW CSE 160 Spring sorted vs. sort hamlet = "to be or not to be that is the question whether tis nobler in the mind to suffer".split()
5.3 Sorting Techniques. Sorting Techniques Sorting is the process of putting the data in alphabetical or numerical order using a key field primary key.
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.
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.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Compsci 101.2, Fall Plan for eleven-four l Thinking about APTs and test problems  How do you choose: list, string, set, dictionary  Experience?
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.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
Compsci 06/101, Spring Compsci 6/101: Re[gex|cursion] l Regular expressions and grammars  Recognizing and validating text based on rules  Generating.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CompSci 101 Introduction to Computer Science January 15, 2015 Prof. Rodger 1.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Bubble Sort!. What is a bubble sort?!?!?!?!?!?!?!? In a bubble sorting algorithm, the elements of the list "gradually 'bubble' (or rise) to their proper.
CompSci 101 Introduction to Computer Science April 7, 2015 Prof. Rodger.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Why indexing? For efficient searching of a document
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Algorithmic complexity: Speed of algorithms
CompSci 101 Introduction to Computer Science
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson UW CSE 160 Winter 2017
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
What is a Search Engine EIT, Author Gay Robertson, 2017.
Ruth Anderson UW CSE 160 Spring 2018
Algorithmic complexity: Speed of algorithms
Michael Ernst UW CSE 140 Winter 2013
SHENZHEN MIDDLE SCHOOL 2
Algorithmic complexity: Speed of algorithms
Python – May 20 Reading data from the Web
CMPT 120 Lecture 26 – Unit 5 – Internet and Big Data
Presentation transcript:

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? Average?  l Guess a number, I'll tell you high, low, correct  How many guessed needed?  l How do we find the 100 people most like us on …  Amazon, Netflix, Facebook, …  Need a metric for analyzing closeness/nearness,  Need to find people "close", sorting can help

Compsci 6/101, Spring How do we sort? Ask Tim Peters! l Sorting API  Sort lists (or arrays, …)  Backwards, forwards, …  Change comparison First, Last, combo, … l Sorting Algorithms  We'll use what's standard! Best quote: import this I've even been known to get Marmite *near* my mouth -- but never actually in it yet. Vegamite is right out

Compsci 6/101, Spring APTs Sorted and Sortby Frequencies l What are the organizing principles in SortedFreqs?  Alphabetized list of unique words?  Count of number of times each occurs?  Is efficiency an issue? If so what recourse?  To create a list comprehension: # elements in resulting list? Type of elements in list? l What are organizing principles in SortByFreqs?  How do we sort by frequency?  How do we break ties? sorted([(t[1],t[0]) for t in dict.items()]) sorted(dict.items(), key=operator.itemgetter(1))

Compsci 6/101, Spring 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, part of API?  Sorting algorithm is stable, part of API?  sorted(list,reverse=True), part of API 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 6/101, Spring l Women before men …  First sort by height, then sort by gender Stable sorting: respect re-order

Compsci 6/101, Spring Python Sort API by example, (see APT) l Sort by frequency, break ties alphabetically def sort(data): d = {} for w in data: d[w] = d.get(w,0) + 1 ts = sorted([(p[1],p[0]) for p in d.items()]) #print ts return [t[1] for t in ts] How to change to high-to-low: reverse=True How to do two-pass: itemgetter(1) from operator

Compsci 6/101, Spring How to import 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 l Why is import this funny?  This has a special meaning

Compsci 6/101, Spring Organization facilitates search l How does Google find matching web pages?  l How does Soundhound find your song?  l How does tineye find your image  l How do you search in a "real" dictionary  l How do you search a list of sorted stuff?  bisect.bisect_left(list,elt)