Download presentation
Presentation is loading. Please wait.
Published bySamuel Dalton Modified over 9 years ago
1
Searching
2
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
3
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
4
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
5
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
6
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; }
7
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
8
RHS – SOC 8 Searching 4112124303744678939
9
RHS – SOC 9 Searching 4112124303744678939
10
RHS – SOC 10 Searching 3744678939
11
RHS – SOC 11 Searching 3744678939
12
RHS – SOC 12 Searching 3739 No match …
13
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
14
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
15
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
16
RHS – SOC 16 Run-time complexity More formally, we could write the expected running time T(n) as T(n) = k 1 n + k 2 k 1 and k 2 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
17
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)
18
RHS – SOC 18 Run-time complexity Examples: –O(log 2 (n))- very slow growth –O(n)- linear growth (Linear Search) –O(n 2 )- quadratic growth (fairly fast) –O(n 4 )- fast growth –O(2 n )- extremely fast growth
19
RHS – SOC 19 Run-time complexity n25102050 O(log 2 (n))12346 O(n)25102050 O(n 2 )4251004002,500 O(n 4 )1662510,000160,0006 ×10 6 O(2 n )4321,0241 ×10 6 1 ×10 15
20
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
21
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)
22
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
23
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/2 k ) Now write n = 2 k, and thus k = log 2 (n) Then T(n) = log 2 (n) + T(1) = O(log 2 (n))
24
RHS – SOC 24 Run-time complexity Since the run-time complexity of Binary Search is O(log 2 (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
25
RHS – SOC 25 Run-time complexity And that’s why the magic trick works: log 2 (1000) = 10
26
RHS – SOC 26 Exercises Review: R14.6 Programming: P14.8 Also try to program a recursive version of binary search
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.