Search Lesson Outline

Similar documents
ECE 122. Engineering Problem Solving Using Java

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

SORTING AND SEARCHING

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

ASYMPTOTIC COMPLEXITY

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Introduction to the Analysis of Algorithms. Algorithm

O(1) How long does a function take to run? CS61A Lecture 6

CS : Programming for Non-Majors, Fall 2018 Programming Project #5: Big Statistics Due by 10:20am Wednesday November

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

1 Dynamic Memory continued: Memory Leaks

Algorithm Design and Recursion. Search and Sort Algorithms

(Refer Slide Time: 01.26)

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

CS261 Data Structures. Ordered Array Dynamic Array Implementation

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

What is an algorithm?

Algorithm Analysis. Big Oh

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Data Structures Lecture 3 Order Notation and Recursion

1 ICS 161: Design and Analysis of Algorithms Lecture notes for January 23, Bucket Sorting

Analysis of Algorithms. 5-Dec-16

What Every Programmer Should Know About Floating-Point Arithmetic

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

ASYMPTOTIC COMPLEXITY

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Searching and Sorting

Sorting/Searching and File I/O. Sorting Searching Reading for this lecture: L&L

Sorting and Searching. Lecture 14

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

CS261 Data Structures. Ordered Bag Dynamic Array Implementa;on

Analysis of algorithms

EECS 203 Spring 2016 Lecture 8 Page 1 of 6

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Variables and Data Representation

Chapter Fourteen Bonus Lessons: Algorithms and Efficiency

BLM1011 Bilgisayar Bilimlerine Giriş I

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

Trombone players produce different pitches partly by varying the length of a tube.

Intro to Algorithms. Professor Kevin Gold

LeakDAS Version 4 The Complete Guide

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Sorting and Searching

ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

Math 4242 Polynomial Time algorithms, IndependentSet problem

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

CPSC 320: Intermediate Algorithm Design and Analysis. Tutorial: Week 3

A Balanced Introduction to Computer Science

Outline: Search and Recursion (Ch13)

Introduction to Analysis of Algorithms

Sorting. Bubble Sort. Selection Sort

CS 215 Lecture 17 Linked lists and time complexities

5 R1 The one green in the same place so either of these could be green.

Algorithm Analysis. This is based on Chapter 4 of the text.

Algorithms and Data Structures

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

MA 1128: Lecture 02 1/22/2018

Module 08: Searching and Sorting Algorithms

CMPSCI 105: Lecture #12 Searching, Sorting, Joins, and Indexing PART #1: SEARCHING AND SORTING. Linear Search. Binary Search.

Sorting. Dr. Baldassano Yu s Elite Education

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

Recursion: The Mirrors. (Walls & Mirrors - Chapter 2)

Engr. Joseph Ronald Canedo's Note 1

These are notes for the third lecture; if statements and loops.

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

User Defined Functions 2 Outline

Sorting, Searching and Array's Built-in Methods. Lecture 16 COMP110 Spring 2018

Section 0.3 The Order of Operations

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

COMP 250 Fall recurrences 2 Oct. 13, 2017

Lecture. Algorithm Design and Recursion. Richard E Sarkis CSC 161: The Art of Programming

Topics. Sorting. Sorting. 1) How can we sort data in an array? a) Selection Sort b) Insertion Sort

CS 261: Data Structures. Sorted Dynamic Array Bag and Set

9 R1 Get another piece of paper. We re going to have fun keeping track of (inaudible). Um How much time do you have? Are you getting tired?

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

CS 106 Introduction to Computer Science I

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS1 Lecture 22 Mar. 6, 2019

Computer Science 210 Data Structures Siena College Fall Topic Notes: Searching and Sorting

CSc 110, Spring 2017 Lecture 39: searching

(Refer Slide Time: 1:27)

CSCI 136 Data Structures & Advanced Programming. Lecture 7 Spring 2018 Bill and Jon

CmpSci 187: Programming with Data Structures Spring 2015

CSC236 Week 5. Larry Zhang

The Big Python Guide

lecture24: Disjoint Sets

Transcription:

1. Searching Lesson Outline 2. How to Find a Value in an Array? 3. Linear Search 4. Linear Search Code 5. Linear Search Example #1 6. Linear Search Example #2 7. Linear Search Example #3 8. Linear Search Example #4 9. Linear Search Example #5 10. Linear Search Example #6 11. How Long Does Linear Search Take? 12. Linear Search: Best Case 13. Linear Search: Worst Case 14. Linear Search: Average Case 15. Why Do We Care About Search Time? Outline 16. Big-O Notation #1 17. Big-O Notation #2 18. Big-O Notation #4 19. Big-O Notation #5 20. Big-O Notation #6 21. Linear Search Code, Again 22. Linear Search is O(n) in the Average Case 23. A Better Search? 24. Faster Search Requires Sorted Data 25. Binary Search 26. Binary Search Code 27. Binary Search Code 28. Binary Search Example #1 29. Binary Search Example #2 30. Binary Search Example #3 31. Time Complexity of Binary Search #1 32. Time Complexity of Binary Search #2 33. Time Complexity of Binary Search #3 34. Need to Sort Array CS1313 Spring 2007 1

