4. Algorithm
[1] i = 1
[2] If K = A[i] , Print “Search is
Successful” and Stop
[3] i = i + 1
[4] If (i <= n) then Go To Step [2]
[5] Else Print “Search is
Unsuccessful” and Stop
[6] Exit
5. Complexity Of Linear Search Array
Case 1: Key matches with the first
element
T(n) = Number of Comparison
T(n) = 1, Best Case = O(1)
Case 2: Key does not exist
T(n) = n, Worst Case = O(n)
Case 3: Key is present at any location
T(n) = (n+1)/2, Average Case = O(n)
6. Linear Search with Linked List
A
Head
Best Case
Worst Case
Average Case
B
C
7. Linear Search with Ordered List
5
7
9
10
13
56
76
82
110
112
1
2
3
4
5
6
7
8
9
10
8. Algorithm
[1] i = 1
[2] If K = A[i] , Print “Search is
Successful” and Stop
[3] i = i + 1
[4] If (i <= n) and (A[i] <= K) then Go To
Step [2]
[5] Else Print “Search is
Unsuccessful” and Stop
[6] Exit
9. Complexity
Case 1: Key matches with the first
element
T(n) = Number of Comparison
T(n) = 1, Best Case = O(1)
Case 2: Key does not exist
T(n) = (n + 1)/2, Worst Case = O(n)
Case 3: Key is present at any location
T(n) = (n+1)/2, Average Case = O(n)
10. Binary Search
l
u
mid
mid = (l + u) /2 K = A[mid] then done
l
u
If K < A[mid]
mid
u = mid -1
mid l
u
If K > A[mid] l = mid +1
11. Algorithm
[1] l =1, u =n
[2] while (l < u) repeat steps 3 to 7
[3] mid = (l + u) / 2
[4] if K = A[mid] then print Successful
and Stop
[5] if K < A[mid] then
[6] u = mid -1
[7] else l = mid + 1
[8] Print Unsuccessful and Exit
15. Fibonacci Search
Fn = Fn – 1 + Fn – 2
with F0 = 0 and F1 = 1
If we expand the nth Fibonacci number
into the form of a recurrence tree, its
result into a binary tree and we can
term this as Fibonacci tree of order n.
16. In a Fibonacci tree, a node correspond to
Fi the ith Fibonacci number
Fi-1 is the left subtree
Fi-2 is the right subtree
Fi-1 and Fi-2 are further expanded until F0
and F1.
18. Apply the following two rules to obtain
the Fibonacci search tree of order n
from a Fibonacci tree of order n
Rule 1: For all leaf nodes corresponding to
F1 and F0, we denote them as external
nodes and redraw them as squares, and
the value of each external node is set to
zero. Value of each node is replaced by
its corresponding Fibonacci number.
19. Rule 2: For all nodes corresponding to Fi
(i>=2), each of them as a Fibonacci
search tree of order i such that
(a) the root is Fi
(b) the left-subtree is a Fibonacci
search tree of order i -1
(c) the right-subtree is a Fibonacci
search tree of order i -2 with all
numbers increased by Fi
23. Algorithm
Elements are in sorted order. Number of
elements n is related to a perfect
Fibonacci number Fk+1 such that Fk+1 =
n+1
24. [1] i = Fk
[2] p = Fk-1 , q = Fk-2
[3] If ( K < Ki ) then
[4] If (q==0) then Print “unsuccessful
and exit”
[5]
Else i = i – q, pold = p, p =q,
q =pold – q
[6] Goto step 3
25. [7] If (K > Ki ) then
[8] If (p ==1) then Print “Unsuccessful
and Exit”
[9] Else i = i + q, p = p+q, q=q-p
[10] Goto step 3
[11] If ( k == Ki ) then Print “Successful at
ith location”
[12] Stop