Top Banner
Searching – Linear and Binary Searches
25

Searching – Linear and Binary Searches

Feb 22, 2016

Download

Documents

donnan

Searching – Linear and Binary Searches. Comparing Algorithms. P1. P2. Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”?. You and Igor: the empirical approach. Implement each candidate. Run it. Time it. That could be lots of work – also error-prone. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Searching – Linear and Binary Searches

Searching – Linear and Binary Searches

Page 2: Searching – Linear and Binary Searches
Page 3: Searching – Linear and Binary Searches
Page 4: Searching – Linear and Binary Searches

Comparing Algorithms

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

P1 P2

Page 5: Searching – Linear and Binary Searches

You and Igor: the empirical approach

Implementeach

candidateRun it Time it

That could be lots of work – also error-prone.

Which inputs?

What machine/OS

?

Page 6: Searching – Linear and Binary Searches

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

Page 7: Searching – Linear and Binary Searches

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

Page 8: Searching – Linear and Binary Searches

Primitives

• Primitive operations– x = 4 assignment– ... x + 5 ... arithmetic– if (x < y) ... comparison– x[4] index an array– *x dereference (C)– x.foo( ) calling a method

• Others– new/malloc memory usage

Page 9: Searching – Linear and Binary Searches

How many foos?

for (j = 1; j <= N; ++j) {foo( );

}

ΣN

j = 11 = N

Page 10: Searching – Linear and Binary Searches

How many foos?

for (j = 1; j <= N; ++j) {for (k = 1; k <= M; ++k) {

foo( );}

}

ΣN

j = 1

1 = NMΣM

k = 1

Page 11: Searching – Linear and Binary Searches

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

Page 12: Searching – Linear and Binary Searches

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

Page 13: Searching – Linear and Binary Searches

How many foos?

void foo(int N) {if(N <= 2)

return;foo(N / 2);

}

T(0) = T(1) = T(2) = 1T(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)…≈ log2 n

Page 14: Searching – Linear and Binary Searches

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

Page 15: Searching – Linear and Binary Searches

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 timeThe algorithm requires a number of steps proportional to the size of the task.(Finding the minimum of a list)

III. O(n2) - quadratic timeThe 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

Page 16: Searching – Linear and Binary Searches

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

Page 17: Searching – Linear and Binary Searches

It’s the 6th one from the left

Page 18: Searching – Linear and Binary Searches

• 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

Page 19: Searching – Linear and Binary Searches

• 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

Page 20: Searching – Linear and Binary Searches

• 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

Page 21: Searching – Linear and Binary Searches

Binary Search

15 ≠ 17: we don't have a match

Page 22: Searching – Linear and Binary Searches

• 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/2k) + k

Binary Search

Page 23: Searching – Linear and Binary Searches

• Assume n is a power of 2,    n = 2m where m = log2(n)

• Then: T(n) = 1 + log2(n) • Binary search is an O(log(n)) algorithm

Binary Search

Page 24: Searching – Linear and Binary Searches

Comparison of Sequential and Binary Searches

Page 25: Searching – Linear 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”