1. Premier University, Chittagong
Department of Computer Science & Engineering
Course Code : CSE-226
Course Name : Algorithm Design and Analysis Laboratory
Report Number : 02
Topics of Report : To search a Binary Number from an array and its
position using Iteractive Approach also by Divide
and Conquer Approach.
Report Submission Date : 02-February-2016, Tuesday.
Submitted By : Md. Bashartullah
Student ID : 1202310200480
Section : C-4-A
Session : January-2016
Remarks :
Submitted To : Mr. Mohammad Imran Chowdhury
Lecturer
Department of Computer Science & Engineering
Premier University, Chittagong.
2. Objective:
I. To search of abinary number and its’ position from an arrayusing Iteractive
approach and Using Divide and Conquer Approach.
II. Finding the way ofimplementing the best use ofthe algorithm to solvethe
problem.
III. Analyzing the data and outputs and finding abest possible way to solve
problems.
Problem Description:
Wewill try to solvethe problem in two ways. First of all, wewill follow the
Iteractive Approach and than wewill follow the Divide and Conquer Approach.
Wewill run the code with the following data:
[1] [2] [3] [4] [5]
15 25 35 45 55
Figure 1: Binary Search Tree
3. Algorithm:
Algorithm BinSearch (a,n,x)
{
low:= 1; high:=n;
while(low<=high) do
{
mid:= [(low+high)/2];
if(x<a[mid])
then high:=mid-1;
elseif(x>a[mid])
then
low:=mid+1;
else
return mid;
}
return 0;
}
Algorithm BinSearch (a,i, l, x)
{
if(l==i) then
{
if(x==a[i]) then return I;
elsereturn 0;
}
else
{
Iteractive Approach
Divide And Conquer Approach
4. mid:= [(i+l)/2];
if(x==a[mid]) then return mid;
elseif(x<a[mid]) then
return BinSearch (a, I,mid-1, x);
elsereturn BinSearch (a, mid+1, l, x);
}
}
Result Analysis:
Data Table:
Input:
Enter the elements of the array
5
Enter 5 integers
15
25
35
45
55
Output:
Enter value to find out
55
55 found atthe location of 5
5. Discussion:
Werun the algorithm of Binary Search in two different approaches like Iteractive
Approach and Divide And Conquer Approach.
Complexity Analysis:
6. Successful Searches
Θ(1) Θ(log n) Θ(log n)
Best Average Worst
Unsuccessful Searches
Θ(log n) Θ(log n) Θ(log n)
Best Average Worst
Appendix:
SourceCodeforIteractiveApproach:
#include<stdio.h>
int main()
{
int array[100], c, l, i, middle, n, search;
printf("Enter the elements of the arrayn ");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find outn");
scanf("%d", &search);
l = 0;
i = n - 1;
middle = (l + i) / 2;
while(l <= i)
{
if(array[middle] < search)
7. l = middle + 1;
else if(array[middle] = search)
{
printf("%d found at the location of %d", search, middle + 1);
break;
}
else
i = middle - 1;
middle =(l + i) / 2;
}
if(l > i)
printf("Not Found! %d is not exists in the list you enteredn", search);
return 0;
}
SourceCodeforDivideAndConquerApproach:
#include<stdio.h>
int binary_search(int *a, int n, int x);
int main()
{
int n, a[30], search, i, j, middle, high, low;
printf("Enter how many elements you want:n");
scanf("%d", &n);
printf("Enter the %d elements:n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("nEnter the value to searchn");
scanf("%d", &search);
low = 0;
high = n;
8. do
{
middle = (low + high) / 2;
if (search < a[middle])
high = middle - 1;
else if (search > a[middle])
low = middle + 1;
}
while (search != a[middle] && low <= high);
if (search == a[middle])
{
printf("Yes! Searching successful!!n");
printf("n %d found in position: %dn", search, middle + 1);
}
else
{
printf("n Oops! Searching Unsuccessfuln %d is not found in the list you gave
usn", search);
}
return 0;
}