Adapted from slides by Marty Stepp and Stuart Reges

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 13
Advertisements

Searching Arrays Linear search Binary search small arrays
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
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.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
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:
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
 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:
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
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 Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Lecture 25: Searching and Sorting
Sort & Search Algorithms
CSc 110, Autumn 2016 Lecture 36: searching.
CSc 110, Spring 2017 Lecture 12: Random Numbers
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
CSc 110, Autumn 2016 Lecture 13: Random Numbers
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
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 14 More Recursive Programming; Grammars
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)
Building Java Programs
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
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
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Implementing ArrayIntList
Sorting Algorithms.
Presentation transcript:

Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Spring 2018 Lecture 38: searching Adapted from slides by Marty Stepp and Stuart Reges

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

binary_search Write the following two functions: # 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 binary_search(list, value) # searches given portion of a sorted list for a given value # examines min_index (inclusive) through max_index (exclusive) # returns the index of the value or -(index it should be inserted at + 1) binary_search(list, value, min_index, max_index)

Using binary_search binary_search returns the index of the number or index1 = binary_search(a, 42) index2 = binary_search(a, 21) index3 = binary_search(a, 17, 0, 16) index2 = binary_search(a, 42, 0, 10) binary_search returns the index of the number or - (index where the value should be inserted + 1)

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, start, stop): min = start max = stop - 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