Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
PRACTICAL
FILE
(COE-222)
SHUBHAM BANSAL
364/CO/15
COE-III
INDEX
S.No. Name of the Program Remarks
for(int i=0;i<d2-d1;i++)
c[i]=b[i];
for(int i=d2-d1;i<=d2;i++)
{
c[i]=b[i]+a[i-(d2-d1)];
}
}
cout<<"The addition of the two polynomials is :\n";
for(int i=0;i<=x;i++)
{
cout<<c[i]<<"x^"<<x-i+1;
if(i!=x)
cout<<"+ ";
}
cout<<"\nwant to continue(y/n)?\n";
cin>>ch;
}
return 0;
}
//Output
//Q.2 Multiplication of two polynomials using array
#include <iostream>
using namespace std;
int main()
{
int d1,d2,d3,i,j;
do
{
cout<<"Enter degree of 1st polynomial(max 9):";
cin>>d1;
}while(d1>9);
do
{
cout<<"Enter degree of 2nd polynomial(max 9):";
cin>>d2;
}while(d2>9);
d3=d1+d2;
int c1[10],c2[10],c3[10],c4[10];
cout<<"Enter coefficients of 1st polynomial(highest to lowest degree resp.):";
for (i = d1; i >= 0; i--)
{
cin>>c1[i];
}
cout<<"The first polynomial is:";
display(c1,d1);
cout<<"Enter coefficients of 2nd polynomial(highest to lowest degree resp.):";
for (i = d2; i >= 0; i--)
{
cin>>c2[i];
}
cout<<"The second polynomial is:";
display(c2,d2);
cout<<"The different steps involved are:"<<endl;
for(i=0;i<=d3;i++)
{
c3[i]=0;
c4[i]=0;
}
for(i=0; i<=d2; i++)
{
for(j=0; j<=d1 ;j++)
{
c3[i+j]+=(c1[j]*c2[i]);
c4[i+j]=(c1[j]*c2[i]);
}
display(c4,d3);
}
cout<<endl<<"Adding the coefficients of like terms:";
cout<<endl<<"The product of the two polynomials is:";
display(c3,d3);
return 0;
}
//output
//Q3. TRANSPOSE OF A SPARSE MATRIX
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Enter the number rows and columns of the matrix\n";
int n,r,c;
cout<<"Enter the number of rows :";
cin>>r;
cout<<"\nEnter the number of columns :";
cin>>c;
cout<<"\nEnter the number of triplets (x,y,value) for non zero elements\n";
cin>>n;
vector< vector< pair<int,int> > >g(c+1);
vector< vector< pair<int,int> > >m(r+1);
int x,y,val;
for(int i=0;i<n;i++)
{
cin>>x>>y>>val;
g[y].push_back(make_pair(x,val));
m[x].push_back(make_pair(y,val));
}
cout<<"\nThe Original matrix is: \n";
for(int i=1;i<=r;i++)
{
int k=1,l=1;
for(vector<pair<int,int> >::iterator j=m[i].begin();j!=m[i].end();)
{
if((*j).first!=k)
{
cout<<"0 ";
}
else
{
cout<<(*j).second<<" ";
l=(*j).first;
l++;
j++;
}
k++;
}
for(;l<=c;l++)
cout<<"0 ";
cout<<endl;
}
cout<<"\nThe transpose of matrix is: \n";
for(int i=1;i<=c;i++)
{
int k=1,l=1;
for(vector<pair<int,int> >::iterator j=g[i].begin();j!=g[i].end();)
{
if((*j).first!=k)
{
cout<<"0 ";
}
else
{
cout<<(*j).second<<" ";
l=(*j).first;
l++;
j++;
}
k++;
}
for(;l<=r;l++)
cout<<"0 ";
cout<<endl;
}
return 0;
}
//Output
//Q4. TRANSPOSE OF SPARSE MATRIX USING FAST TRANSPOSE ALGORITHM
#include<bits/stdc++.h>
using namespace std;
#define max 20
int main()
{
char ch='y';
while(ch=='y')
{
int n,b1[max][3],b2[max][3],i;
cout<<"enter total non zero elements in sparse matrix: ";
cin>>n;
cout<<"print value , x , y for each\n";
for(i=0;i<n;i++)
{
cin>>b1[i][0]>>b1[i][1]>>b1[i][2];
b2[i][0]=b1[i][0];
b2[i][1]=b1[i][2];
b2[i][2]=b1[i][1];
}
cout<<"tanspose of matrix is:\n";
for(i=0;i<n;i++)
{
cout<<b2[i][0]<<" "<<b2[i][1]<<" "<<b2[i][2]<<"\n";
}
cout<<"\nwant to continue (y/n)?\n";
cin>>ch;
}
return 0;
}
//Output
//Q5. EVALUATION OF POSTFIX EXPRESSION
#include<bits/stdc++.h>
using namespace std;
int st[20];
int top = -1;
void push(int x)
{
st[++top] = x;
}
int pop()
{
return st[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the postfix expression :");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
if(top!=0)
printf("Invalid expression\n");
else
printf("\nThe result of expression = %d\n\n",pop());
return 0;
}
//Output
//Q6.CONVERSION OF INFIX EXPRESSION TO POSTFIX EXPRESSION
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[1000],p[1000],k=0;
cout<<"Enter the infix expression in terms of a-z \n";
gets(s);
int u=strlen(s);
s[u]=')';
char st[1000];
int top=0;
st[top]='(';
for(int i=0;i<=u;i++)
{
if(s[i]=='+')
{
while(1)
{
if(st[top]=='+'||st[top]=='-'||st[top]=='*'||st[top]=='/'||st[top]=='^')
{
p[k++]=st[top];
top=top-1;
}
else
{
break;
}
}
top=top+1;
st[top]=s[i];
}
else if(s[i]=='-')
{
while(1)
{
if(st[top]=='-'||st[top]=='+'||st[top]=='*'||st[top]=='/'||st[top]=='^')
{
p[k++]=st[top];
top=top-1;
}
else
{
break;
}
}
top=top+1;
st[top]=s[i];
}
else if(s[i]=='*')
{
while(1)
{
if(st[top]=='*'||st[top]=='/'||st[top]=='^')
{
p[k++]=st[top];
top=top-1;
}
else
{
break;
}
}
top=top+1;
st[top]=s[i];
}
else if(s[i]=='/')
{
while(1)
{
if(st[top]=='*'||st[top]=='/'||st[top]=='^')
{
p[k++]=st[top];
top=top-1;
}
else
{
break;
}
}
top=top+1;
st[top]=s[i];
}
else if(s[i]=='^')
{
top=top+1;
st[top]=s[i];
}
else if(s[i]>='a'&&s[i]<='z')
{
p[k++]=s[i];
}
else if(s[i]=='(')
{
top=top+1;
st[top]=s[i];
}
else if(s[i]==')')
{
while(st[top]!='(')
{
p[k++]=st[top];
top=top-1;
}
top=top-1;
}
}
if(top!=-1)
cout<<"Invalid Infix Expression\n";
else
{
cout<<"The Postfix Expression is \n";
for(int i=0;i<k;i++)
cout<<p[i];
}
return 0;
}
//Output
//Q.7 tower of Hanoi without using recursion
#include<bits/stdc++.h>
using namespace std;
int p1[100],p2[100],p3[100],top1=0,top2=0,top3=0;
void pushp1(int a)
{
if(a!=0)
p1[top1++]=a;
}
int popp1(void)
{
if(top1==0)
return 0;
else
return p1[--top1];
}
void displayp1(void)
{
int i;
printf("Tower 1 ->> ");
if(top1==0)
printf("Empty");
else
for(i=0;i<top1;i++)
{
printf("%d ",p1[i]);
}
printf("\n");
}
void pushp2(int a)
{
if(a!=0)
p2[top2++]=a;
}
int popp2(void)
{
if(top2==0)
return 0;
else
return p2[--top2];
}
void displayp2(void)
{
int i;
printf("Tower 2->> ");
if(top2==0)
printf("Empty");
else
for(i=0;i<top2;i++)
{
printf("%d ",p2[i]);
}
printf("\n");
}
void pushp3(int a)
{
if(a!=0)
p3[top3++]=a;
}
int popp3(void)
{
if(top3==0)
return 0;
else
return p3[--top3];
}
void displayp3(void)
{
int i;
printf("Tower 3 ->> ");
if(top3==0)
printf("Empty");
else
for(i=0;i<top3;i++)
{
printf("%d ",p3[i]);
}
printf("\n");
}
int main()
{
int n,i,x,a,b;
printf("Enter no. of discs\n");
scanf("%d",&n);
for(i=0;i<n;i++)
pushp1(n-i);
x=pow(2,n)-1;
displayp1();
displayp2();
displayp3();
for(i=1;i<=x;i++)
{
if(i%3==1)
{
a=popp1();
b=popp3();
if(a==0)
{
pushp1(b);
}
else
if(b==0)
pushp3(a);
else
if(a>b)
{
pushp1(a);
pushp1(b);
}
else
{
pushp3(b);
pushp3(a);
}
displayp1();
displayp2();
displayp3();
continue;
}
else
if(i%3==2)
{
a=popp1();
b=popp2();
if(a==0)
{
pushp1(b);
}
else
if(b==0)
pushp2(a);
else
if(a>b)
{
pushp1(a);
pushp1(b);
}
else
{
pushp2(b);
pushp2(a);
}
displayp1();
displayp2();
displayp3();
continue;
}
else
if(i%3==0)
{
a=popp2();
b=popp3();
if(a==0)
{
pushp2(b);
}
else
if(b==0)
pushp3(a);
else
if(a>b)
{
pushp2(a);
pushp2(b);
}
else
{
pushp3(b);
pushp3(a);
}
displayp1();
displayp2();
displayp3();
continue;
}
}
printf("Complete\n");
displayp1();
displayp2();
displayp3();
return 0;
}
//OUTPUT
//Q8. QUEUE USING ARRAY
#include <bits/stdc++.h>
using namespace std;
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int frnt = - 1;
int ins()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (frnt == - 1)
frnt = 0;
printf("Insert the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
return 0;
}
int del()
{
if (frnt == - 1 || frnt > rear)
{
printf("Queue Underflow \n");
return 0;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[frnt]);
frnt = frnt + 1;
}
}
int display()
{
int i;
if (frnt == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = frnt; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
return 0;
}
int main()
{
int choice;
while (1)
{
printf("1.insert element to queue \n");
printf("2.delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
ins();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}
}
return 0;
}
//Output
// LINKED LIST OPERATIONS
#include<bits/stdc++.h>
using namespace std;
struct node{
string str;
struct node* next;
};
void push(struct node** head_ref,string s)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->str = s; // put in the data
// link the old list off the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref)= new_node;
}
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
cout<<temp->str<<endl;
temp = temp->next;
}
}
do
{
swapped = 0;
ptr1 = start;
do{
cout<<"Enter what you want to do:\n";
cout<<"1.Insert a new node\n";
cout<<"2.Traverse the list and print the contents\n";
cout<<"3.Count the number of nodes\n";
cout<<"4.Delete an existing node\n";
cout<<"5.Reverse the linked list\n";
cout<<"6.Sorting the words in the linked list\n";
cout<<"7.Combining the two linked lists\n";
cin>>ch;
string s,s1;
switch(ch)
{
case '1': cout<<"Enter the data of the node to be inserted\n";
cin>>s;
push(&head,s);
break;
}
cout<<"Do you want to continue(Y/N)?";
cin>>ch;
}while(ch=='Y'||ch=='y');
return 0;
}
//Output
//Q.10 . Implement stack and queue using array
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* link;
};
void InsertAtBeg(Node** front, int new_data)
{
Node* new_node = new Node;
new_node->data = new_data;
new_node->link = (*front);
(*front) = new_node;
}
void insertAfter(Node* prev_node, int new_data)
{
Node* new_node =new Node;
new_node->data = new_data;
new_node->link = prev_node->link;
prev_node->link = new_node;
}
int DelAtBeg(Node** front)
{
if (*front==NULL)
return INT_MIN;
Node* temp=*front;
*front = (*front)->link;
int popped = temp->data;
free(temp);
return popped;
}
void traverse(Node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->link;
}
}
int main()
{
int data,choice,ch;
cout<<"1.Stack\n2.Queue\n3.Exit\n";
cin>>ch;
while(ch!=3)
{
if(ch==1)
{
Node* top=NULL;
do{
cout<<"\n1.Push Elements into Stack\n2.Pop elements from Stack\n3.Exit\nEnter:";
cin>>choice;
if(choice==1)
{
cout<<"Push:";
cin>>data;
InsertAtBeg(&top,data);
cout<<"Stack is:\n";
traverse(top);
}
else if(choice==2)
{
cout<<DelAtBeg(&top)<<" is Popped\n";
cout<<"Stack is:\n";
traverse(top);
if(top==NULL)
cout<<"Empty\n";
}
cout<<"\n\n";
}while(choice!=3);
}
else if(ch==2)
{
Node* front=NULL;
Node* rear=NULL;
do{
cout<<"\n1.Insert Elements into Queue\n2.Delete elements from Queue\n3.Exit\nEnter:";
cin>>choice;
if(choice==1)
{
cout<<"Insert:";
cin>>data;
if(front==NULL&&rear==NULL)
{
InsertAtBeg(&front,data);
rear=front;
rear->link=NULL;
}
else
insertAfter(rear,data);
cout<<"Queue is:\n";
traverse(front);
if(front==NULL&&rear==NULL)
cout<<"Empty\n";
}
else if(choice==2)
{
cout<<DelAtBeg(&front)<<"is popped\n";
cout<<"Queue is:\n";
traverse(front);
}
cout<<"\n\n";
}while(choice!=3);
} cout<<"1.Stack\n2.Queue\n3.Exit\n";
cin>>ch;
}
}
//Output
//Q.11 Implement n queues and n stacks using singly linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
}*head,*temp,*ptr;
int i,sq,info,n;
void push()
{
printf("Enter data value and the value of stack/queue: ");
scanf("%d%d",&info,&sq);
if(sq>n)
printf("NO SUCH STACK/QUEUE EXISTS!!!\n");
else
{
temp=(struct node*)(malloc(sizeof(struct node)));
temp->data=info;
temp->next=NULL;
if(head==NULL)
start[sq]=top[sq]=head=temp;
else if(start[sq]!=NULL)
{
ptr=top[sq]->next;
top[sq]->next=temp;
temp->next=ptr;
top[sq]=temp;
}
else if(start[sq]==NULL)
{
i=sq-1;
while(i>=1)
{
if(start[i]!=NULL)
break;
i--;
}
ptr=top[i]->next;
start[sq]=top[sq]=top[i]->next=temp;
temp->next=ptr;
}
}
}
void enqueue()
{
printf("Enter data value and the value of stack/queue: ");
scanf("%d%d",&info,&sq);
if(sq>n)
printf("NO SUCH STACK/QUEUE EXISTS!!!\n");
else
{
temp=(struct node*)(malloc(sizeof(struct node)));
temp->data=info;
temp->next=NULL;
if(head==NULL)
front[sq]=rear[sq]=head=temp;
else
{
if(front[sq]!=NULL)
{
ptr=rear[sq]->next;
rear[sq]->next=temp;
temp->next=ptr;
rear[sq]=temp;
}
else if(front[sq]==NULL)
{
front[sq]=rear[sq]=temp;
i=sq-1;
while(i>=1)
{
if(front[i]!=NULL)
break;
i--;
}
if(i!=0)
{
ptr=rear[i]->next;
rear[i]->next=temp;
temp->next=ptr;
}
else if(i==0)
{
for(i=n;i>=1;--i)
if(start[i]!=NULL)
break;
ptr=top[i]->next;
top[i]->next=temp;
temp->next=ptr;
}
}
}
}
}
void pop()
{
printf("Enter the stack no to pop from: ");
scanf("%d",&sq);
if(start[sq]==NULL)
printf("UNDERFLOW!!!\n");
else
{
if(head==top[sq])
{
head=head->next;
start[sq]=top[sq]=NULL;
}
else
{
ptr=head;
while(ptr->next!=top[sq])
ptr=ptr->next;
ptr->next=top[sq]->next;
if(start[sq]==top[sq])
top[sq]=start[sq]=NULL;
else
top[sq]=ptr;
}
}
}
void del()
{
printf("Enter the queue to delete from: ");
scanf("%d",&sq);
if(front[sq]==NULL)
printf("UNDERFLOW!!!\n");
else
{
if(head==front[sq])
{
head=head->next;
if(front[sq]==top[sq])
front[sq]=top[sq]=NULL;
else
front[sq]=head;
}
else
{
ptr=head;
while(ptr->next!=front[sq])
ptr=ptr->next;
ptr->next=front[sq]->next;
if(front[sq]==top[sq])
front[sq]=top[sq]=NULL;
else
front[sq]=ptr->next;
}
}
}
void disp()
{
if(head==NULL)
printf("Everything is empty!!!\n");
else
{
for(i=1;i<=n;++i)
if(start[i]!=NULL)
{
printf("STACK %d : ",i);
ptr=start[i];
while(ptr!=top[i]->next)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
for(i=1;i<=n;++i)
if(front[i]!=NULL)
{
printf("QUEUE %d : ",i);
ptr=front[i];
while(ptr!=rear[i]->next)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
}
}
int main()
{
int ch;
head=NULL;
printf("Enter no. of stacks and queues: ");
scanf("%d",&n);
for(i=0;i<=n;++i)
start[i]=rear[i]=top[i]=front[i]=NULL;
while(1)
{
printf("MENU:\n1.Insert in stack\n2.Insert in Queue\n3.Pop from Stack\n4.Delete from
Queue\n5.Display\nYOUR CHOICE: ");
scanf("%d",&ch);
if(ch==1)
push();
else if(ch==2)
enqueue();
else if(ch==3)
pop();
else if(ch==4)
del();
else if(ch==5)
disp();
else
break;
}
return 0;
}
//Output
//Q. 12 Addition of two polynomials using singly linked list
#include<bits/stdc++.h>
using namespace std;
bool chk[100001];
// Node structure containing power and coefficient of variable
struct node
{
int coeff;
int pow;
struct node *next;
};
//r = r->next;
}
else
{
while(z->next!=NULL){
z=z->next;
}
r =(struct node*)malloc(sizeof(struct node));
r->coeff = x;
r->pow = y;
//r->next = (struct node*)malloc(sizeof(struct node));
z->next = r;
r->next = NULL;
}
}
node *start=NULL;
// Function Adding two polynomial numbers
void polyadd(struct node *poly1, struct node *poly2, struct node *poly)
{
node *temp,*ptr;
int p,c;
poly=NULL;
while(poly1!=NULL)
{
ptr=poly2;
c=poly1->coeff;
p=poly1->pow;
chk[p]=1;
while(ptr!=NULL)
{
if(ptr->pow==poly1->pow)
{
c+=ptr->coeff;
break;
}
ptr=ptr->next;
}
if(start==NULL)
{
start=(struct node*)malloc(sizeof(struct node));
start->pow=p;
start->coeff=c;
poly=start;
start->next=poly->next=NULL;
}
else
{
if(node->next != NULL)
printf(" + ");
node = node->next;
}
}
// Driver program
int main()
{
struct node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
int n,a,b;
cout<<"Enter number of terms of first polynomial\n";
cin>>n;
cout<<"Enter first polynomial coefficients with powers\n";
while(n--){
cin>>a>>b;
cout<<"\n";
cout<<"Enter number of terms of second polynomial\n";
cin>>n;
cout<<"Enter second polynomial coefficients with powers\n";
while(n--){
cin>>a>>b;
return 0;
}
//Output
//Q.13 Multiplication of two polynomials using singly linked list
#include <iostream>
#include <process.h>
using namespace std;
struct node
{
int exp;
int coeff;
node* next;
};
class poly
{
node *top;
public:
poly()
{
top=NULL;
}
int count()
{
int c=0;
node*temp=top;
while(temp!=NULL)
{
c++;
temp=temp->next;
}
return c;
}
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
//cout<<(temp1->name[0]>n[0])<<" "<<(n[0]>temp2->name[0])<<endl;
if(flag)
{
temp1->next=obj;
}
}
//cout<<"Successfully inserted."<<endl;
}
void display()
{
node* temp=top;
if(top!=NULL)
{
cout<<temp->coeff<<"x^"<<temp->exp;
temp=temp->next;
while(temp!=NULL)
{
if(temp->coeff>0)
cout<<"+"<<temp->coeff<<"x^"<<temp->exp;
else if(temp->coeff<0)
cout<<temp->coeff<<"x^"<<temp->exp;
temp=temp->next;
}
cout<<endl;
//cout<<temp->name<<endl;
}
else
{
cout<<"Zero polynomial."<<endl;
}
}
poly mult(poly p)
{
poly res,res1;
node* temp1=this->top;
node* temp2=p.top;
cout<<endl<<"The various steps involved are:"<<endl;
while(temp1!=NULL)
{
temp2=p.top;
res1.top=NULL;
while(temp2!=NULL)
{
res.insert_node(temp1->exp+temp2->exp,temp1->coeff*temp2->coeff);
res1.insert_node(temp1->exp+temp2->exp,temp1->coeff*temp2->coeff);
temp2=temp2->next;
}
res1.display();
temp1=temp1->next;
//res.display();
}
cout<<endl<<"The final resultant polynomial is:"<<endl;
res.display();
}
};
int main()
{
poly p1,p2,p3;
int res,c,d,i,n,m;
cout<<"Enter the highest degree of first polynomial:"<<endl;
cin>>n;
cout<<"Enter degree and coefficients(press enter after each pair):"<<endl;
for(i=0;i<=n;i++)
{cin>>d>>c;
p1.insert_node(d,c);
//p1.display();
}
cout<<"The first polynomial is:"<<endl;
p1.display();
cout<<"Enter the highest degree of second polynomial:"<<endl;
cin>>m;
cout<<"Enter degree and coefficients(press enter after each pair):"<<endl;
for(i=0;i<=m;i++)
{cin>>d>>c;
p2.insert_node(d,c);}
cout<<"The second polynomial is:"<<endl;
p2.display();
p1.mult(p2);
return 0;
}
//Output
//Q. 14 implement singly circular linked list
#include <iostream>
#include <fstream>
#include <process.h>
using namespace std;
struct node
{
int n;
node* next;
};
class sin_cir
{
node *top;
public:
sin_cir()
{
top=NULL;
}
int count()
{
int c=1;
node*temp=top;
if(temp==NULL)
{
return 0;
}
temp=temp->next;
while(temp!=top)
{
c++;
temp=temp->next;
}
return c;
}
node* create(int n)
{
node* temp=new node();
if(temp==NULL)
{
cout<<"List overflow(No memory can be allocated.)"<<endl;
return NULL;
}
temp->n=n;
temp->next=NULL;
return temp;
}
void insert_node(int n)
{
node* obj=create(n);
node* temp1=top;
node* temp2;
int flag=1;
if(temp1==NULL)
{
top=obj;
top->next=top;
}
else
{
if(temp1->n>n)
{
obj->next=top;
temp2=temp1;
while(temp2->next!=top)
{
temp2=temp2->next;
}
temp2->next=obj;
top=obj;
cout<<"Successfully inserted."<<endl;
return;
}
while(temp1->next!=top)
{
temp2=temp1;
temp1=temp1->next;
//cout<<(temp1->name[0]>n[0])<<" "<<(n[0]>temp2->name[0])<<endl;
if((temp1->n>n) && (n>temp2->n))
{
obj->next=temp1;
temp2->next=obj;
flag=0;
//cout<<"Correct position"<<endl;
break;
}
}
if(flag)
{
temp1->next=obj;
obj->next=top;
}
}
cout<<"Successfully inserted."<<endl;
}
void display()
{
node* temp=top;
if(top!=NULL)
{
cout<<"The contents of the list are:"<<endl;
cout<<"CUR\tNEXT\t"<<endl;
cout<<temp->n<<"\t"<<temp->next->n<<endl;
temp=temp->next;
while(temp!=top)
{
cout<<temp->n<<"\t"<<temp->next->n<<endl;
temp=temp->next;
}
//cout<<temp->name<<endl;
}
else
{
cout<<"The list is empty."<<endl;
}
}
int find(int n)
{
node* temp=top;
int flag=0;
if(temp==NULL)
{
return 0;
}
if(top->n==n)
{
return 1;
}
temp=temp->next;
while(temp!=top)
{
if(temp->n==n)
{
flag=1;
return flag;
}
temp=temp->next;
}
return flag;
}
void del(int n)
{
node* temp1=top;
node* temp2;
if(temp1==NULL)
{
cout<<"Queue underflow"<<endl;
return;
}
else if(count()==1)
{
top=NULL;
return;
}
if(temp1->n==n)
{
temp2=top->next;
while(temp2->next!=top)
{
temp2=temp2->next;
}
temp2->next=temp1->next;
top=top->next;
delete temp1;
return;
}
temp2=temp1;
temp1=temp1->next;
while(temp1!=top)
{
if(temp1->n==n)
{
temp2->next=temp1->next;
//cout<<"Yes i have deleted"<<endl;
delete temp1;
return;
}
temp2=temp1;
temp1=temp1->next;
//temp=temp->next;
}
/*if(temp1->next==top && temp1->n==n)
{
}*/
}
void reverse()
{
node *cur,*prev,*next_node,*temp1,*temp2;
if(top==NULL)
return;
temp1=top->next;
temp2=top;
while(temp1->next!=top)
{
temp2=temp1;
temp1=temp1->next;
}
cur=top;
prev=temp1;
//next_node=NULL;
next_node=cur->next;
cur->next=prev;
prev=cur;
cur=next_node;
while(cur!=top)
{
next_node=cur->next;
cur->next=prev;
prev=cur;
cur=next_node;
}
top=prev;
}
};
int main()
{
ifstream myfile ("abc.txt");
sin_cir s1;
int n,res;
int ch;
if (myfile.is_open())
{
while (myfile>>n)
{
s1.insert_node(n);
}
myfile.close();
}
else
cout<<"File not found";
while(1)
{
cout<<endl<<endl<<"MENU"<<endl<<endl;
cout<<"1.Insert"<<endl<<"2.Delete"<<endl<<"3.Display"<<endl<<"4.Search"<<endl;
cout<<"5.Reverse"<<endl<<"6.Count"<<endl<<"0.Exit"<<endl;
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
break;
}
}
return 0;
}
//Output
//Q. 15 implement output restricted deque using singly circular linked list
#include <iostream>
#include <fstream>
#include <process.h>
using namespace std;
struct node
{
int n;
node* next;
};
class deq_cir
{
node *top;
public:
deq_cir()
{
top=NULL;
}
int count()
{
int c=1;
node*temp=top;
if(temp==NULL)
{
return 0;
}
temp=temp->next;
while(temp!=top)
{
c++;
temp=temp->next;
}
return c;
}
node* create(int n)
{
node* temp=new node();
if(temp==NULL)
{
cout<<"List overflow(No memory can be allocated.)"<<endl;
return NULL;
}
temp->n=n;
temp->next=NULL;
return temp;
}
}
else
{
obj->next=top;
}
cout<<"Successfully inserted."<<endl;
return;
}
}
void display()
{
node* temp=top;
if(top!=NULL)
{
cout<<"The contents of the list are:"<<endl;
cout<<"CUR\tNEXT\t"<<endl;
cout<<temp->n<<"\t"<<temp->next->n<<endl;
temp=temp->next;
while(temp!=top)
{
cout<<temp->n<<"\t"<<temp->next->n<<endl;
temp=temp->next;
}
//cout<<temp->name<<endl;
}
else
{
cout<<"The list is empty."<<endl;
}
}
int find(int n)
{
node* temp=top;
int flag=0;
if(temp==NULL)
{
return 0;
}
if(top->n==n)
{
return 1;
}
temp=temp->next;
while(temp!=top)
{
if(temp->n==n)
{
flag=1;
return flag;
}
temp=temp->next;
}
return flag;
}
void del()
{
node* temp1=top;
node* temp2;
if(temp1==NULL)
{
cout<<"Queue underflow"<<endl;
return;
}
else if(count()==1)
{
cout<<"You are deleting the number "<<temp1->n<<endl;
top=NULL;
return;
}
else
{
cout<<"You are deleting the number "<<temp1->n<<endl;
temp2=top->next;
while(temp2->next!=top)
{
temp2=temp2->next;
}
temp2->next=temp1->next;
top=top->next;
delete temp1;
return;
}
}
};
int main()
{
ifstream myfile ("abc.txt");
deq_cir s1;
int n,res;
int ch;
if (myfile.is_open())
{
while (myfile>>n)
{
s1.insert_node(n,1);
}
myfile.close();
}
else
cout<<"File not found";
while(1)
{
cout<<endl<<endl<<"MENU"<<endl<<endl;
cout<<"1.Insert at beginning"<<endl<<"2.Insert at
end"<<endl<<"3.Delete"<<endl<<"4.Display"<<endl<<"5.Search"<<endl<<"0.Exit"<<endl;
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
break;
case 4:
s1.display();
break;
case 5:
cout<<"Enter the number to be searched:";
cin>>n;
res=s1.find(n);
if(res==1)
{
cout<<"Found"<<endl;
}
else
{
cout<<"Not found"<<endl;
}
break;
case 0:
return 0;
default:
cout<<"Enter wrong choice. Please enter a number between 0 and 5."<<endl;
break;
}
}
return 0;
}
//Output
//Q.16 implement doubly linked list with various operations
#include <iostream>
#include <fstream>
using namespace std;
struct node
{
int n;
node* next;
node* prev;
};
class double_ll
{
node *top;
public:
double_ll()
{
top=NULL;
}
int count()
{
int c=0;
node*temp=top;
while(temp!=NULL)
{
c++;
temp=temp->next;
}
return c;
}
node* create(int n)
{
node* temp=new node();
if(temp==NULL)
{
cout<<"List overflow(No memory can be allocated.)"<<endl;
return NULL;
}
temp->n=n;
temp->next=NULL;
temp->prev=NULL;
return temp;
}
void insert_node(int n)
{
node* obj=create(n);
node* temp1=top;
node* temp2;
int flag=1;
if(temp1==NULL)
{
top=obj;
}
else
{
if(temp1->n>n)
{
obj->next=top;
top->prev=obj;
top=obj;
cout<<"Successfully inserted."<<endl;
return;
}
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
//cout<<(temp1->name[0]>n[0])<<" "<<(n[0]>temp2->name[0])<<endl;
if((temp1->n>n) && (n>temp2->n))
{
obj->next=temp1;
temp1->prev=obj;
temp2->next=obj;
obj->prev=temp2;
flag=0;
//cout<<"Correct position"<<endl;
break;
}
}
if(flag)
{
temp1->next=obj;
obj->prev=temp1;
}
}
cout<<"Successfully inserted."<<endl;
}
void display()
{
node* temp=top;
if(top!=NULL)
{
cout<<"The contents of the list are:"<<endl;
cout<<"PREV\tCUR\tNEXT\t"<<endl;
//cout<<temp->prev->n<<"\t"<<temp->n<<"\t"<<temp->next->n<<endl;
while(temp!=NULL)
{
if(temp->prev!=NULL)
cout<<temp->prev->n;
cout<<"\t"<<temp->n<<"\t";
if(temp->next!=NULL)
cout<<temp->next->n<<endl;
cout<<endl;
temp=temp->next;
}
//cout<<temp->name<<endl;
}
else
{
cout<<"The list is empty."<<endl;
}
}
int find(int n)
{
node* temp=top;
int flag=0;
while(temp!=NULL)
{
if(temp->n==n)
{
flag=1;
return flag;
}
temp=temp->next;
}
return flag;
}
void del(int n)
{
node* temp1=top;
node* temp2;
if(temp1->n==n)
{
if(top->next!=NULL)
top->next->prev=NULL;
top=top->next;
delete temp1;
return;
}
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
if(temp1->n==n)
{
temp2->next=temp1->next;
if(temp1->next!=NULL)
{
temp1->next->prev=temp1->prev;
}
delete temp1;
//cout<<"Yes i deleted"<<endl;
return;
}
//temp=temp->next;
}
};
int main()
{
ifstream myfile("abc.txt");
double_ll s1;
int n,res;
int ch;
if (myfile.is_open())
{
while (myfile>>n)
{
s1.insert_node(n);
}
myfile.close();
}
else
cout<<"File not found";
while(1)
{
cout<<endl<<endl<<"MENU"<<endl<<endl;
cout<<"1.Insert"<<endl<<"2.Delete"<<endl<<"3.Display"<<endl<<"4.Search"<<endl<<"0.Exit"<<endl;
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
break;
}
}
return 0;
}
//Ouptut
//Q. 17 implement circular doubly linked list
#include <iostream>
#include <fstream>
using namespace std;
struct node
{
int n;
node* next;
node* prev;
};
class doub_cir
{
node *top;
public:
doub_cir()
{
top=NULL;
}
int count()
{
int c=1;
node*temp=top;
if(temp==NULL)
{
return 0;
}
temp=temp->next;
while(temp!=top)
{
c++;
temp=temp->next;
}
return c;
}
node* create(int n)
{
node* temp=new node();
if(temp==NULL)
{
cout<<"List overflow(No memory can be allocated.)"<<endl;
return NULL;
}
temp->n=n;
temp->next=NULL;
temp->prev=NULL;
return temp;
}
void insert_node(int n)
{
node* obj=create(n);
node* temp1=top;
node* temp2;
int flag=1;
if(temp1==NULL)
{
top=obj;
top->next=top;
top->prev=top;
}
else
{
if(temp1->n>n)
{
//cout<<top->prev->next->n<<endl;
//cout<<top->prev->n<<endl;
//cout<<top->n<<endl;
top->prev->next=obj;
obj->prev=top->prev;
obj->next=top;
top->prev=obj;
/*temp2=temp1;
while(temp2->next!=top)
{
temp2=temp2->next;
}
temp2->next=obj;*/
top=obj;
cout<<"Successfully inserted."<<endl;
return;
}
while(temp1->next!=top)
{
temp2=temp1;
temp1=temp1->next;
//cout<<(temp1->name[0]>n[0])<<" "<<(n[0]>temp2->name[0])<<endl;
if((temp1->n>n) && (n>temp2->n))
{
obj->next=temp1;
temp1->prev=obj;
temp2->next=obj;
obj->prev=temp2;
flag=0;
//cout<<"Correct position"<<endl;
break;
}
}
if(flag)
{
temp1->next=obj;
obj->next=top;
top->prev=obj;
obj->prev=temp1;
}
}
cout<<"Successfully inserted."<<endl;
}
void display()
{
node* temp=top;
if(top!=NULL)
{
cout<<"The contents of the list are:"<<endl;
cout<<"PREV\tCUR\tNEXT\t"<<endl;
cout<<temp->prev->n<<"\t"<<temp->n<<"\t"<<temp->next->n<<endl;
temp=temp->next;
while(temp!=top)
{
cout<<temp->prev->n<<"\t"<<temp->n<<"\t"<<temp->next->n<<endl;
temp=temp->next;
}
//cout<<temp->name<<endl;
}
else
{
cout<<"The list is empty."<<endl;
}
}
int find(int n)
{
node* temp=top;
int flag=0;
if(temp==NULL)
{
return 0;
}
if(top->n==n)
{
return 1;
}
temp=temp->next;
while(temp!=top)
{
if(temp->n==n)
{
flag=1;
return flag;
}
temp=temp->next;
}
return flag;
}
void del(int n)
{
node* temp1=top;
node* temp2;
if(temp1==NULL)
{
cout<<"Queue underflow"<<endl;
return;
}
else if(count()==1)
{
top=NULL;
return;
}
if(temp1->n==n)
{
temp2=top->prev;
/*while(temp2->next!=top)
{
temp2=temp2->next;
}*/
temp2->next=temp1->next;
temp1->next->prev=temp2;
top=top->next;
delete temp1;
return;
}
temp2=temp1;
temp1=temp1->next;
while(temp1!=top)
{
if(temp1->n==n)
{
temp2->next=temp1->next;
temp1->next->prev=temp2;
//cout<<"Yes i have deleted"<<endl;
delete temp1;
return;
}
temp2=temp1;
temp1=temp1->next;
//temp=temp->next;
}
/*if(temp1->next==top && temp1->n==n)
{
}*/
}
};
int main()
{
ifstream myfile ("abc.txt");
doub_cir s1;
int n,res;
int ch;
if (myfile.is_open())
{
while (myfile>>n)
{
s1.insert_node(n);
}
myfile.close();
}
else
cout<<"File not found";
while(1)
{
cout<<endl<<endl<<"MENU"<<endl<<endl;
cout<<"1.Insert"<<endl<<"2.Delete"<<endl<<"3.Display"<<endl<<"4.Search"<<endl<<"0.Exit"<<endl;
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
break;
}
}
return 0;
}
//Ouptut
//Q. 18 implement binary search tree
#include <iostream>
#include <fstream>
using namespace std;
struct node
{
int n;
node* left;
node* right;
};
class tree
{
node* root;
public:
tree()
{
root=NULL;
}
node* allocate_node(int n)
{
node* temp=new node;
temp->n=n;
temp->left=NULL;
temp->right=NULL;
return temp;
}
void create(int n)
{
root=allocate_node(n);
}
}
else if (n<r->n)
r->left= insert(r->left, n);
else if(n>r->n)
r->right=insert(r->right,n);
return r;
}
}
if(r->n==n)
{
return r;
}
else if (n<r->n)
return search(r->left, n);
else if(n>r->n)
return search(r->right,n);
void preorder(node* r)
{
if(r==NULL)
return;
cout<<r->n<<"\t";
preorder(r->left);
preorder(r->right);
}
bool istree()
{
if(root==NULL)
return false;
else
return true;
}
void insert_node(int n)
{
node *temp=insert(root,n);
}
node* getroot()
{
return root;
}
int main()
{
ifstream myfile ("tree.txt");
tree t;
int n,ch;
if (myfile.is_open())
{
if(myfile>>n)
t.create(n);
while (myfile>>n)
{
t.insert_node(n);
}
myfile.close();
}
else
cout<<"File not found";
while(1)
{
cout<<endl<<"MENU"<<endl;
cout<<"1.Insert"<<endl<<"2.Display"<<endl<<"3.Delete"<<endl<<"4.Search"<<endl<<"5.Create"<<endl<
<"0.Exit"<<endl;
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 5:
if(t.istree()==false)
{
cout<<"Enter the number:";
cin>>n;
t.create(n);
cout<<"Successfully created"<<endl;
}
else
{
cout<<"The tree already exists. Please choose a valid option."<<endl;
}
break;
case 1:
if (t.istree()==false)
{
cout<<"Please create the tree first."<<endl;
}
else
{
cout<<"Enter the number:";
cin>>n;
t.insert_node(n);
cout<<"Successfully inserted"<<endl;
}
break;
case 2:
if (t.istree()==false)
{
cout<<"The tree doesn't exist"<<endl;
}
else
{
cout<<"The preorder traversal of the nodes is as follows:"<<endl;
t.preorder(t.getroot());
cout<<endl;
}
break;
case 3:
if (t.istree()==false)
{
cout<<"Please create the tree first."<<endl;
}
else
{
cout<<"Enter the number:";
cin>>n;
node* res=t.search(t.getroot(),n);
if(res==NULL)
cout<<"The element does not exist in tree"<<endl;
else
{
node *temp=t.del(t.getroot(),n);
cout<<"Deleted"<<endl;
}
break;
case 4:
if (t.istree()==false)
{
cout<<"Please create the tree first."<<endl;
}
else
{
cout<<"Enter the number:";
cin>>n;
node* res=t.search(t.getroot(),n);
if(res==NULL)
cout<<"Not found"<<endl;
else
cout<<"Found"<<endl;
}
case 0:
return 0;
default:
cout<<"Please enter a valid option."<<endl;
}
}
return 0;
}
//Ouptut
//Q. 19 a)compare two binary trees; b) copy one into another; c)create mirror tree
#include <iostream>
#include <fstream>
#include<string.h>
using namespace std;
struct node
{
int n;
node* left;
node* right;
};
class tree
{
node* root;
int preIndex;
public:
tree()
{
root=NULL;
preIndex = 0;
}
node* allocate_node(int n)
{
node* temp=new node;
temp->n=n;
temp->left=NULL;
temp->right=NULL;
return temp;
}
void create(int n)
{
root=allocate_node(n);
}
}
else if (n<r->n)
r->left= insert(r->left, n);
else if(n>r->n)
r->right=insert(r->right,n);
return r;
}
node* search(node* r, int n)
{
if (r==NULL)
{
return NULL;
}
if(r->n==n)
{
return r;
}
else if (n<r->n)
return search(r->left, n);
else if(n>r->n)
return search(r->right,n);
bool istree()
{
if(root==NULL)
return false;
else
return true;
}
void insert_node(int n)
{
node *temp=insert(root,n);
}
node* getroot()
{
return root;
}
void mirror(node* r)
{
if (r==NULL)
return;
else
{
node* temp;
mirror(r->left);
mirror(r->right);
temp = r->left;
r->left = r->right;
r->right = temp;
}
}
node* copyTree(node* r)
{
node* temp = NULL;
if (r)
{
temp = new node();
temp->n = r->n;
temp->left = copyTree(r->left);
temp->right = copyTree(r->right);
}
return temp;
}
void copy(node* r)
{
this->root=this->emptytree(this->getroot());
this->root=copyTree(r);
int main()
{
tree t1,t2;
int n,ch,num,c;
char in[10],pre[10];
int indexin=0,indexpre=0,index;
ifstream file,file1;
file.open("input1.txt");
if (file.is_open())
{
file>>num;
//cout<<n;
index=0;
while (file>>c)
{
if(index++<num)
in[indexin++]=c;
else
pre[indexpre++]=c;;
}
file.close();
}
else
cout<<"file not found";
if(indexin!=indexpre)
{
cout<<"Tree cannot be formed. Exiting.";
return 0;
}
for(index=0;index<indexpre;index++);
t1.tree_create(in, pre, 0, index - 1);
cout<<endl<<"The preorder traversal of tree 1 is:"<<endl;
t1.preorder(t1.getroot());
cout<<endl;
file.open("input2.txt");
strcpy(in,"\0");
strcpy(pre,"\0");
indexin=0;indexpre=0;
if (file.is_open())
{
file>>num;
//cout<<n;
index=0;
while (file>>c)
{
if(index++<num)
in[indexin++]=c;
else
pre[indexpre++]=c;;
}
file.close();
}
else
cout<<"file not found";
if(indexin!=indexpre)
{
cout<<"Tree cannot be formed. Exiting.";
return 0;
}
for(index=0;index<indexpre;index++);
//t2.preorder(t2.getroot());
t2.copy(t1.getroot());
cout<<endl<<"The copied binary tree is:";
t2.preorder(t2.getroot());
}
else if(ch==3)
{
bool res=t1.compare(t1.getroot(),t2.getroot());
if(res==true)
{
cout<<"The two trees are equal."<<endl;
}
else
{
cout<<"The two trees are unequal"<<endl;
}
}
else if(ch==1)
{
t1.mirror(t1.getroot());
cout<<"The mirror of the tree has the following preorder traversal:"<<endl;
t1.preorder(t1.getroot());
cout<<endl;
}
else if (ch==0)
return 0;
else
cout<<"Please enter a valid option."<<endl;
}
return 0;
}
//output
//Q. 20 BFS and DFS traversal
#include<stdio.h>
#include<conio.h>
#define size 20
int a[10][10],vertex[10],n,e;
/*STACK FUNCTIONS*/
#define bottom -1
int stack[size],top=bottom;
int stackempty()
{
return(top=bottom) ? 1:0;
}
int stackfull()
{
return(top==size-1) ? 1:0;
}
int pop()
{
if(stackempty())
{
printf("\n STACK IS EMPTY");
return -1;
}
else
return stack[top--];
}
int peep()
{
if(stackempty())
{
printf("\n STACK IS EMPTY");
return -1;
}
else
return stack[top];
}
/* QUEUE FUNCTIONS */
#define start -1
int q[size];
int f=start,r=start;
void addq(int c)
{
if(qfull())
printf("\nQUEUE IS FULL");
else
q[++r]=c;
}
int delq()
{
if(qempty())
{
printf("\n QUEUE IS EMPTY");
return -1;
}
else
return q[++f];
}
// j is unvisited adjecent vertex to i
int adjvertex(int i)
{
int j;
for(j=0;j<n;j++)
if(a[i][j]==1&&vertex[j]==0)
return j;
return n;
}
int visitall()
{
int i;
for(i=0;i<n;i++)
if(vertex[i]==0)
return 0;
return 1;
}
/*MAIN PROGRAM*/
int main()
{
int i,j,k;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
a[i][j]=0;
dfs();
bfs();
getch();
}
//Output
//Q. 21 implement sorting algorithm
#include<iostream>
#include<vector>
int orig[100],n,b[100];
vector<int>v[10];
void radix();
void bubble();
void selection();
void insertion();
void countsort();
void mergesort(int l,int mid,int r);
void part(int l,int r);
void disp(int a[]);
int menu(int);
int main()
{
int flag=1;
//int n;
cout<<"Enter no of elements :";
cin>>n;
cout<<"Enter array elements :"<<endl;
for(int i=0;i<n;i++)
{
cin>>orig[i];
}
while(flag)
flag=menu(flag);
return 0;
}
case 2: {
radix();
//disp();
break;
}
case 3: {
insertion();
//disp();
break;
}
case 4: {
selection();
//disp();
break;
}
case 5:
{
for(int i=0;i<n;i++)
{
b[i]=orig[i];
}
part(0,n-1);
disp(b);
break;
}
case 6: {
countsort();
break;
}
case 0: flag=0;
break;
default:
cout<<"Please enter valid choice\n";
}
return flag;
}
void radix()
{
int i,a[100],temp[n];
for(i=0;i<n;i++)
{
a[i]=orig[i];
}
int x=1,cnt=0;
while(1)
{
for(int i=0;i<n;i++)
v[(a[i]/x)%10].push_back(i);
int k=0;
for(int i=0;i<10;i++)
{
vector<int>::iterator it;
for(it=v[i].begin();it<v[i].end();it++)
temp[k++]=a[*it];
}
for(int i=0;i<n;i++)
a[i]=temp[i];
cnt=0;
for(int i=0;i<10;i++)
v[i].clear();
x*=10;
for(int i=0;i<n;i++)
if(a[i]/x==0)
cnt++;
if(cnt==n)
break;
}
disp(a);
}
void bubble()
{
int i,j,t,a[n];
for(i=0;i<n;i++)
{
a[i]=orig[i];
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j+1]<a[j])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
}
disp(a);
}
void selection()
{
int a[n],i,j,k=0,cnt2=0;
for(i=0;i<n;i++)
{
a[i]=orig[i];
}
for(k=0;k<n-1;k++)
{
int min=INT_MAX;
for(i=k;i<n;i++)
{
if(a[i]<min)
{min=a[i];
j=i;}
}
int t=a[j];
a[j]=a[k];
a[k]=t;
}
disp(a);
}
void insertion()
{
int i,j,arr[100];
arr[0]=INT_MIN;
for(i=0;i<n;i++)
arr[i+1]=orig[i];
for(i=2;i<=n;i++)
{
int temp=arr[i];
j=i-1;
while(arr[j]>temp)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
disp(arr+1);
}
void countsort()
{
int d[100]={0};
for(int i=0;i<n;i++)
{
d[orig[i]]++;
}
for(int i=0;i<100;i++)
{
if(d[i]!=0)
{
while(d[i]>0)
{
cout<<i<<" ";
d[i]--;
}
}
}
cout<<endl;
}
while(x<=mid&&y<=r)
{
if(b[x]<b[y])
tmp[k++]=b[x++];
if(b[x]>b[y])
tmp[k++]=b[y++];
if(b[x]==b[y])
{
tmp[k++]=b[x++];
y++;
}
}
while(x<=mid)
tmp[k++]=b[x++];
while(y<=r)
tmp[k++]=b[y++];
for(int i=0;i<k;i++)
{
b[l+i]=tmp[i];
}
}