searching. rhs – soc 2 searching a magic trick: –let a person secretly choose a random number...

Post on 01-Jan-2016

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

with only 10 attempts– The person must say ”higher” if the number

is higher than your guess– The person must say ”lower” if the number

is lower than your guess

RHS – SOC 3

Searching

• The magic trick obviously does not work, if the person does not ”direct” us

• How many guesses would we then typically need?

• If we increase the range to 1 up to 2000, how many more guesses will we need?– In the undirected case: 500– In the directed case: 1

RHS – SOC 4

Searching

• Searching in sorted data is vastly easier than searching in unsorted data

• Consider an old-fashioned phone book– Given a name, find the number (easy)– Given a number, find the name (very hard)

• First search is Binary Search, second search is Linear Search

RHS – SOC 5

Searching

• Linear Search:– Given an unsorted array of data D containing

n elements, check if the element e is in D– No shortcuts; got to check each element in D

one by one– The time needed to do this is proportional to

the size of D, i.e. proportional to n

RHS – SOC 6

Searching

public boolean linearSearch(int e, int[] data, int n)

{

for (int i = 0; i < n; i++)

{

if (data[i] = e)

return true;

}

return false;

}

RHS – SOC 7

Searching

• Binary search:– Given a sorted array of data D containing n

elements, check if the element e is in D– Let us be smarter now:

• Check the value in the middle of the array• If the value is equal to e, we are done!• If the value is larger than e, then start over with the

first half of the array• If the value is smaller than e, then start over with

the second half of the array

RHS – SOC 8

Searching

4 11 21 24 30 37 44 67 8939

RHS – SOC 9

Searching

4 11 21 24 30 37 44 67 8939

RHS – SOC 10

Searching

37 44 67 8939

RHS – SOC 11

Searching

37 44 67 8939

RHS – SOC 12

Searching

3739

No match

RHS – SOC 13

Searching

• Clearly, Binary Search is much faster than Linear Search…

• …but how fast is it?

• We can do an analysis of the so-called run-time complexity of the algorithm

RHS – SOC 14

Run-time complexity

• The run-time complexity gives us a measure of the expected running time of a specific algorithm

• Or rather, how the run-ning time is proportional to the ”size” of the input

RHS – SOC 15

Run-time complexity

• Example – Linear Search– Input is an array of length n– We will – on average – need to examine n/2

elements in the array– One examination of an element takes a

constant amount of time– The complete running time is therefore

proportional to n.– If n doubles, the running time doubles

RHS – SOC 16

Run-time complexity

• More formally, we could write the expected running time T(n) as T(n) = k1n + k2

• k1 and k2 are constants, which may be different for different hardware, program-ming language, and other factors

• We are usually only interested in the proportionality, not the exact running time

RHS – SOC 17

Run-time complexity

• For denoting the proportionality between the running time and the input size, we use the notation

O(f(n))

• meaning: the proportionality between the running time and the input size follows the function f(n)

RHS – SOC 18

Run-time complexity

• Examples:– O(log2(n)) - very slow growth

– O(n) - linear growth (Linear Search)– O(n2) - quadratic growth (fairly fast)– O(n4) - fast growth– O(2n) - extremely fast growth

RHS – SOC 19

Run-time complexity

n 2 5 10 20 50

O(log2(n)) 1 2 3 4 6

O(n) 2 5 10 20 50

O(n2) 4 25 100 400 2,500

O(n4) 16 625 10,000 160,000 6 ×106

O(2n) 4 32 1,024 1 ×106 1 ×1015

RHS – SOC 20

Run-time complexity

• Given an algorithm, how do we then find the run-time complexity?

• We must examine the structure of loops in the algorithm, in particular nested loops

• We must examine the structure of method calls – very important in dealing with recursive algorithms

• Need to take library methods into account

RHS – SOC 21

Run-time complexity

• Example: Binary Search

• Input: Array of size n, element e to find

• Algorithm steps:– Until array has length 1, or e found

• Check value in middle of array• If value is e; done – else run again with half of the

array (which half depends on value)

RHS – SOC 22

Run-time complexity

• The time for doing Binary Search on n elements, is thus the sum of:– Doing a simple value comparison– Doing Binary Search on n/2 elements

• This can be written as:

T(n) = 1 + T(n/2)

• This is a recursive function definition

RHS – SOC 23

Run-time complexity

• If T(n) = 1 + T(n/2)

• Then T(n) = 1 + (1 + T(n/4))

• and generally T(n) = k + T(n/2k)

• Now write n = 2k, and thus k = log2(n)

• Then T(n) = log2(n) + T(1) = O(log2(n))

RHS – SOC 24

Run-time complexity

• Since the run-time complexity of Binary Search is O(log2(n)), it is much faster than Linear Search

• If you need to search an array more than once, sort it first…

• …even though sorting does have a higher run-time complexity than Linear Search

RHS – SOC 25

Run-time complexity

• And that’s why the magic trick works:

log2(1000) = 10

RHS – SOC 26

Exercises

• Review: R14.6

• Programming: P14.8

• Also try to program a recursive version of binary search

top related