You are on page 1of 49

LINEARSEARCH (ITERATIVE)

#include <iostream>

using namespace std;

# define max 10

template<class T>

void Lisearch(T arr[],int n,T key)

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

if(key==arr[i])

cout<<"element found at"<<i+1<<endl;

exit(0);

cout<<endl<<"key not found";

int main()

int arr[max],n,key;

cout<<endl<<"enter the size of array";

cin>>n;

cout<<endl<<"enter he array elements";

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

cin>>arr[i];
cout<<endl<<"enter the element to be searched";

cin>>key;

Lisearch(arr,n,key);

return 0;

OUTPUT:
LINEARSEARCH(RECURSIVE)

#include <iostream>

using namespace std;

#define max 10

template <class T>

void Lisearch(T arr[],int i,int n,T key)

if(key==arr[i]&&i<n)

cout<<"element found at"<<i+1<<endl;

else if(i>n)

cout<<endl<<"key not found"<<endl;

else

Lisearch(arr,i+1,n,key);

int main()

int arr[max],n,key;

cout<<"enter the size of array"<<endl;

cin>>n;

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

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

cin>>arr[i];
cout<<endl<<"enter the element to be searched";

cin>>key;

Lisearch(arr,0,n,key);

return 0;

OUTPUT:
BINARYSEARCH(ITERATIVE)

#include <iostream>

using namespace std;

#define max 10

template<class T>

int Binary(T arr[],int n,T key)

int mid;

int front=0;

int rear=n-1;

while(front<=rear)

mid=(front+rear)/2;

if(key==arr[mid])

return (mid+1);

else if(key<arr[mid])

rear=mid-1;

else

front=mid+1;

}
return(-1);

int main()

int arr[max],key,n;

cout<<endl<<"enter the size of array";

cin>>n;

cout<<"enter the elements of array";

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

cin>>arr[i];

cout<<"enter the key to be searched";

cin>>key;

int res=Binary(arr,n,key);

if(res==-1)

cout<<"key not found";

else

cout<<"key found at"<< res;

return 0;

}
OUTPUT:
BINARYSEARCH(RECURSIVE)

#include<iostream>

using namespace std;

template<class T>

int RecursiveBsearch(T arr[],int low, int high,T key)

int mid;

if(low<=high)

mid=(low+high)/2;

if(key==arr[mid])

cout<<"search successful\n";

return (mid);

else if(key>arr[mid])

RecursiveBsearch(arr,mid+1,high,key);

else

RecursiveBsearch(arr,low,mid-1,key);

else

return (-1);

int main()

{
int arr[10];

int n,i,pos,key;

cout<<"enter how many elements:\n";

cin>>n;

cout<<"enter "<<n<<"no's\n";

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

cin>>arr[i];

cout<<"enter a key value:\n";

cin>>key;

pos=RecursiveBsearch(arr,0,n-1,key);

if(pos==-1)

cout<<"unsuccessful search\n";

else

cout<<key<<"is found at position "<<pos;

}
OUTPUT:
HASHING

#include <iostream>

using namespace std;

bool anyval;

template<class K>

class Hashtable

int D;

K* ht;

bool* empty;

public:

Hashtable(int divisor)

D=divisor;

ht=new K[D];

empty=new bool[D];

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

empty[i]=true;

~Hashtable()

delete[]ht;

delete[]empty;

bool search(K);

void insert(K);

void display();
int hsearch(K);

void Delete(K);

};

template <class K>

int Hashtable<K>::hsearch(K k)

int i=k%D;

int j=i;

do{

if(empty[j]||ht[j]==k)

return j;

j=(j+1)%D;

while(j!=i);

return j;

template <class K>

bool Hashtable<K>::search(K k){

int b=hsearch(k);

if(empty[b]||ht[b]!=k)

return false;

return true;

template <class K>

void Hashtable<K>::display()

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

if(empty[i]==false)
cout<<ht[i]<<endl;

template <class K>

void Hashtable<K>::insert(K k)

int b=hsearch(k);

if(ht[b]==k)

cout<<"duplicate";

return;

if(empty[b])

empty[b]=false;

ht[b]=k;

template<class K>

void Hashtable<K>::Delete(K k)

for(int p=0;p<D;p++)

if(k==ht[p])

empty[p]=true;

}
int main()

int ch,v,n;

bool key;

cout<<"enter the size";

cin>>n;

Hashtable<int>ha(n);

cout<<"enter 1.insert 2.search 3.display 4.delete 5.exit\n";

while(1)

cout<<"enter choice";

cin>>ch;

switch(ch)

case 1:

cout<<"enter the element";

cin>>v;

ha.insert(v);

break;

case 2:

cout<<"enyer key value to search";

cin>>v;

key=ha.search(v);

if(key==true)

cout<<"element found";

else

cout<<"not found";

break;
case 3:

cout<<"the key values are";

ha.display();

break;

case 4:

cout<<"enter the value to delete\n";

cin>>v;

ha.Delete(v);

break;

case 5:

exit(0);

return 0;

}
OUTPUT:
PRIORITYQUEUE

