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

Vidya DAA Lab 1

daa lab

Uploaded by

shivadubey2002
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)
11 views2 pages

Vidya DAA Lab 1

daa lab

Uploaded by

shivadubey2002
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

Recursive Linear Search Program

#include <bits/stdc++.h> NAME: Vidya Thakur


using namespace std; ROLL NO.: 2200320130192
int linearsearch(int arr[], int size, int key)

if (size == 0)return -1;

else if (arr[size - 1] == key)return size - 1;

return linearsearch(arr, size - 1, key);

int main()

int n;

cout << "Enter the size of array: ";

cin >> n;

int arr[n];

cout << "Enter the array elements" << endl;

for (int i = 0; i < n; i++)

{ cin >> arr[i];}

int key;

cout << "Enter key: ";

cin >> key;

int ans = linearsearch(arr, 5, key);

if (ans == -1)cout << "The element " << key << " is not found."<< endl;

else cout << "The element " << key << " is found at " << ans << " index of the given array." << endl;

return 0;}

Output:
Recursive Binary Search Program
#include <iostream> NAME: Vidya Thakur
using namespace std; ROLL NO.: 2200320130192
int binarysearch(int *arr,int l,int h,int key){

int m = (l+h)/2;

if(arr[m]==key)return m;

else if(arr[m]>key)return binarysearch(arr,l,m-1,key);

else if(arr[m]<key)return binarysearch(arr,m+1,h,key);

else return -1; }

int main()

{ int n;

cout<<"Enter the size of array: ";

cin>>n;

int arr[n];

cout<<"Enter the array elements"<<endl;

for(int i=0;i<n;i++){

cin>>arr[i];

int k;

cout<<"Enter key: ";

cin>>k;

int l = 0,h = n-1;

int res = binarysearch(arr,l,h,k);

if(res==-1)cout<<"Key not found";

else cout<<"Key found at posiMon "<<res+1;}

Output:

You might also like