Introduction. two of the most fundamental concepts in computer science are, given an array of values:

Similar documents
Introduction. two of the most fundamental concepts in computer science are, given an array of values:

ECE 122. Engineering Problem Solving Using Java

CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, March 18, 2009.

L14 Quicksort and Performance Optimization

(Refer Slide Time: 00:50)

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

Searching. 11. Searching

Here we will only consider the problem of searching among the elements of an array. Intro Programming in C++

Hash Table and Hashing

Searching, Sorting. Arizona State University 1

CS261 Data Structures. Ordered Array Dynamic Array Implementation

CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions. The Midterm Exam was give in class on Wednesday, October 21, 2009.

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

LECTURE 17. Array Searching and Sorting

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Sorting and Searching -- Introduction

Sorting & Searching (and a Tower)

Algorithm Design and Recursion. Search and Sort Algorithms

AE52/AC52/AT52 C & Data Structures JUNE 2014

Outline: Search and Recursion (Ch13)

Signed umbers. Sign/Magnitude otation

lecture notes September 2, How to sort?

We will stamp HW Block day:

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

The beautiful binary heap.

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

COMP 364: Computer Tools for Life Sciences

106B Final Review Session. Slides by Sierra Kaplan-Nelson and Kensen Shi Livestream managed by Jeffrey Barratt

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

The Beauty and Joy of Computing

Sorting Algorithms. + Analysis of the Sorting Algorithms

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

Binary Adders: Half Adders and Full Adders

Sorting is a problem for which we can prove a non-trivial lower bound.

SORTING AND SEARCHING

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

Chapter 8 Algorithms 1

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Repetition Through Recursion

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Math 4242 Polynomial Time algorithms, IndependentSet problem

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Sorting and Selection

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

Advanced Computer Programming

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

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Slide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

UNIT 5B Binary Search

Lecture 6 Binary Search

Instructions PLEASE READ (notice bold and underlined phrases)

5 Graphs

Expressions and Casting

Computers in Engineering COMP 208. Where s Waldo? Linear Search. Searching and Sorting Michael A. Hawker

Search Lesson Outline

Vector and Parallel Processors. Amdahl's Law

