Professional Documents
Culture Documents
Practical Lab Manual-CSE-492
Practical Lab Manual-CSE-492
Problem 1: Implement Binary Search (Recursive & Iterative) using Divide and Conquer
approach
10 20 30 40 50 60 70 80
Search Value = 80
Linear Search Time Complexity = size of array = O (n) = O (8)
10 20 30 40 50 60 70 80
0 1 2 3 4 5 6 7
Left = 0
Right = 7
FIRST:
´
Mid = (¿¿)/2 = (0 + 7)/2 ~ 4
10 20 30 40 50 60 70 80
0 1 2 3 4 5 6 7
SECOND:
JIS University
´
Mid = (¿¿)/2 = (5 + 7)/2 ~ 6
THIRD:
10 20 30 40 50 60 70 80
0 1 2 3 4 5 6 7
´
Mid = (¿¿)/2 = (7 + 7)/2 ~ 7
T (n) = T(n/2) + 1
= T(n/22) + 2
= .................................
= T(n/2i) + i
= T(1) + log2n
n/2i=1=>i = log2n
log28 = 3
JIS University
Theory:
Divide and Conquer
1. N-element sequence which is divided into two-subsequences of N/2 size
(Divide)...Definition is recursive
2. Sort the two subsequences recursively using merge sort (Conquer)
3. Merge all the solutions to get the total solution
Merge(A, p, q, r);
}