Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Wednesday, 11/25/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/25/02  QUESTIONS??  Today:  More on sorting. Advanced sorting algorithms.  Complexity:
Wednesday, 11/13/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/13/02  QUESTIONS??  Today:  More on searching a list; binary search  Sorting a list;
Searching/Sorting Introduction to Computing Science and Programming I.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
Abstract Data Types (ADTs) Data Structures The Java Collections API
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Chapter 19: Searching and Sorting Algorithms
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)
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Sorting.
Computer Science 112 Fundamentals of Programming II Searching, Sorting, and Complexity Analysis.
Searching Topics Sequential Search Binary Search.
 1 Searching. Why searching? Searching is everywhere. Searching is an essential operation for a dictionary and other data structures. Understand the.
Unit 2 Day 11 FOCS – Human Computer Interaction. Tower Building Presentation Explain your solution.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
1 Algorithms Searching and Sorting Algorithm Efficiency.
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.
Intro to Data Structures Concepts ● We've been working with classes and structures to form linked lists – a linked list is an example of something known.
Analysis of Algorithms
CMPT 120 Topic: Searching – Part 2
Week 9 - Monday CS 113.
Introduction to Search Algorithms
Introduction to complexity
Computer Science 112 Fundamentals of Programming II
Introduction to Algorithms
Introduction to Recursion
Introduction to Algorithms
Sorting by Tammy Bailey
Computation.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSc 110, Spring 2017 Lecture 39: searching.
Types, Truth, and Expressions (Part 2)
Binary Search and Intro to Sorting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Analysis of Algorithms
Searching: linear & binary
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ch. 2: Getting Started.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CPS120: Introduction to Computer Science
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CPS120: Introduction to Computer Science
Algorithmic complexity
Binary Search Counting
Analysis of Algorithms
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Intro to Computer Science CS1510 Dr. Sarah Diesburg Intro to Searching Intro to Computer Science CS1510 Dr. Sarah Diesburg

Any Questions Programming assignment PA10?

Algorithm What is an algorithm, again?

Algorithm What is an algorithm, again? Describes the process of how we solve a problem Is code-neutral

Searching Algorithms Many objects already provide searching methods for us Good example of encapsulation myList.index(4) But how do those functions work? Let’s write some code in basic python to search a list without methods

def basicSearch(lyst,what): for item in lyst: if item==what: return True return False

def linearSearch(lyst,what): for index in range(len(lyst)): if lyst[index]==what: return index return -1

Big “O” Notation Can we optimize our search? Big “O” notation is a analysis on how long our algorithm takes as our data set grows Typically asking for two values: On average Worst case scenario

Big “O” Notation Based on the functions we developed Worst case scenario, how many things do we need to look at to find our number in the list? On average, how many things do we need to look at to find our number in the list? What if our list were of size 20?

Big “O” Notation Based on the functions we developed Worst case scenario, how many things do we need to look at to find our number in the list? 20 On average, how many things do we need to look at to find our number in the list? 10

Big “O” Notation Based on the functions we developed Worst case scenario, how many things do we need to look at to find our number in the list? N, where N is the number of items in the list On average, how many things do we need to look at to find our number in the list? N/2

Linear Search Our previous algorithms make one comparison for every item as worst case. Also called a linear search, because the number of comparisons scales as the amount of items in the list increases Can we do better than a linear search Less comparisons, even on worst case?

Optimized Searching Have you ever played the higher or lower game? Think of a number As the player guesses the number, you say “higher” or “lower” until the player finally guesses the correct number Like the clock game on Price Is Right

Optimized Searching One good strategy if you are the guesser is to Guess the middle number of the range If the person says “higher”, then adjust your low range bound to be your guess+1 If the person says “lower”, then adjust your high range bound to be your guess-1 Repeat

Optimized Searching Same idea if you are looking up a vocabulary term in a dictionary You will open the book, look at the current word, and figure out if you should search lower or higher We might as well use this kind of additional information to optimize our searching

Binary Search We can use this type of search on our list Does our list have to be sorted for this to work? Say that we have a list of 20 items What is the worst case number of comparisons? What about a list of 40 items?

Binary Search Every time I double the number of items in my list, my search complexity only goes up by 1 Is much better than linear time as number of items in the list goes up Let’s write a binary search.

def binarySearch(lyst,what): lowIndex = 0 highIndex = len(lyst)-1 while lowIndex<=highIndex : middle = (lowIndex + highIndex)//2 if lyst[middle]==what: return middle if lyst[middle]>what: highIndex=middle-1 if lyst[middle]<what: lowIndex=middle+1 return -1