Programmer s spring SPECIFICATION FIRST ATTEMPT. int search( int [] a, int x) { // Pre: Sorted(a) // Post: a[r]= x } Weaken.

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

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

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

Data structures and libraries

Problem 1: Hello World!

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

CS10 The Beauty and Joy of Computing

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

MIDTERM EXAM (HONORS SECTION)

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

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

Algorithms and Data Structures

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

CS 223: Data Structures and Programming Techniques. Exam 2. April 19th, 2012

Problem:Given a list of n orderable items (e.g., numbers, characters from some alphabet, character strings), rearrange them in nondecreasing order.

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

4. SEARCHING AND SORTING LINEAR SEARCH

Sorting. Order in the court! sorting 1

Objectives of the lesson

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

C/C++ Programming Lecture 18 Name:

(Refer Slide Time: 01.26)

Lecture 15 Notes Binary Search Trees

PRAM Divide and Conquer Algorithms

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

EECE.2160: ECE Application Programming Spring 2018 Exam 3 May 10, 2018

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

COUNTING AND CONVERTING

Lecture 15 Binary Search Trees

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Chapter 7 Sorting. Terminology. Selection Sort

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

University of the Western Cape Department of Computer Science

CS187 - Science Gateway Seminar for CS and Math

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

The Big Picture. Chapter 3

Transcription:

Searching Class 28

Introduction two of the most fundamental concepts in computer science are, given an array of values: search through the values to see if a specific value is present and, if so, where sort the values in nondecreasing order

Introduction two of the most fundamental concepts in computer science are, given an array of values: search through the values to see if a specific value is present and, if so, where sort the values in nondecreasing order as a computer scientist, you must be able to understand and program several different algorithms for each of these tasks

Introduction two of the most fundamental concepts in computer science are, given an array of values: search through the values to see if a specific value is present and, if so, where sort the values in nondecreasing order as a computer scientist, you must be able to understand and program several different algorithms for each of these tasks in all of these slides, array is a generic term it means either an old-fashioned array or a modern vector

Searching an array that contains an arbitrary number of values each value is in a specific array location, but the values are not sorted 17 23 5 11 2 29 3

Searching an array that contains an arbitrary number of values each value is in a specific array location, but the values are not sorted 17 23 5 11 2 29 3 search for the value 11 do a comparison on 17, 23, 5, 11 (4 comparisons) return 3 (the position where 11 was found)

Searching an array that contains an arbitrary number of values each value is in a specific array location, but the values are not sorted 17 23 5 11 2 29 3 search for the value 11 do a comparison on 17, 23, 5, 11 (4 comparisons) return 3 (the position where 11 was found) search for the value 7 do comparisons on 17, 23, 5, 11, 2, 29, 3 (7 comparisons) return a not-found indicator

Linear Search Program 8-1, page 464, a function to do this int linearsearch(const int arr[], int size, int value); up to size elements of the array arr are searched for the existence of value if value is found within the first size elements of arr, the first position where value is encountered is returned by the function if value is not found, the sentinel value 1 is returned

Linear Search Program 8-1, page 464, a function to do this int linearsearch(const int arr[], int size, int value); up to size elements of the array arr are searched for the existence of value if value is found within the first size elements of arr, the first position where value is encountered is returned by the function if value is not found, the sentinel value 1 is returned the search is called linear search because the algorithm searches along the line of array elements, one by one, starting at the beginning, trying to find value

Problems there are several problems with Gaddis approach the biggest one is that none of his int data types will be accepted by a strict modern compiler the data type for a size is size t, and size t is an unsigned integer; thus, it is impossible to return 1 as a sentinel value for the not-found condition

Problems there are several problems with Gaddis approach the biggest one is that none of his int data types will be accepted by a strict modern compiler the data type for a size is size t, and size t is an unsigned integer; thus, it is impossible to return 1 as a sentinel value for the not-found condition a solution to this problem is to return the size of the array as the not-found sentinel value in an array of size, say 10, the largest valid index is 9 if the searched-for value is not found, we return 10, which is not a valid index, indicating not-found look at the complete program, using a vector: program 8 1 modified.cpp

Notice several things to note in the modified program: the return type of the function is size t the index and position are also of type size t there is no parameter for size because a vector knows how big it is

Searching a Sorted List linear search works perfectly on an unsorted array of values for unsorted values, it is the only practical searching algorithm

Searching a Sorted List linear search works perfectly on an unsorted array of values for unsorted values, it is the only practical searching algorithm linear search also works perfectly on a sorted array of values however, is it not the only search algorithm for sorted values furthermore, it is not a even a good algorithm in this case

Linear Search Analysis it is traditional to use n to denote the number of elements of an array (this is the same thing as vector s size)

Linear Search Analysis it is traditional to use n to denote the number of elements of an array (this is the same thing as vector s size) do the following experiment (I encourage you to write this program):

Linear Search Analysis it is traditional to use n to denote the number of elements of an array (this is the same thing as vector s size) do the following experiment (I encourage you to write this program): 1. put 1,000 random values, chosen from 0 10,000, sorted, into a vector

Linear Search Analysis it is traditional to use n to denote the number of elements of an array (this is the same thing as vector s size) do the following experiment (I encourage you to write this program): 1. put 1,000 random values, chosen from 0 10,000, sorted, into a vector 2. at random, pick a value in the range 0 10,000 and

Linear Search Analysis it is traditional to use n to denote the number of elements of an array (this is the same thing as vector s size) do the following experiment (I encourage you to write this program): 1. put 1,000 random values, chosen from 0 10,000, sorted, into a vector 2. at random, pick a value in the range 0 10,000 and 2.1 see if is in the vector or not

Linear Search Analysis it is traditional to use n to denote the number of elements of an array (this is the same thing as vector s size) do the following experiment (I encourage you to write this program): 1. put 1,000 random values, chosen from 0 10,000, sorted, into a vector 2. at random, pick a value in the range 0 10,000 and 2.1 see if is in the vector or not 2.2 count how many comparisons it takes to find out

Linear Search Analysis it is traditional to use n to denote the number of elements of an array (this is the same thing as vector s size) do the following experiment (I encourage you to write this program): 1. put 1,000 random values, chosen from 0 10,000, sorted, into a vector 2. at random, pick a value in the range 0 10,000 and 2.1 see if is in the vector or not 2.2 count how many comparisons it takes to find out 3. repeat step 2 10,000 times

Linear Search Analysis it is traditional to use n to denote the number of elements of an array (this is the same thing as vector s size) do the following experiment (I encourage you to write this program): 1. put 1,000 random values, chosen from 0 10,000, sorted, into a vector 2. at random, pick a value in the range 0 10,000 and 2.1 see if is in the vector or not 2.2 count how many comparisons it takes to find out 3. repeat step 2 10,000 times 4. calculate the average number of comparisons from step 2

Linear Search Analysis results: Total hits: 930 Total misses: 9070 Minimum comparisons: 1 Maximum comparisons: 1000 Average number of comparisons: 954.747 with 1,000 random values from 0 to 10,000 the average number of comparisons is about 955 have to search almost the entire array almost every time

Linear Search Linear Search Pros very easy algorithm to understand easy algorithm to code correctly the only practical algorithm for unsorted values Linear Search Cons very inefficient for sorted values must examine n/2 elements on average for a value that is in the array must examine all n elements for a value not in the array

Linear Search Linear Search Pros very easy algorithm to understand easy algorithm to code correctly the only practical algorithm for unsorted values Linear Search Cons very inefficient for sorted values must examine n/2 elements on average for a value that is in the array must examine all n elements for a value not in the array for sorted values, we can do better

Binary Search 2 3 5 11 17 23 29 1. divide the range of elements to search into 3:

Binary Search 2 3 5 11 17 23 29 1. divide the range of elements to search into 3: 1.1 the very middle element

Binary Search 2 3 5 11 17 23 29 1. divide the range of elements to search into 3: 1.1 the very middle element 1.2 the elements to the left of middle

Binary Search 2 3 5 11 17 23 29 1. divide the range of elements to search into 3: 1.1 the very middle element 1.2 the elements to the left of middle 1.3 the elements to the right of middle

Binary Search 2 3 5 11 17 23 29 1. divide the range of elements to search into 3: 1.1 the very middle element 1.2 the elements to the left of middle 1.3 the elements to the right of middle 2. if the range of elements is empty, return not-found sentinel

Binary Search 2 3 5 11 17 23 29 1. divide the range of elements to search into 3: 1.1 the very middle element 1.2 the elements to the left of middle 1.3 the elements to the right of middle 2. if the range of elements is empty, return not-found sentinel 3. else if the searched-for value is the middle element, you ve found it and you re done

Binary Search 2 3 5 11 17 23 29 1. divide the range of elements to search into 3: 1.1 the very middle element 1.2 the elements to the left of middle 1.3 the elements to the right of middle 2. if the range of elements is empty, return not-found sentinel 3. else if the searched-for value is the middle element, you ve found it and you re done 4. else if the searched-for value is smaller than the middle element, repeat step 1 on the left half

Binary Search 2 3 5 11 17 23 29 1. divide the range of elements to search into 3: 1.1 the very middle element 1.2 the elements to the left of middle 1.3 the elements to the right of middle 2. if the range of elements is empty, return not-found sentinel 3. else if the searched-for value is the middle element, you ve found it and you re done 4. else if the searched-for value is smaller than the middle element, repeat step 1 on the left half 5. else repeat step 1 on the right half

Binary Search 2 3 5 11 17 23 29 search for 11

Binary Search 2 3 5 11 17 23 29 search for 11 do a comparison on 11 (1 comparison)

Binary Search 2 3 5 11 17 23 29 search for 11 do a comparison on 11 (1 comparison) return 3 (the position where 11 was found) done

Binary Search 2 3 5 11 17 23 29 search for 11 do a comparison on 11 (1 comparison) return 3 (the position where 11 was found) done search for 7

Binary Search 2 3 5 11 17 23 29 search for 11 do a comparison on 11 (1 comparison) return 3 (the position where 11 was found) done search for 7 do a comparison on 11, 3, 5 (3 comparisons)

Binary Search 2 3 5 11 17 23 29 search for 11 do a comparison on 11 (1 comparison) return 3 (the position where 11 was found) done search for 7 do a comparison on 11, 3, 5 (3 comparisons) report not-found done

Binary Search Implementation as with linear search, Gaddis implementation won t compile on clang see program 8 2 modified.cpp

Binary Search Analysis do the same simulation with binary search as with linear search 10,000 times, search for a random number 0 1,000 results: Total hits: 971 Total misses: 9029 Minimum comparisons: 1 Maximum comparisons: 10 Average number of comparisons: 9.8768 remember the linear results: Total hits: 930 Total misses: 9070 Minimum comparisons: 1 Maximum comparisons: 1000 Average number of comparisons: 954.747 on average linear search takes a hundred times as many steps as binary search! binary search never takes more than ten comparisons

Binary Search Analysis let s dig a little deeper into exactly how long the two searches take

Binary Search Analysis let s dig a little deeper into exactly how long the two searches take linear search starts with a search space of size n

Binary Search Analysis let s dig a little deeper into exactly how long the two searches take linear search starts with a search space of size n with each comparison, the search space decreases by one

Binary Search Analysis let s dig a little deeper into exactly how long the two searches take linear search starts with a search space of size n with each comparison, the search space decreases by one if the value is not in the array, clearly it takes n steps to decrease the search space to size zero and determine not-found

Binary Search Analysis let s dig a little deeper into exactly how long the two searches take linear search starts with a search space of size n with each comparison, the search space decreases by one if the value is not in the array, clearly it takes n steps to decrease the search space to size zero and determine not-found binary search also starts with a search space of size n

Binary Search Analysis let s dig a little deeper into exactly how long the two searches take linear search starts with a search space of size n with each comparison, the search space decreases by one if the value is not in the array, clearly it takes n steps to decrease the search space to size zero and determine not-found binary search also starts with a search space of size n with each comparison, the search space decreases by half

Binary Search Analysis let s dig a little deeper into exactly how long the two searches take linear search starts with a search space of size n with each comparison, the search space decreases by one if the value is not in the array, clearly it takes n steps to decrease the search space to size zero and determine not-found binary search also starts with a search space of size n with each comparison, the search space decreases by half how many steps does it take to repeatedly divide a number by 2 until you get to zero?

Binary Search Analysis let s dig a little deeper into exactly how long the two searches take linear search starts with a search space of size n with each comparison, the search space decreases by one if the value is not in the array, clearly it takes n steps to decrease the search space to size zero and determine not-found binary search also starts with a search space of size n with each comparison, the search space decreases by half how many steps does it take to repeatedly divide a number by 2 until you get to zero? the term for this is logarithm

Logarithms logarithms are particularly important in algorithm analysis computer scientists deal mainly with base-2 logarithms the simplest working definition of a base-2 logarithm is Base-2 Logarithm How many times can you divide a number n by 2 using integer division before the result is 1 or 0? the answer to this question is approximately the base-2 logarithm of n you can estimate base-2 logarithms directly from the powers of 2 table

Powers of 2 you should learn all the powers of 2 from 0 to 10 2 0 = 1 2 1 = 2 2 2 = 4 2 3 = 8 2 4 = 16 2 5 = 32 2 6 = 64 2 7 = 128 2 8 = 256 2 9 = 512 2 10 = 1024

Binary Search remember the simulation results searching in an array with 1,000 elements the most comparisons ever needed was 10 the base-2 logarithm of 1,000 is approximately 10 the number of comparisons needed for binary search for an array of size n is at most log 2 n

Binary Search remember the simulation results searching in an array with 1,000 elements the most comparisons ever needed was 10 the base-2 logarithm of 1,000 is approximately 10 the number of comparisons needed for binary search for an array of size n is at most log 2 n compare this to linear search, which needs at most 1,000 comparisons for a 1,000-element array

Binary Search Binary Search Pros much more efficient than linear search requires at most log 2 n comparisons easy algorithm to understand and code correctly Binary Search Cons requires array elements to already be sorted

Binary Search Binary Search Pros much more efficient than linear search requires at most log 2 n comparisons easy algorithm to understand and code correctly Binary Search Cons requires array elements to already be sorted clearly, sorting is an issue, and we turn to that next