Search Lesson Outline Searching Lesson Outline

Slides:



Advertisements
Similar presentations
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Advertisements

Chapter 1 – Basic Concepts
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Analysis of Algorithms
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
CSC 211 Data Structures Lecture 13
Searching Topics Sequential Search Binary Search.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Kevin Quinn Fall 2015.
Introduction to Analysis of Algorithms
CMPT 120 Topic: Searching – Part 2
Week 13: Searching and Sorting
CMPT 120 Topic: Searching – Part 2
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
CSC 222: Object-Oriented Programming
Recitation 13 Searching and Sorting.
CMPT 120 Topic: Searching – Part 1
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
COMP 53 – Week Seven Big O Sorting.
Introduction to Algorithms
Quicksort
Teach A level Computing: Algorithms and Data Structures
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Quicksort 1.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Algorithm design and Analysis
Announcements P2 is due tomorrow Prelim on Monday
Sorting Lesson Outline
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Dan Grossman Fall 2013.
Big O Notation.
Data Structures Review Session
Last Class We Covered Sorting algorithms Searching algorithms
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Searching and Sorting 1-D Arrays
Algorithm Efficiency: Searching and Sorting Algorithms
Searching CLRS, Sections 9.1 – 9.3.
Big-O, Ω, ϴ Trevor Brown CSC263 Tutorial 1 Big-O, Ω, ϴ Trevor Brown
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
24 Searching and Sorting.
Algorithmic Complexity
Sub-Quadratic Sorting Algorithms
Searching, Sorting, and Asymptotic Complexity
Analysis of Algorithms
Quicksort.
Data Structures Introduction
IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos
slides created by Ethan Apter
Quicksort.
Analysis of Algorithms
Linear and Binary Search
Discrete Mathematics CS 2610
Module 8 – Searching & Sorting Algorithms
Analysis of Algorithms
CMPT 120 Lecture 33 – Unit 5 – Internet and Big Data
Quicksort.
Presentation transcript:

Search Lesson Outline Searching Lesson Outline How to Find a Value in an Array? Linear Search Linear Search Code Linear Search Example #1 Linear Search Example #2 Linear Search Example #3 Linear Search Example #4 Linear Search Example #5 Linear Search Example #6 How Long Does Linear Search Take? Linear Search: Best Case Linear Search: Worst Case Linear Search: Average Case Why Do We Care About Search Time? “Big-O” Notation #1 “Big-O” Notation #2 “Big-O” Notation #4 “Big-O” Notation #5 “Big-O” Notation #6 Linear Search Code, Again Linear Search is O(n) in the Average Case A Better Search? Faster Search Requires Sorted Data Binary Search Binary Search Code Binary Search Example #1 Binary Search Example #2 Binary Search Example #3 Time Complexity of Binary Search #1 Time Complexity of Binary Search #2 Time Complexity of Binary Search #3 Need to Sort Array Search Lesson CS1313 Spring 2017

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

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

Linear Search Code int linear_search (float* array, int number_of_elements, float target_value) { /* linear_search */ const int first_element = 0; const int nonexistent_element = first_element - 1; 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 */ -23 97 18 21 5 -86 64 -37 Search Lesson CS1313 Spring 2017

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

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

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

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

Linear Search Example #5 -23 97 18 21 5 -86 64 -37 element Searching for -86. Search Lesson CS1313 Spring 2017

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

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? Search Lesson CS1313 Spring 2017

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. Search Lesson CS1313 Spring 2017

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. Search Lesson CS1313 Spring 2017

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 – which is proportional to the length of the array – O(n) again! Search Lesson CS1313 Spring 2017

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. Search Lesson CS1313 Spring 2017

“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. Search Lesson CS1313 Spring 2017

“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.” Search Lesson CS1313 Spring 2017

“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. Search Lesson CS1313 Spring 2017

“Big-O” Notation #5 Now, suppose we have an algorithm that, for n pieces of data, takes 0.03n2 + 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 n2 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. Search Lesson CS1313 Spring 2017

“Big-O” Notation #6 In the general case, suppose an algorithm on n pieces of data takes: cknk + ck-1nk-1 + ck-2nk-2 + ... + c1n + c0 for constants c0, c1, etc. Keep in mind the principles that we’ve already seen: We don’t care about the constants ci. We don’t care about the terms smaller than nk. We really only care about nk. So we say that this algorithm has time complexity “of order nk.” We denote this O(nk). We pronounce it “big-O of n to the k.” Search Lesson CS1313 Spring 2017

Linear Search Code, Again int linear_search (float* array, int number_of_elements, float target_value) { /* linear_search */ const int first_element = 0; const int nonexistent_element = first_element - 1; 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 */ -23 97 18 21 5 -86 64 -37 Search Lesson CS1313 Spring 2017

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(n2). You can imagine that, when n is big – a million, a billion, etc – this is terribly inefficient. Can we do better? Search Lesson CS1313 Spring 2017

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. Search Lesson CS1313 Spring 2017

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. Search Lesson CS1313 Spring 2017

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

Binary Search Code Search Lesson int binary_search (float* array, int number_of_elements, float target_value) { /* binary_search */ const int first_element = 0; const int nonexistent_element = first_element - 1; 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; Search Lesson CS1313 Spring 2017

Binary Search Code Search Lesson 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 */ Search Lesson CS1313 Spring 2017

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

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

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

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 kth iteration, it’s (1/2k-1) of the array. Search Lesson CS1313 Spring 2017

Time Complexity of Binary Search #2 How fast is binary search? For the kth iteration of the binary search loop, the search region is (1/2k-1) of the array. What’s the maximum number of loop iterations? log2n 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(log2n). Search Lesson CS1313 Spring 2017

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

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. Search Lesson CS1313 Spring 2017