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

CAP770 Advanced Data Structure and Algorithm Ca1 Vaishnavi

The document presents a lab evaluation for the CAP770 Advanced Data Structure and Algorithm course, authored by Vaishnavi Sharma. It includes a recursive implementation of the Binary Search algorithm in C++, demonstrating how to find a target element in a sorted array. The code also handles cases where the target element is not present in the array.

Uploaded by

Subham Gupta
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)
3 views

CAP770 Advanced Data Structure and Algorithm Ca1 Vaishnavi

The document presents a lab evaluation for the CAP770 Advanced Data Structure and Algorithm course, authored by Vaishnavi Sharma. It includes a recursive implementation of the Binary Search algorithm in C++, demonstrating how to find a target element in a sorted array. The code also handles cases where the target element is not present in the array.

Uploaded by

Subham Gupta
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

CAP770 Advanced Data Structure and Algorithm

Lab Evaluation 1

Name: Vaishnavi Sharma


Reg No: 12208828
Roll No: B41
Section: D2215

Q. Implement Binary Search using Recursion.


ANS:
#include <iostream>
using namespace std;
int binarySearch(int arr[], int left, int right, int target) {
if (right >= left) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
}

if (arr[mid] > target) {


return binarySearch(arr, left, mid - 1, target);
}
return binarySearch(arr, mid + 1, right, target);
}
return -1;
}
int main() {
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int target = 10;
int result = binarySearch(arr, 0, n - 1, target);
if (result == -1) {
cout << "Element not present in array" << endl;
}
else {
cout << "Element found at index " << result << endl;
}
return 0;
}

OUTPUT:

You might also like