0% found this document useful (0 votes)
7 views6 pages

Assignment 1

Uploaded by

Mannat Sharda
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)
7 views6 pages

Assignment 1

Uploaded by

Mannat Sharda
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/ 6

Assignment 1

Ques1. Implement iterative and recursive algorithms for (1)


Linear Search (2) Binary Search.

// Iterative algorithm of linear search

#include <iostream>
using namespace std;
int linearSearch(int arr[],int n, int key){
for(int i=0;i<n;i++){
if (arr[i]==key){
return i;
}
}
return -1;

}
int main(){
int size, key;
cout<<"Enter the size of the array :";
cin>>size;
int *array = new int[size];
cout<<"Enter elements in array"<<endl;
for(int i=0;i<size;i++){
cin>>array[i];
}
cout<<"Array is :";
for(int i=0;i<size;i++){
cout<<array[i]<<',';
}

cout<<"\nEnter a you want to be search :";


cin>>key;

int result = linearSearch(array, size, key);

if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
return 0;
}

Output:

Assignment 1 1
// Recursive linear search
#include <iostream>
using namespace std;

int rec(int a[],int key , int n){


if(n<0){
return -1;
}
if(a[n-1]==key){
return n-1;
}
return rec( a , key , n-1);

Assignment 1 2
}
int main(){
int n, key;
cout<<"enter size of array :";
cin >> n;
int* a = new int[n];
cout<<"Enter array elements:"<<endl;
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"display array"<<endl;
for(int i=0;i<n;i++){
cout<<a[i]<<',';
}
cout<<"\nenter a key to be searched in array :";
cin>>key;
int result = rec(a,key,n);
cout<<result;
delete[] a;
}

Output:

Assignment 1 3
// iterative binary search

#include <iostream>
using namespace std;
int binarySearch(int arr[], int n , int key){
int start=0,end=n;
while(start<=end){
int mid=(start+end)/2;

if(arr[mid]==key){
return mid;
}
else if(arr[mid]>key){
end = mid-1;
}
else{
start= mid+1;
}
}
return -1;
}
int main(){
int size,key;
cout<<"Enter the size of the array";
cin>>size;
int *array = new int[size];
cout<<"Enter elements in array"<<endl;
for(int i=0;i<size;i++){
cin>>array[i];
}
cout<<"Array is :";
for(int i=0;i<size;i++){
cout<<array[i]<<',';
}

Assignment 1 4
cout<<"\nEnter a you want to be search :";
cin>>key;
int result = binarySearch(array, size, key);

if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
return 0;
}

Output:

//recursive binary code

#include<iostream>
using namespace std;

int Rec(int a[],int mid,int key, int n){


if(a[mid]==key){
return mid;
}
else if(a[mid]<key){
return Rec(a, mid+1 , key , n);

}
else{
return Rec(a, 0 , key , mid-1);
}
return -1;

Assignment 1 5
}
int main(){
int n, mid , key;
cout<<"enter size of array :";
cin >> n;
int* a = new int[n];
cout<<"Enter array elements:"<<endl;
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"display array"<<endl;
for(int i=0;i<n;i++){
cout<<a[i]<<',';
}
cout<<"enter a key to be searched in array :";
cin>>key;
mid = n/2;
int result = Rec(a,mid,key,n);
cout<<result;
delete[] a;
}

Output:

Assignment 1 6

You might also like