And now for something completely different . . .

Slides:



Advertisements
Similar presentations
Chapter 9: Searching, Sorting, and Algorithm Analysis
Advertisements

8 Algorithms Foundations of Computer Science ã Cengage Learning.
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,
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Recursion Introduction to Computing Science and Programming I.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
Searching Arrays Linear search Binary search small arrays
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Problem Solving and Algorithms
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
CSCI 130 Array Searching. Methods of searching arrays Linear –sequential - one at the time –set does not have to be ordered Binary –continuously cutting.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
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.
Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Using recursion for Searching and Sorting
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Searching and Sorting Algorithms
CMPT 120 Topic: Searching – Part 2
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Introduction to Computing Science and Programming I
Searching Given a collection and an element (key) to find… Output
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Sorting by Tammy Bailey
Divide-and-Conquer The most-well known algorithm design strategy:
COSC160: Data Structures Linked Lists
Divide-and-Conquer The most-well known algorithm design strategy:
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
CSc 110, Spring 2017 Lecture 39: searching.
And now for something completely different . . .
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Data Structures Review Session
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching: linear & binary
Linear Search Binary Search Tree
Divide-and-Conquer The most-well known algorithm design strategy:
Recurrences (Method 4) Alexandra Stefan.
Data Structures Sorted Arrays
And now for something completely different . . .
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Presentation transcript:

And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 1

Python Searching Fri 5th Jan 2018 AD c 2018 2

Searching 10.1 Introduction to Searching 10.2 The Sequential Search 10.3 The Binary Search Fri 5th Jan 2018 AD c 2018 3

Searching 10.1 Introduction to Searching 10.2 The Sequential Search 10.3 The Binary Search Fri 5th Jan 2018 AD c 2018 4

Introduction to Searching The topics of searching and sorting occupy prominent positions in computer science. Fri 5th Jan 2018 AD c 2018 5

It has been estimated that anywhere from 25% to 50% of all computing time in the commercial world is spent on searching and sorting data. Fri 5th Jan 2018 AD c 2018 6

Searching 10.1 Introduction to Searching 10.2 The Sequential Search 10.3 The Binary Search Fri 5th Jan 2018 AD c 2018 7

Sequential Search One of the most straightforward and elementary searches is the sequential search, also known as the linear search. Fri 5th Jan 2018 AD c 2018 8

Sequential Search You start at the beginning of a sequence and go through each item one by one, in the order they exist in the list, until you find the item you are looking for. . . Fri 5th Jan 2018 AD c 2018 9

A dictionary is a book that lists (in alphabetical order) the words of a language and against each word a description of its meaning. . . Fri 5th Jan 2018 AD c 2018 10

bliss n. perfect happiness Fri 5th Jan 2018 AD c 2018 11

You're guaranteed to find any word in a dictionary, if you start at the beginning and check every word one after the other. . . Fri 5th Jan 2018 AD c 2018 12

Linear search requires you to look at, on average, half of the elements in the list. Fri 5th Jan 2018 AD c 2018 13

In the worst case scenario, you may have to search the entire list to find the item of interest. Fri 5th Jan 2018 AD c 2018 14

Advantages of Sequential Search? Easy to implement Data does not need to be sorted Fri 5th Jan 2018 AD c 2018 15

Sequential (Linear) Search Animation Click here to see the animation: http://cs.armstrong.edu/liang/animation/web/LinearSearchNew.html Fri 5th Jan 2018 AD c 2018 16

A Sequential Search program list1 = [11,27,36,44,51,22,65,1,78] numbertofind = int(input("Enter a number\n")) found = 0 for i in list1: if numbertofind == i: print (numbertofind, " at index: ",list1.index(numbertofind)) found = 1 if found == 0: print ("Number not found") 10-01.py Fri 5th Jan 2018 AD c 2018 17

Another Sequential Search program mylist = [10,11,3,4,55,12,23,14,16] n = len(mylist) print (n) for i in range(n): print (mylist[i], end=" ") search = int(input("\nPlease enter a number to search for: ")) print (search) found = False if mylist[i] == search: found = True index = i print() if found == True: print (str(search) + " found at index " + str(index)) else: print (str(search) + " not found") 10-02.py Fri 5th Jan 2018 AD c 2018 18

Sequential (Linear) Search Video Click here to see the video: https://youtu.be/eQN-EB8Wsls Fri 5th Jan 2018 AD c 2018 19

Searching 10.1 Introduction to Searching 10.2 The Sequential Search 10.3 The Binary Search Fri 5th Jan 2018 AD c 2018 20

Binary Search Binary search is a more specialized algorithm than sequential search as it takes advantage of the fact that the data has been sorted. . . Fri 5th Jan 2018 AD c 2018 21

Binary Search The underlying idea of binary search is to divide the sorted data into two halves and to examine the data at the point of the split. Fri 5th Jan 2018 AD c 2018 22

2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 Fri 5th Jan 2018 AD c 2018 23

Binary Search Since the data is sorted, we can easily ignore one half or the other depending on where the value we're looking for lies in comparison to the value at the split. This makes for a much more efficient search than linear search. Fri 5th Jan 2018 AD c 2018 24

Binary Search Algorithms A binary search algorithm involves breaking down the problem into a smaller version of itself, and hence is an ideal candidate for a technique known as recursion. . . Fri 5th Jan 2018 AD c 2018 25

Recursion Recursion is a method for solving a problem in which the problem is broken down into a smaller version of itself. Fri 5th Jan 2018 AD c 2018 26

Recursion A recursive solution to a programming problem uses a function which calls itself. We study recursion in detail in a separate unit. Fri 5th Jan 2018 AD c 2018 27

Binary Search Binary search involves binary decisions, decisions with two choices. At each step in the process you can eliminate half of the data you're searching. . . Fri 5th Jan 2018 AD c 2018 28

Binary Search If you have an list of 16 numbers (N = 16), you can find any one number in at most, 4 steps: 16 -> 8 -> 4 -> 2 -> 1 Log216 = 4 Fri 5th Jan 2018 AD c 2018 29

Searching for number 7 is 7 <= 33? 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 33? Fri 5th Jan 2018 AD c 2018 30

Searching for number 7 is 7 <= 10? 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 10? Fri 5th Jan 2018 AD c 2018 31

Searching for number 7 is 7 <= 7? 7 found! 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 7? 7 found! Fri 5th Jan 2018 AD c 2018 32

Fri 5th Jan 2018 AD c 2018 33

Binary Search In general, binary search operates on one of two data structures: lists and trees.In this course, we focus on lists. Fri 5th Jan 2018 AD c 2018 34

Binary Search Animation Click here to see the animation: http://cs.armstrong.edu/liang/animation/web/BinarySearchNew.html Fri 5th Jan 2018 AD c 2018 35

Binary Search Video Click here to see the video: https://youtu.be/_G7ivNb6BPk Fri 5th Jan 2018 AD c 2018 36

Binary Search Python Code Click here to see the binary search programs 10-04.p and 10-05.py: http://www.annedawson.net/python3programs.html Fri 5th Jan 2018 AD c 2018 37

This presentation uses the following program files: 10-01.py to 10-05.py Click here to see Python code: http://www.annedawson.net/python3programs.html Fri 5th Jan 2018 AD c 2018 38

Last updated: Friday 5th January 2018, 7:00 PT, AD Fri 5th Jan 2018 AD c 2018 39