How to Find a Value in an Array? -23 97 18 21 5-86 64 0-37 Suppose you have a big array full of data, and you want to find a particular value. How will you find that value? CS1313 Spring 2007 2

Linear Search -23 97 18 21 5-86 64 0-37 Linear search means looking at each element of the array, in turn, until you find the target value. CS1313 Spring 2007 3

Linear Search Code -23 97 18 21 5-86 64 0-37 int linear_search (float* array, int number_of_elements, float target_value) { /* linear_search */ const int nonexistent_element = -1; const int first_element = 0; int element; /* Idiotproofing belongs here. */ for (element = first_element; element < number_of_elements; element++) { if (array[element] == target_value) { return element; } /* if (array[element] ==...) */ } /* for element */ return nonexistent_element; } /* linear_search */ CS1313 Spring 2007 4

Linear Search Example #1-23 97 18 21 5-86 64 0-37 element Searching for -86. CS1313 Spring 2007 5

Linear Search Example #2-23 97 18 21 5-86 64 0-37 element Searching for -86. CS1313 Spring 2007 6

Linear Search Example #3-23 97 18 21 5-86 64 0-37 element Searching for -86. CS1313 Spring 2007 7

Linear Search Example #4-23 97 18 21 5-86 64 0-37 element Searching for -86. CS1313 Spring 2007 8

Linear Search Example #5-23 97 18 21 5-86 64 0-37 element Searching for -86. CS1313 Spring 2007 9

Linear Search Example #6-23 97 18 21 5-86 64 0-37 element Searching for -86: found! CS1313 Spring 2007 10

How Long Does Linear Search Take? Okay, great, now we know how to search. But how long will our search take? That s really three separate questions: How long will it take in the best case? How long will it take in the worst case? How long will it take in the average case? CS1313 Spring 2007 11

Linear Search: Best Case How long will our search take? In the best case, the target value is in the first element of the array. So the search takes some tiny, and constant, amount of time. Computer scientists denote this O(1) which will be explained later. In real life, we don t care about the best case, because it so rarely actually happens. CS1313 Spring 2007 12

Linear Search: Worst Case How long will our search take? In the worst case, the target value is in the last element of the array. So the search takes an amount of time proportional to the length of the array. Computer scientists denote this O(n) which will be explained later. CS1313 Spring 2007 13

Linear Search: Average Case How long will our search take? In the average case, the target value is somewhere in the array. In fact, since the target value can be anywhere in the array, any element of the array is equally likely. So on average, the target value will be in the middle of the array. So the search takes an amount of time proportional to half the length of the array also proportional to the length of the array O(n) again! CS1313 Spring 2007 14

Why Do We Care About Search Time? We know that time is money. So finding the fastest way to search (or any task) is good, because then we ll save time, which saves money. CS1313 Spring 2007 15

Big-O Notation #1 Suppose we can describe the amount of time it takes to do a task in terms of the number of pieces of data in the task. For example, suppose that our search algorithm takes 3n + 12 time units to execute. Well, as n becomes very big a million, a billion, etc then we stop caring about the 12, because it s basically zero compared to the 3n term. CS1313 Spring 2007 16

Big-O Notation #2 Suppose we can describe the amount of time it takes to do a task in terms of the number of pieces of data in the task. For example, suppose that our search algorithm takes 3n + 12 time units to execute. No matter the size of n, the 3 in 3n isn t all that interesting, because running on a different computer changes the actual time cost of a time unit. CS1313 Spring 2007 17

Big-O Notation #4 Suppose we can describe the amount of time it takes to do a task in terms of the number of pieces of data in the task. For example, suppose that our search algorithm takes 3n + 12 time units to execute. So, we really only care about the n in 3n + 12. CS1313 Spring 2007 18

Big-O Notation #5 Now, suppose we have an algorithm that, for n pieces of data, takes 0.03n 2 + 12n + 937.88 time units. Again, we don t care about the constants. And, as n becomes big a million, a billion, etc we no longer care about the n term (12n), because it grows much slower than the n 2 term. Nor do we care about the constant term (937.88), which doesn t grow at all. That is, as n becomes big, the smaller terms are so tiny as to be pretty much zero. CS1313 Spring 2007 19

Big-O Notation #6 In the general case, suppose an algorithm on n pieces of data takes: c k n k + c k-1 n k-1 + c k-2 n k-2 +... + c 1 n + c 0 for constants c 0, c 1, etc. Keep in mind the principles that we ve already seen: we don t care about the constants c i, and we don t care about the terms smaller than n k. We really only care about n k. So we say that this algorithm has time complexity of order n k. We denote this O(n k ). We pronounce it big-o of n to the k. CS1313 Spring 2007 20

