Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

CS 171: Introduction to Computer Science II Mergesort.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Cmpt-225 Algorithm Efficiency.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
Analysis of Algorithm.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SORTING AND SEARCHING CHAPTER Slides by Rick Giles 14.
Chapter 19 Searching, Sorting and Big O
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
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.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
Growth of Functions. Asymptotic Analysis for Algorithms T(n) = the maximum number of steps taken by an algorithm for any input of size n (worst-case runtime)
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
CSC 211 Data Structures Lecture 13
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Sorting.
Algorithm Analysis (Big O)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Big-O Analyzing Algorithms Asymptotically CS February 2002 Noah Smith ( )
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithm Analysis 1.
16 Searching and Sorting.
19 Searching and Sorting.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Searching – Linear and Binary Searches
Algorithmic Efficency
Lecture 14 Searching and Sorting Richard Gesick.
Sorting by Tammy Bailey
Building Java Programs
Lecture 11 Searching and Sorting Richard Gesick.
24 Searching and Sorting.
14 SORTING AND SEARCHING CHAPTER
Building Java Programs
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
Presentation transcript:

Searching – Linear and Binary Searches

Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2

You and Igor: the empirical approach Implement each candidate Run itTime it That could be lots of work – also error-prone. Which inputs? What machine/OS ?

Toward an analytic approach … How to solve “which algorithm” problems without machines, test data, or Igor!

The Big Picture Input (n = 3) Output Input (n = 4) Output Input (n = 8) Output How long does it take for the algorithm to finish? Algorithm

Primitives Primitive operations – x = 4assignment –... x arithmetic – if (x < y)...comparison – x[4]index an array – *xdereference (C) – x.foo( )calling a method Others – new/mallocmemory usage

How many foos? for (j = 1; j <= N; ++j) { foo( ); } ΣN j = 1 1 = N

How many foos? for (j = 1; j <= N; ++j) { for (k = 1; k <= M; ++k) { foo( ); } ΣN j = 1 1 = NM ΣM k = 1

How many foos? for (j = 1; j <= N; ++j) { for (k = 1; k <= j; ++k) { foo( ); } ΣN j = 1 1 = Σj k = 1 ΣN j = 1 j = N (N + 1) 2

How many foos? for (j = 0; j < N; ++j) { for (k = 0; k < j; ++k) { foo( ); } for (j = 0; j < N; ++j) { for (k = 0; k < M; ++k) { foo( ); } N(N + 1)/2 NM

How many foos? void foo(int N) { if(N <= 2) return; foo(N / 2); } T(0) = T(1) = T(2) = 1 T(n) = 1 + T(n/2) if n > 2 T(n)= 1 + (1 + T(n/4)) = 2 + T(n/4) = 2 + (1 + T(n/8)) = 3 + T(n/8) = 3 + (1 + T(n/16)) = 4 + T(n/16) … ≈ log 2 n

What is Big-O? Big-O refers to the order of an algorithm runtime growth in relation to the number of items Focus on dominant terms and ignore less significant terms and constants as n grows SortingBigOh

What is Big-O? Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant time (Push and pop elements on a stack) II. O(n) - linear time The algorithm requires a number of steps proportional to the size of the task. (Finding the minimum of a list) III. O(n 2 ) - quadratic time The number of operations is proportional to the size of the task squared. (Selection and Insertion sort) IV. O(log n) - logarithmic time (Binary search on a sorted list) V. O(n log n) - "n log n " time (Merge sort and quicksort) SortingBigOh

How would you find the first locker with a chinchilla in it?

It’s the 6 th one from the left

Linear search: also called sequential search Examines all values in an array until it finds a match or reaches the end Number of visits for a linear search of an array of n elements: – The average search visits n/2 elements – The maximum visits is n A linear search locates a value in an array in O(n) steps Searching

Pick a number between 1 and 100. You want to determine my number in the least amount of guesses possible. How do you do it? Pick a Number

Locates a value in a sorted array by – Determining whether the value occurs in the first or second half – Then repeating the search in one of the halves Binary Search

15 ≠ 17: we don't have a match

Count the number of visits to search an sorted array of size n – We visit one element (the middle element) then search either the left or right subarray – Thus: T(n) = T(n/2) + 1 where T(n) is time to search an array of size n If n is n/2, then T(n/2) = T(n/4) + 1 Substituting into the original equation: T(n) = T(n/4) + 2 This generalizes to: T(n) = T(n/2 k ) + k Binary Search

Assume n is a power of 2, n = 2 m where m = log 2 (n) Then: T(n) = 1 + log 2 (n) Binary search is an O(log(n)) algorithm Binary Search

Comparison of Sequential and Binary Searches

Assignment Program a linear/sequential search of an array of Strings – Output the index where the element was found. Output must be user friendly – “Your value was found at index 13”; – “Your value was found at index -1” or “Your value was not found” Program a binary search of an ArrayList of Integer – Recursive – “Your value was found in the array” or “Your value was not found in the array”