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

Binary Search in Array

The document discusses binary search in arrays. It defines a struct Array to hold array elements and functions to display array elements, swap elements, perform binary search, and recursively perform binary search. Main creates a sample array, calls binary search to find an element, displays the array, and returns 0.

Uploaded by

wissam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views2 pages

Binary Search in Array

The document discusses binary search in arrays. It defines a struct Array to hold array elements and functions to display array elements, swap elements, perform binary search, and recursively perform binary search. Main creates a sample array, calls binary search to find an element, displays the array, and returns 0.

Uploaded by

wissam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Binary Search in Array

#include<stdio.h>
struct Array
{
int A[10];
int size;
int length;
};
void Display(struct Array arr)
{
int i;
printf("\nElements are\n");
for(i=0;i<arr.length;i++)
printf("%d ",arr.A[i]);
}
void swap(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}

int BinarySearch(struct Array arr,int key)


{
int l,mid,h;
l=0;
h=arr.length-1;
while(l<=h)
{
mid=(l+h)/2;
if(key==arr.A[mid])
return mid;
else if(key<arr.A[mid])
h=mid-1;
else
l=mid+1;
}
return -1;
}

int RBinSearch(int a[],int l,int h,int key)


{

int mid=0;
if(l<=h)
{
mid=(l+h)/2;
if(key==a[mid])
return mid;
else if(key<a[mid])
return RBinSearch(a,l,mid-1,key);
}
else
return RBinSearch(a,mid+1,h,key);
return -1;
}

int main()
{

struct Array arr1={{2,3,9,16,18,21,28,32,35},10,9};


printf("%d",BinarySearch(arr1,16));
Display(arr1);
return 0;
}

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