#include <iostream>

#define MAX 10

using namespace std;

template<class T>

class Maxpq

T arr[MAX];

int n;

public:

Maxpq()

n=-1;

bool empty();

int size();

T top();

void pop();

void push(T);

void heapify(int);

void print();

void shiftup(int);

void swap(T&,T&);

};

template<class T>

bool Maxpq<T>::empty()

{
if(n==-1)

return true;

else

return false;

template<class T>

int Maxpq<T>::size()

return(n+1);

template<class T>

T Maxpq<T>::top()

if(n>=0)

return (arr[0]);

else

return(-1);

template<class T>

void Maxpq<T>::pop()

if(n!=0)

cout<<"popped element is"<<arr[0];

arr[0]=arr[n];

n--;

heapify(0);
}

else

cout<<endl<<"empty";

template <class T>

void Maxpq<T>::push(T t)

n++;

arr[n]=t;

shiftup(n);

template <class T>

void Maxpq<T>::shiftup(int i)

int parent=(i-1)/2;

while((i>0)&&(arr[parent]<arr[i]))

swap(arr[parent],arr[i]);

i=parent;

parent=(i-1)/2;

template <class T>

void Maxpq<T>::heapify(int i)

int largest=i;

int left=2*i+1;

int right=2*1+2;
if((left<n)&&(arr[left]>arr[largest]))

largest=left;

if((right<n)&&(arr[right]>arr[largest]))

largest=right;

if(largest!=i)

swap(arr[i],arr[largest]);

heapify(largest);

template<class T>

void Maxpq<T>::swap(T& t1,T& t2)

T t;

t=t1;

t1=t2;

t2=t;

template<class T>

void Maxpq<T>::print()

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

cout<<arr[i]<<" ";

int main()

{
Maxpq<int>pq;

int choice,value;

while(1)

cout<<"1.push 2.top 3.pop 4.print 5.exit\n";

cin>>choice;

switch(choice)

case 1:

cout<<"enter the value\n";

cin>>value;

pq.push(value);

break;

case 2:

value=pq.top();

cout<<"the top element is\n";

cout<<value;

break;

case 3:

pq.pop();

break;

case 4:

pq.print();

break;

case 5:

exit(0);
}

return 0;

OUTPUT:
INORDER TRAVERSAL OF A BINARY TREE (NON RECURSIVE)

#include<iostream>

#include<cstdlib>

using namespace std;

struct BTNode{

int info;

BTNode *left,*right;

};

