You are on page 1of 6

National University of Modern Languages

Department of Software Engineering

Name: Abdul Rehman


Sys Id: Numl-S22-24338
Class: BSSE 31-2A
Instructor: Dr. Javveria Kanwal
Binary Search with Recursion:
#include<iostream>
using namespace std;
int binary_search(int arr[] , int item, int beg, int end);
int main()
{
int arr[]={3,6,2,4,9};
int item;
cout<<"ENTER THE ITEM : ";
cin>>item;
int beg=0;
int end=4;

int result = binary_search(arr , item, beg, end);


if (result == -1)
{
cout<<"VALUE NOT FOUND";
}
else {
cout<<"VALUE FOUND";
}
}
int binary_search(int arr[] , int item, int beg, int end)
{
if(beg>end)
{
return -1;
}

int mid = (beg+end)/2;

if(arr[mid] == item)
{
return mid;
}
else if(arr[mid] < item)
{
binary_search(arr , item, mid+1, end);
}
else if(arr[mid] > item)
{
binary_search(arr , item, beg, mid-1);
}
}

Stack:
#include<iostream>
using namespace std;
int stack[5];
int top = -1;
int size=5;
class stack1
{
public:
void push(int item)
{
if(top==size-1)
{
cout<<"STACK IS FULL:"<<endl;
exit(0);
}
else
{
top++;
stack[top]=item;
}

}
public:
int pop(){
if(top==-1)
{
cout<<" STACK IS EMPTY:"<<endl;

}
else
{
cout<<stack[top];
top--;

}
}
};
main()
{
stack1 obj;
obj.push(3);
obj.push(6);
obj.push(8);
obj.push(7);
obj.push(10);

obj.pop();
cout<<endl;
obj.pop();
cout<<endl;
obj.pop();
cout<<endl;
obj.pop();
cout<<endl;obj.pop();
cout<<endl;
}

You might also like