Data Structures Algorithms - Lecture 18 19 20 - Basic Searching Algorithms
Data Structures Algorithms - Lecture 18 19 20 - Basic Searching Algorithms
AHMAD
4
Linear Search
Linear Search ( Array A, KEY)
Step 1: Set i to L
Step 2: While i <= U do
Step 3: if A[i] = KEY then go to step 7
Step 4: Set i to i + 1
Step 5: End While
Step 6: Print “Element not found” & Exit
Step 7: Print “Element x Found at index i”
Step 8: Exit
5
Linear Search
procedure linear_search (Array, KEY)
End procedure
6
Linear Search
Algorithm LinearSearch (Array A[L..U], KEY)
For i = L to U
If A[i] == KEY
return i
End If
End For
return NULL
7
Working
KEY=33
8
Working
KEY=33
9
Working
KEY=33
10
Working
KEY=33
11
Working
KEY=33
12
Working
KEY=33
13
Working
KEY=33
14
Working
KEY=33
15
Working
KEY=33
1 2 3 4 5 6 7 8 9 10
return 7
16
Analysis of Linear Search
Algorithm LinearSearch (Array A[L..U], KEY)
For i = L to U
If A[i] == KEY
If n is the size of the array,
return i
End If S(n) = O(1)
End For
return NULL
17
Analysis of Linear Search
Algorithm LinearSearch (Array A[L..U], KEY)
For i = L to U
If A[i] == KEY If n is the size of the array,
the loop will run not more
return i than “n” times.
End If In each iteration, it performs
End For some constant time operations.
return NULL
T(n) = n.c = O(n)
18
Best Case T(n)BestCase = O(1)
KEY=10
1 2 3 4 5 6 7 8 9 10
return 1
19
Worst Case - 1 T(n)WorstCase = O(n)
KEY=44
1 2 3 4 5 6 7 8 9 10
return 10
20
Worst Case - 2 T(n)WorstCase = O(n)
KEY=100
1 2 3 4 5 6 7 8 9 10
return NULL
21
Average Case
■ In the Average Case, the value can be somewhere in the array.
■ Average Case Time Complexity is the average of Time
Complexities of all the possible inputs.
23
Disadvantages of Linear Search
■ It has a very poor efficiency as it takes lots of
comparisons to find a particular record in big
files.
24
Scenario 1
■ Suppose you’re searching for a person in the
phone book. His/Her name starts with K.
■ You could start at the beginning and keep
flipping pages until you get to the Ks.
■ But you’re more likely to start at a page in the
middle, because you know the Ks are going to be
near the middle of the phone book.
25
Scenario 2
26
Scenario 3
■ Suppose you log on to Facebook. When you do,
Facebook has to verify that you have an account on the
site.
■ So, it needs to search for your username in its database.
■ Let your username is LARAIB – for instance -
Facebook could start from the As and search for your
name.
■ But it makes more sense for it to begin somewhere in
2.7 billion monthly active users
the middle.
27
Binary Search
■ All these cases use the same algorithm to solve the problem: BINARY
SEARCH.
28
Binary Search
■ Binary search is a searching algorithm.
29
Comparison with Linear Search
■ I’m thinking of a number between 1 and 100.
30
Comparison with Linear Search
■ Suppose you start guessing like this: 1,
2, 3, 4 ….
■ Here’s how it would go.
31
Comparison with Linear Search
■ Suppose you start guessing like this: 1,
2, 3, 4 ….
■ Here’s how it would go.
32
Comparison with Linear Search
■ This is the sequential search.
■ With each guess, you’re eliminating only one number.
■ If my number was 99, it could take you 99 guesses to get there!
33
Comparison with Linear Search
■ Here’s a better technique.
■ Start with 50.
34
Comparison with Linear Search
■ You just eliminated half the numbers!
■ Now you know that 1–50 are all too low.
35
Comparison with Linear Search
■ Next guess: 75.
■ Too high, but again you cut down half the remaining numbers!
■ With binary search, you guess the middle number and eliminate half
the remaining numbers every time.
36
Comparison with Linear Search
■ Next is 63 (halfway between 50 and 75).
37
Comparison with Linear Search
■ This is binary search.
38
Comparison with Linear Search
39
Comparison with Linear Search
40
Comparison with Linear Search
■ Simple search could take 240,000 steps if the word you’re
looking for is the very last one in the book.
■ With each step of binary search, you cut the number of words in
half until you’re left with only one word.
41
Working
KEY=31
42
Working
KEY=31
43
Working
KEY=31
44
Working
KEY=31
45
Working
KEY=31
46
Working
KEY=31
return 5
47
Binary Search (Iterative Version)
Algorithm BinarySearch (Array A[L..U], KEY)
While L<=U, do
mid = (L+U)/2
If A[mid] == KEY
return mid
ElseIf A[mid] < KEY
L = mid + 1
Else
U = mid - 1
End If
End While
return NULL 48
Binary Search (Recursive Version)
Algorithm BinSearch (Array A, KEY, U, L)
If L>U
return NULL
End If
mid = (L+U)/2
If A[mid] == KEY
return mid
ElseIf A[mid] < KEY
return BinSearch(A, KEY, mid + 1, U)
Else
return BinSearch(A, KEY, L, mid - 1)
End If 49
Binary Search – Side by Side
Algorithm BinarySearch (Array A[L..U], KEY) Algorithm BinSearch (Array A, KEY, U, L)
While L<=U, do If L>U
mid = (L+U)/2 return NULL
If A[mid] == KEY End If
return mid mid = (L+U)/2
ElseIf A[mid] < KEY If A[mid] == KEY
L = mid + 1 return mid
Else ElseIf A[mid] < KEY
U = mid - 1 return BinSearch(A, KEY, mid + 1, U)
End If Else
End While return BinSearch(A, KEY, L, mid - 1)
return NULL End If
50
If n is the size of the array,
=T(n/22)+c+c
=T(n/22)+2c …eq(iv)
Putting eq(iii) in eq(iv):
T(n) = T(n/23)+c+2c
=T(n/23)+3c
Suppose we reach T(1) after k calls:
T(n) =T(n/2k)+kc ..eq(v)
59
Back Substitution Method
For what value of k will T(n/2k) become T(1).
n/2k=1 n=2k k=log2n
So eq(v) will become:
T(n) = T(n/2k)+kc
= T(1)+kc
= T(0) + c + kc
= c+c+kc
= 2c+clog2n
= O(log2n)
60
Recursion Tree Method
T(n) =T(n/2)+c, for n>1
T(1) =T(0)+c
T(0) =c This is the Anchor Condition
-------------------------------------------------------------------------
Note that here, at each step, only 1 branch will be drawn;
decreasing the input size by a factor of 2 at each step.
And a total of c work will be done at each branching.
61
A(n) c
A(n/2) c
A(n/22) c
62
Master Theorem
63
Practice Questions
64
QUIZ #1
■ Time: 03:50 pm to 04:40 pm
■ Duration: 50 minutes
65
End of Lecture
THANK YOU
66