class stack{

struct node{

BTNode *btnode;

node *next;

};

node *head,*top;

public:

stack(){

head=top=NULL;

void push(BTNode*);

BTNode* pop();

bool empty(){

if(top==NULL)

return true;

else

return false;

};
class BST{

public:

BTNode *root;

BST(){

root=NULL;

void insert(BTNode *,BTNode *);

void preorder(BTNode *);

void inorder(BTNode *);

void postorder(BTNode *);

BTNode* search(BTNode *,int);

stack s;

};

void stack::push(BTNode *t){

node *temp=new node;

temp->btnode=t;

temp->next=NULL;

if(head==NULL){

head=top=temp;

return;

top->next=temp;

top=temp;

BTNode* stack::pop(){

if(top==NULL){
cout<<"stack underflow";

BTNode *t;

t=top->btnode;

if(head==top){

delete(head);

head=top=NULL;

else{

node *p;

p=head;

while(p->next!=top)

p=p->next;

delete(top);

top=p;

top->next=NULL;

return(t);

void BST::insert(BTNode *tree,BTNode *newnode){

if(root==NULL){

root=newnode;

cout<<"Root node added"<<endl;

return;

if(tree->info==newnode->info){

cout<<"element alrady in the Binary tree"<<endl;


return;

if(tree->info>newnode->info){

if(tree->left!=NULL)

insert(tree->left,newnode);

else

tree->left=newnode;

cout<<"Node added to left"<<endl;

return;

else

if(tree->right!=NULL)

insert(tree->right,newnode);

else{

tree->right=newnode;

(tree->right)->left=NULL;

(tree->right)->right=NULL;

cout<<"Node added to right"<<endl;

return;

void BST::inorder(BTNode *ptr){

if(root==NULL){
cout<<"empty Binart tree";

return;

BTNode *cur=ptr;

while((s.empty()==false)||(cur!=NULL)){

while(cur!=NULL){

s.push(cur);

cur=cur->left;

cur=s.pop();

cout<<cur->info<<"->";

cur=cur->right;

void BST::postorder(BTNode *ptr){

if(root==NULL){

cout<<"empty Binart tree";

return;

if(ptr!=NULL){

postorder(ptr->left);

postorder(ptr->right);

cout<<ptr->info<<"->";

void BST::preorder(BTNode *ptr){

if(root==NULL){
cout<<"empty Binart tree";

return;

if(ptr!=NULL){

cout<<ptr->info<<"->";

preorder(ptr->left);

preorder(ptr->right);

BTNode* BST::search(BTNode *r,int key){

if(r==NULL)

{cout<<"element not found"<<endl;

return r;

else if(r->info==key){

cout<<"element found"<<endl;

return r;

else if(key>r->info)

return(search(r->right,key));

return(search(r->left,key));

int main(){

int choice,value,t;

BST bst;

BTNode *temp,*b;

cout<<"_________________________"<<endl;
cout<<"operations on binary search tree"<<endl;

cout<<"____________________________"<<endl;

cout<<"1.Insert element\n2.preorder Traversal\n3.Inorder Traversal\n4.postorder Traversal\


n5.search\n6.exit\n";

while(1){

cout<<"enter the choice";

cin>>choice;

switch(choice){

case 1 :temp=new BTNode;

cout<<"enter the number to be inserted";

cin>>temp->info;

temp->left=temp->right=NULL;

bst.insert(bst.root,temp);

break;

case 2 :cout<<"preorder Traversal"<<endl;

bst.preorder(bst.root);

break;

case 3 : cout<<"inorder Traversal"<<endl;

bst.inorder(bst.root);

break;

case 4 :cout<<"postorder Traversal"<<endl;

bst.postorder(bst.root);

break;

case 5:cout<<"search the element"<<endl;

cout<<"enter the element to search";

cin>>t;

b= bst.search(bst.root,t);

break;
case 6 :exit(1);

}}

return 0;

}
INSERTION SORT(INCREASING ORDER)

#include <iostream>

using namespace std;

template<class T>

void inssort(T a[],int n)

int i,j,k;

T temp;

int no_of_passes=0,no_comp=0,no_exch=0;

cout<<"unsorted list";

for(k=0;k<n;k++)

cout<<a[k]<<" ";

cout<<endl;

for(i=1;i<n;i++)

no_of_passes++;

temp=a[i];

j=i-1;

while((temp<a[j])&&(j>=0))

a[j+1]=a[j];

j--;

no_comp++;

no_exch++;

a[j+1]=temp;

cout<<"after pass"<<i<<" ";


for(k=0;k<n;k++)

cout<<a[k]<<" ";

cout<<endl;

cout<<"no of passes"<<no_of_passes;

cout<<"no of comparisions"<<no_comp;

cout<<"no of exchanges"<<no_exch;

int main()

int a[100],n;

cout<<"enter the number of elements\n";

cin>>n;

cout<<"enter"<<n<<"elements";

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

cin>>a[i];

inssort(a,n);

return 0;

OUTPUT:
SELECTION SORT(DECREASING ORDER)

#include <iostream>

using namespace std;

template<class T>

void selsort(T a[],int n)

int i,j,k,loc,temp;

T max;

cout<<"unsorted list";

for(k=0;k<n;k++)

cout<<a[k]<<" ";

cout<<endl;

for(i=0;i<n-1;i++)

max=a[i];

loc=i;

for(j=i+1;j<n;j++)

if(a[j]>max)

max=a[j];

loc=j;

if(loc!=i)

temp=a[i];
a[i]=a[loc];

a[loc]=temp;

cout<<"sorted list";

for(k=0;k<n;k++)

cout<<a[k]<<" ";

int main()

int a[100],n;

cout<<"enter the number of elements\n";

cin>>n;

cout<<"enter"<<n<<"elements";

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

cin>>a[i];

selsort(a,n);

return 0;

OUTPUT:
HEAP SORT(INCREASING ORDER)

#include <iostream>

using namespace std;

template<class T>

void heapify(T a[],int n)

int i,j,k;

T item;

for(k=1;k<n;k++)

item=a[k];

i=k;

j=(i-1)/2;

while((i>0)&&(item>a[j]))

a[i]=a[j];

i=j;

j=(i-1)/2;

a[i]=item;

template<class T>

void adjust(T a[],int n)

int i,j;

T item;

j=0;
item=a[j];

i=2*j +1;

while(i<=n-1)

if(i+1<=n-1)

if(a[i]<a[i+1])

i++;

if(item<a[i])

a[j]=a[i];

j=i;

i=2*j +1;

else

break;

a[j]=item;

template<class T>

void heapsort(T a[],int n)

int i,j;

T t;

heapify(a,n);

for(i=n-1;i>0;i--)

t=a[0];
a[0]=a[i];

a[i]=t;

adjust(a,i);

template<class T>

void printarray(T x[],int n)

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

cout<<x[i]<<" ";

cout<<endl;

int main()

int *a,n,i;

cout<<"enter the size\n";

cin>>n;

a=new int[n];

cout<<"enter the elements\n";

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

cin>>a[i];

heapsort(a,n);

cout<<"elements after sorting\n";

printarray(a,n);

}
QUICKSORT(INCREASING ORDER)

#include <iostream>

using namespace std;

template <class T>

void quicksort(T x[],int first,int last)

int i,j ,pivot;

if(first<last)

pivot=first;

i=first;

j=last;

while(i<j)

while((x[i]<=x[pivot])&&(i<last))

i++;

while(x[j]>x[pivot])

j--;

if(i<j)

swap(x[i],x[j]);

swap(x[pivot],x[j]);

quicksort(x,first,j-1);

quicksort(x,j+1,last);

}
template<class T>

void swap(T *t1,T *t2)

T t;

t=t1;

t1=t2;

t2=t;

int main()

int a[100],n;

cout<<"enter the number of elements\n";

cin>>n;

cout<<"enter"<<n<<"elements";

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

cin>>a[i];

quicksort(a,0,n-1);

cout<<"sorted list";

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

cout<<a[l]<<" ";

return 0;

}
MERGESORT(INCREASING ORDER)

#include <iostream>

using namespace std;

int n;

int no_passes;

template<class T>

void merge(T *a,int low, int high, int mid)

int i, j, k;

T temp[high-low+1];

i = low;

k = 0;

j = mid + 1;

while (i <= mid && j <= high)

no_passes++;

if (a[i] < a[j])

temp[k] = a[i];

k++;

i++;

else

temp[k] = a[j];

k++;

j++;
}

while (i <= mid)

temp[k] = a[i];

k++;

i++;

while (j <= high)

temp[k] = a[j];

k++;

j++;

for (i = low; i <= high; i++)

a[i] = temp[i-low];

template<class T>

void mergesort(T *a, int low, int high)

int mid;

if (low < high)

mid=(low+high)/2;

mergesort(a, low, mid);

mergesort(a, mid+1, high);

merge(a, low, high, mid);


}

int main()

int i;

cout<<"\nenter the number of element ";

cin>>n;

int arr[n];

cout<<"enter"<<n<<"elements";

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

cin>>arr[i];

mergesort(arr, 0, n-1);

cout<<"\nafter sorting\n";

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

cout<<arr[i]<<" ";

return 0;

OUTPUT:
DFS

#include <iostream>

using namespace std;

# define MAX 10

int a[MAX][MAX],visited[MAX],n;

void dfs(int v)

int i;

visited[v]=1;

for(i=1;i<=n;i++)

if(a[v][i]&&!visited[i])

cout<<v<<"->"<<i<<endl;

dfs(i);

int main()

int i,j,k,v;

cout<<endl<<"enter the no of vertices";

cin>>n;

cout<<endl<<"enter the adjacency matrix\n";

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)
cin>>a[i][j];

cout<<endl<<"enter starting vertex";

cin>>v;

cout<<endl<<"DFS traversal\n";

dfs(v);

return 0;

}
OUTPUT:
BFS

#include <iostream>

using namespace std;

int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;

void bfs(int v)

for(i=1;i<=n;i++)

if(a[v][i]&&!visited[i])

q[++r]=i;

if(f<=r)

visited[q[f]]=1;

bfs(q[f++]);

int main()

int v;

cout<<"enter the bo of vertices\n";

cin>>n;

for(i=1;i<=n;i++)

q[i]=0;

visited[i]=0;

cout<<"enter adjacency matrix\n";

for(i=1;i<=n;i++)

{
for(j=1;j<=n;j++)

cin>>a[i][j];

cout<<"enter the starting vertex\n";

cin>>v;

bfs(v);

cout<<"the node which are reachable are\n";

for(i=1;i<=n;i++)

if(visited[i])

cout<<i;

else

cout<<"bfs is not possible\n";

return 0;

OUTPUT:

You might also like