COMPILED BY: SULMAN
AHMAD
Lecture # 18 & 19 & 20
Basic Searching Algorithms
INSTRUCTOR: Saeed
Ahmed
1
Searching
■ Searching is the process of determining whether
or not a given value exists in a data structure or a
storage media.
■ We will discuss two searching methods on 1D
(Linear) data structures:
– Linear search
– Binary search.
2
Linear Search
■ It is also called as Serial or Sequential Search.
■ The linear (or sequential) search algorithm on an
array is:
– Sequentially scan the array, comparing each array
item with the searched value.
– If a match is found; return the index of the matched
element; otherwise return NULL.
■ Note: linear search can be applied to both sorted
and unsorted arrays.
3
Linear Search
■ Step through array of records, one at a time.
■ Look for record with matching key.
■ Search stops when
– record with matching key is found
– or when search has examined all records without
success.
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)
For each item in the Array
If item == KEY
return the item's location
End If
End For
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.
■ Suppose we have an array of n records.
■ If we search for the first record, then it requires 1 comparison; if
the second, then 2 comparisons, and so on.
■ The average of all these searches is:
(1+2+3+4+5+6+7+8+9+…+n)/n = (n+1)/2 = O(n)
22
Advantages of Linear Search
■ The linear search is simple.
■ It is easier to understand & implement.
■ It does not require the data to be stored in any
particular order.
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.
■ It fails in very large databases especially in real-
time systems.
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
■ Suppose you’re searching for a word in a
dictionary, and it starts with O.
■ Again, you’ll start near the middle.
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.
■ Its input is a sorted list of elements.
■ If an element you’re looking for is in that list, binary search returns
the position where it’s located.
■ Otherwise, binary search returns NULL.
29
Comparison with Linear Search
■ I’m thinking of a number between 1 and 100.
■ You have to try to guess my number in the fewest tries possible.
■ With every guess, I’ll tell you if your guess is too low, too high, or
correct.
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.
■ Here’s how many numbers you can eliminate every time.
38
Comparison with Linear Search
■ Whatever number I’m thinking of, you can guess in a maximum of
seven guesses—because you eliminate so many numbers with every
guess!
39
Comparison with Linear Search
■ Suppose you’re looking for a word in the
dictionary. The dictionary has 240,000
words.
■ In the worst case, how many steps do you
think each search will take?
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,
Space Complexity S(n)Iterative = O(1)
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
51
If n is the size of the array,
Space Complexity S(n)Recursive = O(log2n)
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
52
A(32)
A(0) 1 call
Space Complexity A(1)
A(2)
2 calls
3 calls A(16) c
Algorithm BinSearch (Array A, KEY, U, L) A(4) 4 calls
A(8) 5 calls
If L>U A(8) c
A(16) 6 calls
return NULL
A(32) 7 calls
End If A(4) c
mid = (L+U)/2 A(n) x calls
If A[mid] == KEY A(2) c
return mid
ElseIf A[mid] < KEY A(1) c
return BinSearch(A, KEY, mid + 1, U)
Else A(0) c
return BinSearch(A, KEY, L, mid - 1)
End If c
53
A(32)
A(0) 1 call
Space Complexity A(1)
A(2)
log21+2=2 calls
log22+2=3 calls A(16) c
Algorithm BinSearch (Array A, KEY, U, L) A(4) log24+2=4 calls
If L>U A(8) log28+2=5 calls A(8) c
return NULL A(16) log216+2=6 calls
A(32) log232+2=7 calls
End If A(4) c
mid = (L+U)/2
A(n) log2n+2=x calls
If A[mid] == KEY A(2) c
return mid
ElseIf A[mid] < KEY A(1) c
return BinSearch(A, KEY, mid + 1, U)
Else A(0) c
return BinSearch(A, KEY, L, mid - 1)
End If c
54
If each block of stack
Space Complexity consists of k memory
Algorithm BinSearch (Array A, KEY, U, L)
locations, then total
If L>U
space required is k(log2n+2)
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)
log2n+2
Else blocks
return BinSearch(A, KEY, L, mid - 1)
End If
55
Space Complexity S(n)Recursive = O(log2n)
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)
log2n+2
Else blocks
return BinSearch(A, KEY, L, mid - 1)
End If
56
Time Complexity
Algorithm BinSearch (Array A, KEY, U, L)
If L>U T(n) = T( n/2 ) + c
return NULL T(1) = T(0) + c
End If T(0) = c
mid = (L+U)/2
If A[mid] == KEY
return mid
ElseIf A[mid] < KEY T(n) = T( n/2 ) + c
return BinSearch(A, KEY, mid + 1, U) T(1) = T(0) + c
Else T(0) = c
return BinSearch(A, KEY, L, mid - 1)
End If
57
Back Substitution Method
T(n) =T(n/2)+c, for n>1
T(1) =T(0)+c
T(0) =c This is the Anchor Condition
-------------------------------------------------------------------------
T(n) =T(n/2)+c …eq(i)
T(n/2) =T(n/2/2)+c
=T(n/22)+c …eq(ii)
T(n/22) =T(n/22/2)+c
=T(n/23)+c …eq(iii) 58
Back Substitution Method
Putting eq(ii) in eq(i): T(n) =T(n/2)+c …eq(i)
T(n/2) =T(n/22)+c …eq(ii)
T(n) =T(n/2)+c T(n/22) =T(n/23)+c …eq(iii)
=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
For what value of 2k will T(n/2k) become T(1). A(n/23) c
n/2k=1 n=2k k=log2n
T(n) = c(k+2)
= c(log2n+2)
A(n/2k) = T(1) c
= O(log2n)
A(0) c
62
Master Theorem
T(n) =T(n/2)+c, for n>1
Here, a=1, b=2, c=0, and d=0
Now, bc=20=1
Here a=bc, so second possibility is our selection.
As d=0, so solution will be:
T(n) = (nlog21log0+1n) = (n0log1n) = (log n)
63
Practice Questions
64
QUIZ #1
■ Time: 03:50 pm to 04:40 pm
■ Duration: 50 minutes
65
End of Lecture
THANK YOU
66