SlideShare a Scribd company logo
1 of 8
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.
 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
 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
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
 Discussion:
 Werun the algorithm of Binary Search in two different approaches like Iteractive
Approach and Divide And Conquer Approach.
 Complexity Analysis:
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)
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;
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;
}

More Related Content

What's hot

Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data StructureMeghaj Mallick
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructurerajshreemuthiah
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)Home
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stackvaibhav2910
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked listsAbdullah Al-hazmy
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary searchNisha Soms
 
Queue implementation
Queue implementationQueue implementation
Queue implementationRajendran
 

What's hot (20)

Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Hashing
HashingHashing
Hashing
 
Queue
QueueQueue
Queue
 
stack & queue
stack & queuestack & queue
stack & queue
 
Counting Sort
Counting SortCounting Sort
Counting Sort
 
Queues
QueuesQueues
Queues
 
Hashing
HashingHashing
Hashing
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 

Similar to Report 02(Binary Search)

Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)Make Mannan
 
Design and Analysis of Algorithm Brute Force 1.ppt
Design and Analysis of Algorithm Brute Force 1.pptDesign and Analysis of Algorithm Brute Force 1.ppt
Design and Analysis of Algorithm Brute Force 1.pptmoiza354
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab filesNitesh Dubey
 
Rsa Signature: Behind The Scenes
Rsa Signature: Behind The Scenes Rsa Signature: Behind The Scenes
Rsa Signature: Behind The Scenes acijjournal
 
Data structure and algorithm notes
Data structure and algorithm notesData structure and algorithm notes
Data structure and algorithm notessuman khadka
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfjanakim15
 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu
 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu
 
Sample prac exam2013
Sample prac exam2013Sample prac exam2013
Sample prac exam2013hccit
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear searchmontazur420
 

Similar to Report 02(Binary Search) (20)

DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
C lab excellent
C lab excellentC lab excellent
C lab excellent
 
C and Data Structures Lab Solutions
C and Data Structures Lab SolutionsC and Data Structures Lab Solutions
C and Data Structures Lab Solutions
 
C and Data Structures
C and Data Structures C and Data Structures
C and Data Structures
 
Chapter one
Chapter oneChapter one
Chapter one
 
Design and Analysis of Algorithm Brute Force 1.ppt
Design and Analysis of Algorithm Brute Force 1.pptDesign and Analysis of Algorithm Brute Force 1.ppt
Design and Analysis of Algorithm Brute Force 1.ppt
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
Rsa Signature: Behind The Scenes
Rsa Signature: Behind The Scenes Rsa Signature: Behind The Scenes
Rsa Signature: Behind The Scenes
 
Data structure and algorithm notes
Data structure and algorithm notesData structure and algorithm notes
Data structure and algorithm notes
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
 
Arrays
ArraysArrays
Arrays
 
Algorithms.
Algorithms. Algorithms.
Algorithms.
 
Sample prac exam2013
Sample prac exam2013Sample prac exam2013
Sample prac exam2013
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Arrays.pdf
Arrays.pdfArrays.pdf
Arrays.pdf
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 

Report 02(Binary Search)

  • 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; }