CMSC201 Computer Science I for Majors Lecture 22 – Searching

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

HST 952 Computing for Biomedical Scientists Lecture 9.
Analysis And Algorithms CMSC 201. Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4,
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
222 Sqn Comms Training Phonetic Alphabet Self Test Select Button to Start.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Introduction. 2COMPSCI Computer Science Fundamentals.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Air Cadet Radio Training Welcome to this self teach package. It has been produced for use within the Air Cadet Organisation and must not be published elsewhere.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
CSCI 51 Introduction to Programming March 12, 2009.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking Prof. Katherine Gibson Based on slides by Shawn Lupoli and Max Morawski at UMBC.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
Now with a speaking professor! (hopefully...)
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Last Class We Covered Sorting algorithms Searching algorithms
Last Class We Covered Sorting algorithms “Run” time Selection Sort
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
Air Cadet Radio Training
Air Cadet Radio Training
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
CS1371 Introduction to Computing for Engineers
CompSci 101 Introduction to Computer Science
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
CMSC201 Computer Science I for Majors Lecture 27 – Final Exam Review
Prof. Neary Adapted from slides by Dr. Katherine Gibson
And now for something completely different . . .
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Data representation Binary numbers ASCII values
CISC101 Reminders Slides have changed from those posted last night…
CS190/295 Programming in Python for Life Sciences: Lecture 6
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Coding Concepts (Basics)
Searching: linear & binary
Lesson 09: Lists Class Chat: Attendance: Participation
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review
Last Class We Covered Asymptotic Analysis Run “time” Big O
Algorithmic complexity: Speed of algorithms
Prof. Katherine Gibson Prof. Jeremy Dixon
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Final Exam Information
And now for something completely different . . .
CHAPTER 4: Lists, Tuples and Dictionaries
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Last Class We Covered Recursion Stacks Parts of a recursive function:
“Everything Else”.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE 326: Data Structures Lecture #14
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 22 – Searching

Welcome Back!

Review: Tuples & Dictionaries Create an empty tuple Create a dictionary that contains three different (key, value) pairs, similar to “a is for apple” Add one additional (key, value) pair Update one of your (key, value) pairs Remove one of your (key, value) pairs Why must dictionary keys be unique? Do values need to be unique?

Review: Matching Symbols Match the following data types to the symbols needed to create them (may be more than one) { } Dictionary ( ) List " " String [ ] Tuple ' '

Review: Matching Symbols Match the following data types to the symbols needed to create them (may be more than one) { } Dictionary ( ) List " " String [ ] Tuple ' '

Review: Mutability Which of the following are mutable data types? Boolean ??? Dictionary ??? Float ??? Integer ??? List ??? String ??? Tuple ???

Review: Mutability Which of the following are mutable data types? Boolean ??? Immutable Dictionary ??? Mutable Float Immutable ??? Integer Immutable ??? List Mutable ??? String ??? Immutable Tuple Immutable ???

Review: Implementation You are given a dictionary of the NATO phonetic alphabet, in the form: ALPHA = {"A" : "Alpha", "B" : "Bravo", "C" : "Charlie", ... etc.} Write a function to convert a string from the user into its phonetic code words You only need to handle letters (case insensitive)

Review: Implementation Example Here is an example of how it should work: Please enter a word: EXAMPLE The word "EXAMPLE" becomes "Echo X-ray Alpha Mike Papa Lima Echo" Please enter a word: dogmeat The word "dogmeat" becomes "Delta Oscar Golf Mike Echo Alpha Tango"

Any questions about the material we just reviewed?

Today’s Objectives To learn more about searching algorithms Linear search Binary search

Search

Motivations for Searching Want to know if something exists Python can do this for us! Want to know where something exists Python can actually do this for us too! raceWinners.index(718) But how does Python does this?

Exercise: find() Write a function that takes a list and a variable and returns the index of the variable in the list If it’s not found, return -1 You can’t use index()! def find(searchList, var)

Exercise: find() Solution def find(searchList, var): for i in range(len(searchList)): if searchList[i] == var: return i # outside the loop, means that # we didn't find the variable return -1

Linear Search You just programmed up a search function! This algorithm is called linear search It’s a common, fundamental algorithm in CS It’s especially useful when our information isn’t in a sorted order But it isn’t very fast

Searching Sorted Information Now, imagine we’re looking for information in something sorted, like a phone book We know someone’s name (it’s our “variable”), and want to find their number in the book What is a good method for locating their phone number? Think about how you would do this.

Algorithm in English Open the book midway through. If the person’s name is on the page you opened to You’re done! If the person’s name is after the page you opened to Tear the book in half, throw the first half away and repeat this process on the second half If the person’s name is before the page you opened to Tear the book in half, throw the second half away and repeat this process on the first half This is rough on the phone book, but you’ll find the name!

Binary Search

Binary Search The algorithm we just demonstrated is better known as binary search We talked about it briefly last class, remember? Binary search is only usable on sorted lists Why?

Solving Binary Search Binary search is a problem that can be broken down into Something simple (breaking a list in half) A smaller version of the original problem (searching that half of the list) That means we can use ... recursion!

Exercise: Recursive Binary Search Write a recursive binary search! To make the problem slightly easier, make it “checking to see if something is in a sorted list” If there’s no “middle” of the list, we’ll just look at the lower of the two “middle” indexes

Exercise: Recursive Binary Search Write a recursive binary search! Remember to ask yourself: What is our base case(s)? What is the recursive step? def binarySearch(myList, item): A hint: in order to get the number at the middle of the list, use this line: myList[len(myList) // 2]

Time for… LIVECODING!!!

Announcements Final is Thursday, December 15th (3:30 – 5:30) Project 2 will come out soon The third survey will be announced on Blackboard shortly (0.5% of your grade) Not on Blackboard TA Feedback; anonymous to the TAs