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

Student Name: Himanshu UID: 18 BCS 1717 Branch: CSE Section/Group: NTPP-1 (A) Semester: 5

The document contains a student's name, UID, branch, semester, and response to a question asking for a C program to implement binary search. The student provides a binarySearch function that takes an array, left and right indices, and a target value. It recursively searches the sorted array by checking the middle element and updating the left or right index based on whether the middle element is less than or greater than the target. The main function calls binarySearch on a sample array, prints whether the target element is found or not found based on the return value.

Uploaded by

Himanshu Kimothi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Student Name: Himanshu UID: 18 BCS 1717 Branch: CSE Section/Group: NTPP-1 (A) Semester: 5

The document contains a student's name, UID, branch, semester, and response to a question asking for a C program to implement binary search. The student provides a binarySearch function that takes an array, left and right indices, and a target value. It recursively searches the sorted array by checking the middle element and updating the left or right index based on whether the middle element is less than or greater than the target. The main function calls binarySearch on a sample array, prints whether the target element is found or not found based on the return value.

Uploaded by

Himanshu Kimothi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Student Name: Himanshu

UID: 18 BCS 1717

Branch: CSE

Section/Group : NTPP-1( A)

Semester: 5

Q1-Wap for the binary search?


Ans1- #include<stdio.h>
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1; A
}
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 40;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("The element is not present"
" in array")
: printf("The element is present at "
"index %d", result);
return 0;
}
OUTPUT:

You might also like