Linear Search Code, Again -23 97 18 21 5-86 64 0-37 int linear_search (float* array, int number_of_elements, float target_value) { /* linear_search */ const int nonexistent_element = -1; const int first_element = 0; int element; /* Idiotproofing belongs here. */ for (element = first_element; element < number_of_elements; element++) { if (array[element] == target_value) { return element; } /* if (array[element] ==...) */ } /* for element */ return nonexistent_element; } /* linear_search */ CS1313 Spring 2007 21

Linear Search is O(n) in the Average Case Recapping, linear search is O(n) in the average case. But what if we expect to do lots of searches through our dataset of length n? What if we expect to do n searches on our n data? Well, the time complexity will be n. O(n), which is to say O(n 2 ). You can imagine that, when n is big a million, a billion, etc this is terribly inefficient. Can we do better? CS1313 Spring 2007 22

A Better Search? Consider how you search for someone in the phone book say, Henry Neeman. You start with the first letter of their last name, N. You guess roughly where N would be in the phonebook. You open to that page. If you re wrong, you move either forward or backward in the book that is, if you actually opened to J, you move forward, but if you opened to T, you move backward. You keep repeating this action until you find Neeman. CS1313 Spring 2007 23

Faster Search Requires Sorted Data Why not use linear search? Linear search means start at the beginning, and look at every piece of data until you find your target. This is much much slower than the way you search a phonebook in real life. Why? The reason you can do the phonebook search so quickly is because the names in the phonebook are sorted specifically, they re in alphabetical order by last name, then by first name. CS1313 Spring 2007 24

Binary Search The general term for a smart search through sorted data is a binary search. 1. The initial search region is the whole array. 2. Look at the data value in the middle of the search region. 3. If you ve found your target, stop. 4. If your target is less than the middle data value, the new search region is the lower half of the data. 5. If your target is greater than the middle data value, the new search region is the higher half of the data. 6. Continue from Step 2. CS1313 Spring 2007 25

Binary Search Code int binary_search (float* array, int number_of_elements, float target_value) { /* binary_search */ const int nonexistent_element = -1; const int first_element = 0; int low_element, middle_element, high_element; /* Idiotproofing goes here. */ /* Start with the entire array as the search region. */ low_element = first_element; high_element = number_of_elements 1; CS1313 Spring 2007 26

Binary Search Code while ((low_element > first_element) && (high_element < number_of_elements) && (low_element < high_element)) { /* Examine the middle of the current search region. */ middle_element = (low_element + high_element) / 2; /* What should we search next? */ if (array[middle_element] < target_value) { /* Reduce the search region to the lower half. */ high_element = middle_element - 1; } /* if (array[middle_element] < target_value) */ else if (array[middle_element] > target_value) { /* Reduce the search region to the higher half. */ low_element = middle_element + 1; } /* if (array[middle_element] > target_value) */ else { /* Target has been found, so stop searching. */ low_element = middle_element; high_element = middle_element; } /* if (array[middle_element] >...)...else */ } /* while (low_element < high_element) */ if (high_element == low_element) { return middle_element; } /* if (high_element == low_element) */ return nonexistent_element; } /* binary_search */ CS1313 Spring 2007 27

Binary Search Example #1-86 -37-23 0 5 18 21 64 97 low middle high Searching for 18. CS1313 Spring 2007 28

Binary Search Example #2-86 -37-23 0 5 18 21 64 97 low middle high Searching for 18. CS1313 Spring 2007 29

Binary Search Example #3-86 -37-23 0 5 18 21 64 97 low middle high Searching for 18: found! CS1313 Spring 2007 30

Time Complexity of Binary Search #1 How fast is binary search? Think about how it operates: after you examine a value, you cut the search region in half. So, the first iteration of the loop, your search region is the whole array. The second iteration, it s half the array. The third iteration, it s a quarter of the array.... The k th iteration, it s (1/2 k-1 ) of the array. CS1313 Spring 2007 31

Time Complexity of Binary Search #2 How fast is binary search? For the k th iteration of the binary search loop, the search region is (1/2 k-1 ) of the array. What s the maximum number of loop iterations? log 2 n That is, we can t cut the search region in half more than that many times. So, the time complexity of binary search is O(log 2 n). CS1313 Spring 2007 32

Time Complexity of Binary Search #3 How fast is binary search? We said that the time complexity of binary search is O(log 2 n). But, O(log 2 n) is exactly the same as O(log n). Why? Well, we know from math class that log a x log b x / log b a So the relationship between logs to various bases is simply a constant, (1 / log b a). Therefore, O(log n) is the same as O(log b n) for any base b we don t care about the base. CS1313 Spring 2007 33

Need to Sort Array Binary search only works if the array is already sorted. It turns out that sorting is a huge issue in computing and the subject of our next lecture. CS1313 Spring 2007 34