0% found this document useful (0 votes)
19 views

Name: Muheet Rashid Div-D7B Roll No. 43 A.Y.-2019-20 Subj. - Data Structures Binary Search Program Code

This document contains a C program code that implements a binary search algorithm to search for an element in an array. The program takes input of number of elements and values to populate the array. It then takes the element to search as input. It uses a binary search approach by repeatedly dividing the search interval in half and searching in the narrowed down sub-interval. The program outputs the index at which the element is found or reports that it is not present.

Uploaded by

Muheet Bhat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Name: Muheet Rashid Div-D7B Roll No. 43 A.Y.-2019-20 Subj. - Data Structures Binary Search Program Code

This document contains a C program code that implements a binary search algorithm to search for an element in an array. The program takes input of number of elements and values to populate the array. It then takes the element to search as input. It uses a binary search approach by repeatedly dividing the search interval in half and searching in the narrowed down sub-interval. The program outputs the index at which the element is found or reports that it is not present.

Uploaded by

Muheet Bhat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name: MUHEET RASHID DIV- D7B ROLL NO. 43 A.Y.

-2019-20

SUBJ.- DATA STRUCTURES

BINARY SEARCH

PROGRAM CODE:

#include<stdio.h>

void main()

int a[10],n,i,k,mid,u,l,c=0;

printf("¥nEnter the no. of elements of array: ");

scanf("%d",&n);

printf("¥nEnter the elements:");

for(i=0;i<n;i++)

printf("¥n Enter element %d : ",i+1);

scanf("%d",&a[i]);

printf("¥n Enter the element to be searched: ");

scanf("%d",&k);

u=n-1;

l=0;

while(u-l>=0)

mid=(u+l)/2;

if(k==a[mid])
{

printf("¥nElement found at index: %d",mid);

c++;

break;

else if(k>a[mid])

l=mid+1;

else

u=mid-1;

OUTPUT:

Enter the no. of elements of array: 6

Enter the elements:

Enter element 1 : 12

Enter element 2 : 23

Enter element 3 : 34

Enter element 4 : 45

Enter element 5 : 56

Enter element 6 : 67

Enter the element to be searched: 67

Element found at index: 5


Process returned 2686708 (0x28FEF4) execution time : 11.591 s

Press any key to continue.

You might also like