Professional Documents
Culture Documents
#include <iostream>
# define max 10
template<class T>
for(int i=0;i<n;i++)
if(key==arr[i])
exit(0);
int main()
int arr[max],n,key;
cin>>n;
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>
#define max 10
if(key==arr[i]&&i<n)
else if(i>n)
else
Lisearch(arr,i+1,n,key);
int main()
int arr[max],n,key;
cin>>n;
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>
#define max 10
template<class T>
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;
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
cin>>key;
int res=Binary(arr,n,key);
if(res==-1)
else
return 0;
}
OUTPUT:
BINARYSEARCH(RECURSIVE)
#include<iostream>
template<class T>
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;
cin>>n;
cout<<"enter "<<n<<"no's\n";
for(i=0;i<n;i++)
cin>>arr[i];
cin>>key;
pos=RecursiveBsearch(arr,0,n-1,key);
if(pos==-1)
cout<<"unsuccessful search\n";
else
}
OUTPUT:
HASHING
#include <iostream>
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);
};
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;
int b=hsearch(k);
if(empty[b]||ht[b]!=k)
return false;
return true;
void Hashtable<K>::display()
for(int i=0;i<D;i++)
if(empty[i]==false)
cout<<ht[i]<<endl;
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;
cin>>n;
Hashtable<int>ha(n);
while(1)
cout<<"enter choice";
cin>>ch;
switch(ch)
case 1:
cin>>v;
ha.insert(v);
break;
case 2:
cin>>v;
key=ha.search(v);
if(key==true)
cout<<"element found";
else
cout<<"not found";
break;
case 3:
ha.display();
break;
case 4:
cin>>v;
ha.Delete(v);
break;
case 5:
exit(0);
return 0;
}
OUTPUT:
PRIORITYQUEUE
#include <iostream>
#define MAX 10
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)
arr[0]=arr[n];
n--;
heapify(0);
}
else
cout<<endl<<"empty";
void Maxpq<T>::push(T t)
n++;
arr[n]=t;
shiftup(n);
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;
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>
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)
cin>>choice;
switch(choice)
case 1:
cin>>value;
pq.push(value);
break;
case 2:
value=pq.top();
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>
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;
stack s;
};
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);
if(root==NULL){
root=newnode;
return;
if(tree->info==newnode->info){
if(tree->info>newnode->info){
if(tree->left!=NULL)
insert(tree->left,newnode);
else
tree->left=newnode;
return;
else
if(tree->right!=NULL)
insert(tree->right,newnode);
else{
tree->right=newnode;
(tree->right)->left=NULL;
(tree->right)->right=NULL;
return;
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;
if(root==NULL){
return;
if(ptr!=NULL){
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->info<<"->";
if(root==NULL){
cout<<"empty Binart tree";
return;
if(ptr!=NULL){
cout<<ptr->info<<"->";
preorder(ptr->left);
preorder(ptr->right);
if(r==NULL)
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;
while(1){
cin>>choice;
switch(choice){
cin>>temp->info;
temp->left=temp->right=NULL;
bst.insert(bst.root,temp);
break;
bst.preorder(bst.root);
break;
bst.inorder(bst.root);
break;
bst.postorder(bst.root);
break;
cin>>t;
b= bst.search(bst.root,t);
break;
case 6 :exit(1);
}}
return 0;
}
INSERTION SORT(INCREASING ORDER)
#include <iostream>
template<class T>
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<<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;
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>
template<class T>
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;
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>
template<class T>
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>
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>
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>
for(int i=0;i<n;i++)
cout<<x[i]<<" ";
cout<<endl;
int main()
int *a,n,i;
cin>>n;
a=new int[n];
for(i=0;i<n;i++)
cin>>a[i];
heapsort(a,n);
printarray(a,n);
}
QUICKSORT(INCREASING ORDER)
#include <iostream>
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>
T t;
t=t1;
t1=t2;
t2=t;
int main()
int a[100],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>
int n;
int no_passes;
template<class T>
int i, j, k;
T temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
no_passes++;
temp[k] = a[i];
k++;
i++;
else
temp[k] = a[j];
k++;
j++;
}
temp[k] = a[i];
k++;
i++;
temp[k] = a[j];
k++;
j++;
a[i] = temp[i-low];
template<class T>
int mid;
mid=(low+high)/2;
int main()
int i;
cin>>n;
int arr[n];
cout<<"enter"<<n<<"elements";
cin>>arr[i];
mergesort(arr, 0, n-1);
cout<<"\nafter sorting\n";
cout<<arr[i]<<" ";
return 0;
OUTPUT:
DFS
#include <iostream>
# 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;
cin>>n;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>a[i][j];
cin>>v;
cout<<endl<<"DFS traversal\n";
dfs(v);
return 0;
}
OUTPUT:
BFS
#include <iostream>
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;
cin>>n;
for(i=1;i<=n;i++)
q[i]=0;
visited[i]=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cin>>a[i][j];
cin>>v;
bfs(v);
for(i=1;i<=n;i++)
if(visited[i])
cout<<i;
else
return 0;
OUTPUT: