Linear and Binary Search

Slides:



Advertisements
Similar presentations
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Advertisements

CSE Lecture 3 – Algorithms I
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
1 CS100J 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
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.
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)
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
CSC 211 Data Structures Lecture 13
1 CS100J 31 October 2006 Arrays: searching & sorting. Reading: 8.5 Merry Christmas!! On Halloween? Searching and sorting algorithms are on the course website.
1 Algorithms CS 202 Epp section ??? Aaron Bloomfield.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching Arrays Linear search Binary search small arrays
Recursion.
Growth of Functions & Algorithms
Correctness issues and Loop invariants
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Recitation 13 Searching and Sorting.
Search Lesson Outline Searching Lesson Outline
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
Programming for Art: Algorithms
Searching CSCE 121 J. Michael Moore.
Searching.
Announcements P2 is due tomorrow Prelim on Monday
Chapter 8 Search and Sort
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Standard Version of Starting Out with C++, 4th Edition
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Searching CLRS, Sections 9.1 – 9.3.
24 Searching and Sorting.
Cs212: DataStructures Lecture 3: Searching.
Sorting "There's nothing 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 Hat, Harry Potter.
Data Structures Sorted Arrays
Asymptotic complexity Searching/sorting
Review of Searching and Sorting Algorithms
Binary Search and Loop invariants
Asymptotic complexity
Module 8 – Searching & Sorting Algorithms
CS November 2010 Developing array algorithms. Reading:
Searching and Sorting Hint at Asymptotic Complexity
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Searching and Sorting Hint at Asymptotic Complexity
Computer Science 3 03A-Searching
CS100J Nov 04, 2003 Arrays. Reading: 8
Module 8 – Searching & Sorting Algorithms
Algorithms.
CS100A Sections Dec Loop Invariant Review C Review and Example
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Searching.
Algorithms and data structures: basic definitions
Recursive binary search
Presentation transcript:

Linear and Binary Search CS100A Lecture 14, 20 October 1998 Linear and Binary Search Base-2 logarithms Linear Search: Find (the value of) x is array b. This description is too vague, for several reasons: How are we to indicate that x appears in b? What if it appears more than once in b? What is to be done if x is not in b? In general, when approaching a new programming job, the first task is to make the specification of the job as precise as possible. CS100A, Lecture 14, 20 October 1998

One way to specify the task: Write a method that returns the index of the first element of b that equals x. If x does not occur in b, return b.length. Example: b = {1, 2, 8, 5, 2, 9} (so b.length = 6) If x is 1, return 0 If x is 2, return 1 If x is 5, return 3 if x is 3, return 6 // return value i that satisfies: 0<=i<=b.length, // x not in b[0..i-1], // (i = b.length or x=b[i]) public static int linearSearch (int [ ] b, int x) Need a loop. It will compare elements of b with x, beginning with b[0], b[1], … Invariant: 0<=i<=b.length and x not in b[0..i-1] x is not here 0 i b.length b CS100A, Lecture 14, 20 October 1998

i= 0; // Inv: 0 i b.length while ( ) { b x is not here } CS100A, Lecture 14, 20 October 1998

// return the value i that satisfies: 0<=i<=b.length, // x not in b[0..i-1], and (i = b.length or x=b[i]) public static int linearSearch (int [ ] b, int x) { int i= 0; // Inv: 0<=i<=b.length and x not in b[0..i-1], while (i < b.length && x != b[i]) i= i+1; return i; } Alternative method body // Inv: 0<=i<=b.length and x not in b[0..i-1], while (i < b.length) { if (x == b[i]) return i; Worst case, makes up to b.length array comparisons. CS100A, Lecture 14, 20 October 1998

Find x in sorted (in ascending order) array b. Binary search Find x in sorted (in ascending order) array b. Like a search in a telephone directory Specification. Store an integer in k to truthify: b[0..k] <= x < b[k+1..b.length-1] means that every value means that every value in here is <= x here is > x Examples: b x 1 2 3 4 5 6 7 8 9 10 k -1 0 3 3 4 5 5 7 8 8 <= x >x 0 k b.length b 2 3 3 3 5 6 8 8 9 0 1 2 3 4 5 6 7 8 CS100A, Lecture 14, 20 October 1998

Store an integer in k to truthify Binary search Store an integer in k to truthify b[0..k] <= x < b[k+1..b.length-1] Invariant k= ; j= ; while ( ) { int e = ; // We know that -1 <= k < e < j <= b.length if ( b[e] <= x ) else } <= x >x 0 k b.length b 0 k j b.length <= x >x b CS100A, Lecture 14, 20 October 1998

// Given sorted b, return the integer k that satisfies Binary search // Given sorted b, return the integer k that satisfies // b[0..k] <= x < b[k+1..b.length-1] public static int binarySearch( int [ ] b) { int k= -1; int j= b.length; // Invariant: -1 <= k < j <= b.length and // b[0..k] <= x < b[k+1..b.length] while (k+1 != j) { int e = (k+j)/2; // We know -1 <= k < e < j <= b.length if ( b[e] <= x ) k= e; else j= e; } return k; CS100A, Lecture 14, 20 October 1998

Points about this binary search of a sorted array It works when the array is empty (b.length = 0) --return -1. If x is in b, finds its rightmost occurrence If x is not in b, finds position where it belongs In general, it’s faster than a binary search that stops as soon as x is found. The latter kind of binary search needs an extra test (x = b[k]) in the loop body, which makes each iteration slower; however, stopping as soon as x is found can be shown to save only one iteration (on the average). It’s easy to verify correctness of the algorithm --to see that it works. You should memorize the specification and the development of the algorithm. CS100A, Lecture 14, 20 October 1998

How fast is binary search? b.length no.of iterations log(b.length+1) 0 = 20 - 1 0 0 1 = 21 - 1 1 1 3 = 22 - 1 2 2 7 = 23 - 1 3 3 15 = 24 - 1 4 4 32767 = 215 -1 15 15 1048575= 220 -1 20 20 j is the “base 2 logarithm of 2j. m is the base 10 logarithm of 10m. To search an array of a million entries using linear search may require a million iterations of the loop. To search an array of a million entries using binary search requires only 20 iteration! Linear search is a linear algorithm; the time it takes is proportional in the worst case to the size of the array. Binary search is a logarithmic algoririthm; the time it takes is proportional in the worst case to the size of the array. CS100A, Lecture 14, 20 October 1998