CSc 110, Autumn 2016 Lecture 36: searching.

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 13
Advertisements

Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.
Binary Search Visualization i j.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
CSE 143 Lecture 5 Binary search; complexity reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 13 Searching reading: 13.3.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
1 Searching and Sorting Linear Search Binary Search.
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.
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
CSE 143 Lecture 6 Inheritance; binary search reading: 9.1, ; 13.1 slides created by Marty Stepp
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Binary search and complexity reading:
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Building Java Programs Chapter 13 Searching and Sorting Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Searching and Sorting Searching algorithms with simple arrays
Lecture 25: Searching and Sorting
Sort & Search Algorithms
Adapted from slides by Marty Stepp and Stuart Reges
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
CSc 110, Autumn 2017 Lecture 37: searching and sorting
Data Structures and Algorithms
Sorting Data are arranged according to their values.
Adapted from slides by Marty Stepp and Stuart Reges
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 14: binary search and complexity reading:
CSE 143 Lecture 5 Binary search; complexity reading:
CSc 110, Spring 2017 Lecture 39: searching.
Building Java Programs
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
Chapter 8 Search and Sort
slides adapted from Marty Stepp
CSc 110, Spring 2017 Lecture 39: searching and sorting
Sorting Data are arranged according to their values.
Lecture 15: binary search reading:
Adapted from slides by Marty Stepp and Stuart Reges
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
CSE 373 Data Structures and Algorithms
Linear Search Binary Search Tree
CSE 143 Lecture 16 (A) Searching and Comparable
Building Java Programs
slides created by Marty Stepp
Building Java Programs
Building Java Programs Chapter 13
Adapted from slides by Marty Stepp and Stuart Reges
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
CPS120: Introduction to Computer Science
Building Java Programs
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
CPS120: Introduction to Computer Science
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Lecture 4: Introduction to Code Analysis
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Implementing ArrayIntList
Sorting Algorithms.
Presentation transcript:

CSc 110, Autumn 2016 Lecture 36: searching

Sequential search sequential search: Locates a target value in a list by examining each element from start to finish. Used in index. How many elements will it need to examine? Example: Searching the list below for the value 42: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 i

Sequential search How many elements will be checked? def index(value): for i in range(0, size): if (my_list[i] == value): return i return -1 # not found On average how many elements will be checked? index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103

Binary search binary search: Locates a target value in a sorted list by successively eliminating half of the list from consideration. How many elements will it need to examine? Example: Searching the list below for the value 42: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 min mid max

Binary search runtime For an list of size N, it eliminates ½ until 1 element remains. N, N/2, N/4, N/8, ..., 4, 2, 1 How many divisions does it take? Think of it from the other direction: How many times do I have to multiply by 2 to reach N? 1, 2, 4, 8, ..., N/4, N/2, N Call this number of multiplications "x". 2x = N x = log2 N Binary search looks at a logarithmic number of elements

bisect bisect(list, value) bisect(list, value, min_index, max_index) from bisect import * # searches an entire sorted list for a given value # returns the index the value should be inserted at to maintain sorted order # Precondition: list is sorted bisect(list, value) # searches given portion of a sorted list for a given value # examines min_index (inclusive) through max_index (exclusive) bisect(list, value, min_index, max_index)

Using bisect # index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 a = {-4, 2, 7, 9, 15, 19, 25, 28, 30, 36, 42, 50, 56, 68, 85, 92} index1 = bisect(a, 42, 0, 16) # index1 is 11 index2 = bisect(a, 21, 0, 16) # index2 is 6 bisect returns the index where the value could be inserted while maintaining sorted order if the value is already in the list the next index is returned

Binary search code # Returns the index of an occurrence of target in a, # or a negative number if the target is not found. # Precondition: elements of a are in sorted order def binary_search(a, target): min = 0 max = len(a) - 1 while (min <= max): mid = (min + max) // 2 if (a[mid] < target): min = mid + 1 elif (a[mid] > target): max = mid - 1 else: return mid # target found return -(min + 1) # target not found