0% found this document useful (0 votes)
5 views2 pages

Binary Search Using Functions

The document presents a C program that implements binary search using functions. It includes a bubble sort function to sort an array before performing the search. The program prompts the user for the array size, elements, and the key to search for, displaying the result of the search or indicating if the element is not found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Binary Search Using Functions

The document presents a C program that implements binary search using functions. It includes a bubble sort function to sort an array before performing the search. The program prompts the user for the array size, elements, and the key to search for, displaying the result of the search or indicating if the element is not found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

BINARY SEARCH USING FUNCTIONS

#include<stdio.h>
void binarysearch(int a[],int l,int u,int mid,int key);
void swap(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
void bubblesort(int a[],int n)
{
int i,j;
for(i=n-1;i>0;i--)
{
for(j=0;j<i;j++)
{
if(a[j]>a[j+1])
{
swap(&a[j],&a[j+1]);

}
}
}
}
int main()
{
int a[60],i,n,l,u,mid,key;

printf("\n enter the size of the array::");


scanf("%d",&n);
printf("\n enter the elements of the array::");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bubblesort(a,n);
printf("\n display::");
for(i=0;i<n;i++)
{
printf("%4d",a[i]);
}
printf("\n enter the key:");
scanf("%d",&key);
l=0;
u=n-1;
mid=(l+u)/2;
binarysearch(a,l,u,mid,key);

}
void binarysearch(int a[],int l,int u,int mid,int key)
{
while(l<=u)
{
if(a[mid]<key)
{
l=mid+1;
}
else if(a[mid]==key)
{
printf("\n %d is available at index %d",key,mid);
break;
}
else
{
u=mid-1;
}
mid=(l+u)/2;
}
if(l>u)
{
printf("\n element is not found");
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy