You are on page 1of 10

Welcome To My Presentation About

A Simple C Programming
Presented by
Robiul Ibne Ali
ID:CSE-36
Registration no:17502004435
Department of Computer Science and Engineering
Supervised by
MD.Hemaet Uddin
Department of Computer Science and Engineering
Dhaka Commerce College
Presentation Outlines:

 Introduction
 Problem Statement
 What is binary search algorithm?
 Programming Code
 Output
Introduction

■ Binary search works on sorted arrays. Binary search begins by comparing an element in the
middle of the array with the target value. If the target value matches the element, its position
in the array is returned. If the target value is less than the element, the search continues in the
lower half of the array. If the target value is greater than the element, the search continues in
the upper half of the array. By doing this, the algorithm eliminates the half in which the
target value cannot lie in each iteration
Problem Statement

■ Binary search can be performed on a sorted array.


■ If the array is unsorted, linear search is used to determine the position.
What is binary search algorithm?

A binary search, also known as a half-interval search, is an algorithm used in


computer science to locate a specified value (key) within an array. For the
search to be binary, the array must be sorted in either ascending or descending
order.
Programming Code
#include<stdio.h>
void main()
{
int i,n,first,last,middle,search,array[100];
printf("Enter Number of Elements in array:");
scanf("%d",&n);
printf("Enter the Elements in Array:");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
printf("Enter a Number to Search in Array:");
scanf("%d",&search);
first=0;
last=n-1;
middle=(first+last)/2;
while(first<=last)
{

if(array[middle]<search)
first=middle+1;
else if(array[middle]==search)
{
printf("%d is found at location %d",search,middle+1);
break;
}
else
last=middle-1;
middle=(first+last)/2;
}
if(first>last)
printf("Not found!%d isn't present in the Array",search);
}
Output:

■ Enter Number of Elements in array:5


Enter the Elements in Array: 2 3 7 9 8
Enter a Number to Search in Array:7
7 is present at location: 3
🙄
Any question?
Thank you

You might also like