Docsity

Linear Search, Binary Search, Time Complexity Analysis | CMSC 131, Study Guides, Projects, Research of Computer Science

Partial preview of the text

Download Linear Search, Binary Search, Time Complexity Analysis | CMSC 131 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Lecture 39:

Searching

Last time: 1. Overloading and overriding revisited

Interfaces and class inheritance

Interface hierarchies Today: 1. Project #8 assigned!

Course Reviews

Linear search

Binary search

Time complexity analysis

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Project #8 Assigned! z

Project due 5/10 at 11pm

z

Project is open

z

Start before now! z

Read entire assignment from beginning to endbefore starting to code

z

Check out assignment now from CVS

z

Follow the instructions exactly , as much of grading is automated

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Searching z

A basic operation in computer science

z

Problem formulation z Given: z

Collection (array) of values

z

A property on values

z

Find: a value having the property in the array

z

Examples z Find a particular element

z

Find the least element

z

We will study different algorithms for solving thisproblem

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

The Search Game z

Pick an element in the range 0 … 1,

z

How long does it take to guess? z

This is a search problem

z

The collection: numbers 0 … 1,

z

The “property”: number chosen by the other party

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Implementing Linear Search z

Given: String array, String

z

Return: index of String in array, if String is inarray, or -1 if String is not in array public static int linSearch (String[] a, String elt) {

for (int i = 0; i < a.length; i++)
if (elt.equals(a[i])) return i;
return -1; // return -1 if elt is not in a

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

The Search Game Revised: Binary Search z

Remember lower, upper bound

z

Guess middle element z

Answer is “yes / higher / lower”

z

If “higher”, adjust lower bound

z

If “lower”, adjust higher bound

z

How long does it take?

z

This kind of algorithm is called binary search

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Which Search Is Better? z

For linear search: no need for sorted array

z

For binary search:

faster …

or is it?

z

How many guesses needed in search game(worst case) for z

Linear search?

z

Binary search?

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Answers z

For linear search: 1,024 z

Number chosen may be 1,

z

This would require each number to be guessed

z

For binary search: 10

(wow!)

z

Each guess rules out half of the remainingpossibilities

z

Total number of possibilities: 1,

z

This can be cut in half at most 10 times

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Answers z

Linear search: n

z

Binary search: log

2

n

z

log 2 n = number of times n can be cut in half

z

log 10 n = number of times n can be divided by 10

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Number of Guesses as nGrows

linSearch binSearch
n
guesses

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland) Binary Search: As data size n grows, doubling the data yields runtimes that increment by a constant amount z

Run time for binary search: T(n) = a log

b^

cn + d, where n is the data size

z

Prove: lim

n → ∞

T(2n) = T(n) + k, where k is a constant

z

Proof: z

First, note that T(n) = a log b^ cn + d and T(2n) = a log b^ c(2n) + d z Next, examine T(2n) more closely: z T(2n) = a log b^ c(2n) + d z = a log b^ 2(cn) + d z = a[log b^ 2 + log b^ (cn)] + d z = a log b^ 2 + a log b^ (cn) + d z = a log b^ 2 + [ a log b^ (cn) + d ] z = a log b^ 2 + T(n) z Since a log b^ 2 is simply a constant, we can set k = a log b^ 2 z So lim n→ ∞ T(2n) = T(n) + k, where k is a constant √ z

Note that this means each time we double the data size, the runtime increases by the sameconstant value k!

z

Example: T(n) =
log

2

n

n T(n) 64 6 128 7 256 8 n log 2 n Increments by 1 Increments by 1 Increments by 1

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Categories of Algorithms z

We say: z “Linear Search is θ (n)” z “Binary Search is θ( log 2 n) z θ(

θ( log (log n)) θ( log n) θ( log n * log n) θ( n) θ( n log n) θ( n log (log n)) θ( n logn * logn) θ( n

) θ( n 2 ) θ( n 100000000 ) θ( 2 n ) θ( 3 n ) θ(

n ) θ( 4 n ) θ( n!) θ( n n ) Huge chasm betweenpolynomial and exponential

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

“Asymptotic Worst-Case TimeComplexity” z

Asymptotic = “as n gets large” z Worst-case = “how many guesses in the worst case?” z Time complexity = “how much ‘time’ is consumed?” z Giving accurate “to-the-millisecond” characterizations notpossible because algorithms can run on different computers z Time complexity instead refers to “number of basic operations” z Basic operation for search game = guess z If n is a number, then f(n) is a function on that number (e.g. f(n) = 3n). z If an algorithm has worst-case processing time of f(n) for inputsize is n, then it is said to have asymptotic worst-case timecomplexity O(f(n)) (“big-oh of f(n)”) z Linear search is O(n) z Binary search is O(log 2 n)

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Analyzing Asymptotic Worst-Case Time Complexity: Code

public static int linSearch (String[] a, String elt) { for (int i = 0; i < a.length; i++) if (elt.equals(a[i])) return i; return -1; // return

if elt is not in a } z Analysis based on coarse assumptions z Comparisons are “one operation” z Assignments are one operation z What about linSearch ? z Loop executes n times, where n is number of elements in a z Each loop iteration: z Comparison involving i z Comparison involving elt ,^ a[i] z One assignment to i z So three operations per loop iteration z So worst-case time complexity is: O(3n) z By convention, constants omitted, so O(n)