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

binarysearch recursion

The document contains a C program that implements a binary search algorithm after sorting an array using bubble sort. It prompts the user to input the size and elements of the array, sorts the array, and then searches for a specified key. The program outputs the position of the key if found, or indicates that the key is not present in the array.
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)
4 views2 pages

binarysearch recursion

The document contains a C program that implements a binary search algorithm after sorting an array using bubble sort. It prompts the user to input the size and elements of the array, sorts the array, and then searches for a specified key. The program outputs the position of the key if found, or indicates that the key is not present in the array.
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

#include<stdio.

h>
int binarysearch(int a[],int l,int u,int key);
void bubblesort(int a[],int n);
int main()
{
int a[60],i,n,l,u,mid,key,flag=0;

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 the sorted elements are:");
for(i=0;i<n;i++)
{
printf("%4d",a[i]);
}
printf("\n enter the key:");
scanf("%d",&key);
l=0;
u=n-1;

flag=binarysearch(a,l,u,key);

if(flag!=0)
{
printf("\n element is available at %d place",flag);
}
else
{
printf("\n element is not available place");

}
return 0;

}
int binarysearch(int a[],int l,int u,int key)
{
int mid;
if(l<=u)
{
mid=(l+u)/2;
if(key==a[mid])
{
return mid;
}
else if(key<a[mid])
{
return binarysearch(a,l,mid-1,key);
}
else
{
return binarysearch(a,mid+1,u,key);
}
}
else
{
return 0;
}
}
void bubblesort(int a[],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

}
}
}
}

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