0% found this document useful (0 votes)
15 views1 page

Practical No 1

The document presents a practical programming exercise aimed at implementing a binary search algorithm in C. It includes the code for the binary search function and a main function that tests the search on a predefined array. The output confirms that the element being searched for is found at index 1.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views1 page

Practical No 1

The document presents a practical programming exercise aimed at implementing a binary search algorithm in C. It includes the code for the binary search function and a main function that tests the search on a predefined array. The output confirms that the element being searched for is found at index 1.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Practical No:01

Aim: WRITE A PROGRAM TO SEARCH ELEMENT IN AN ARRAY


SEARCH BINARY SEARCH

Student Name: Shreyash P. Bayskar

Class: BCA 1st Year

Code:
#include <stdio.h>
int binarySearch(int array[], int x, int low, int high){
while (low <= high){
int mid = low + (high - low)/2;

if (x == array[mid])
return mid;
if (x > array[mid])
low = mid +1;
else
high = mid -1;
}
return -1;
}
int main(void){
int array[]= {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array)/ sizeof (array[0]);
int x =4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}

Output:
Element is found at index 1

You might also like