Ds All Assignment
Ds All Assignment
P a g e 1 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
RECURSION
1.Factorial of a number
#include<stdio.h>
int fact(int n)
{
if(n>0)
{
return(n*fact(n-1));
}
else
{
return(1);
}
}
void main()
{
int f;
f=fact(5);
printf("Factorial of 5: %d\n",f);
}
Output:
2.Fibonacci Series
#include<stdio.h>
int fib(int n)
{
if(n==1||n==2)
{
return(1);
}
else if(n==0)
{
return(0);
}
return(fib(n-1)+fib(n-2));
P a g e 2 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
void main()
{
int i;
for ( i = 0; i <10; i++)
{
printf(" %d",fib(i));
}
printf("\n");
}
Output:
3.GCD of 2 numbers
#include<stdio.h>
int gcd(int a,int b)
{
if (b==0)
{
return a;
}
if (a==0)
{
return b;
}
return gcd(b,a%b);
}
void main()
{
int gcdre;
gcdre=gcd(10,15);
printf("GCD of 10 and 25 is %d ",gcdre);
Output:
P a g e 3 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
4.Tower of Hanoi
#include<stdio.h>
void TOH(int n,char beg,char aux,char end)
{
if(n>=1)
{
TOH(n-1,beg,end,aux);
printf("%c to %c\n",beg,end);
TOH(n-1,aux,beg,end);
}
}
void main()
{
TOH(3,'A','B','C');
}
Output:
5.Ackerman
#include<stdio.h>
int A(int m,int n);
int A(int m,int n)
{
if(m==0)
{
return n+1;
}
else if(n==0)
{
return A(m-1,1);
}
else
{
return A(m-1,A(m,n-1));
}
}
P a g e 4 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
void main()
{
printf("Magical No=%d",A(1,0));
printf(“\n”);}
Output:
STACK
1.Array implementation
#include<stdio.h>
int stack[20],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main()
{
printf("Enter the elements in the stack");
scanf("%d",&n);
printf("Stack operations using array");
printf("\n---------------------------\n");
while(choice!=4)
{
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit\n");
printf(“\nEnter the choice: ”);
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
printf("Exiting....");
break;
default:
P a g e 5 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
}
}
void push()
{
int val;
if(top==n)
{
printf("\n Overflow");
}
else
{
printf("\nEnter the value:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("Underflow");
}
else
{
printf("popped item %d",stack[top]);
top=top-1;
}
}
void show()
{
for(i=top;i>=0;i--)
{
printf("\nstack is:%d ",stack[i]);
}
if(top==-1)
{
printf("stack is empty");
P a g e 6 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
}
Output:
P a g e 7 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *next;
}*new,*top,*ptr;
void push()
{
int data;
new=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data: ");
scanf("%d",&data);
new->value=data;
P a g e 8 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
new->next=NULL;
if(top==NULL)
{
top=new;
}
else{
new->next=top;
top=new;
}
}
void pop()
{
if(top->next==NULL)
{
printf("Stack Empty");
}
else{
ptr=top;
printf("Deleted item:%d",top->value);
printf("\n");
top=top->next;
ptr->next=NULL;
free(ptr);
}
}
void display()
{
ptr=top;
if(top==NULL)
{
printf("Stack is Empty");
}
else{
}
int main()
{
int c;
while(1)
{
printf("1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT");
printf("\nEnter choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choice");
}
}
return 0;
}
Output:
P a g e 10 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
P a g e 11 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
3.Infix to Postfix
#include<stdio.h>
void pushopr(char);
char popopr();
char peepopr();
void displayopr();
void pushout(char);
void displayout();
int getpriority(char);
char opr[25]={'\0'};
char out[25]={'\0'};
int topopr=-1;
int topout=-1;
char ch;
void main()
{
char infix[25]={'\0'},ele,popele;
int i;
printf("\n Enter infix Expression:-");
scanf("%s",&infix);
printf("\n infix Expresion=%s",infix);
i=0;
while(infix[i]!='\0')
{
ele=infix[i];
if(ele=='(')
{
pushopr(ele);
}
else if(ele==')')
{
while (peepopr()!='(')
{
popele=popopr();
pushout(popele);
}
popopr();
}
else if(ele=='^'|| ele=='*'||ele=='/'||ele=='+'||ele=='-')
{
P a g e 12 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
if(topopr>=0)
{
while(getpriority(peepopr())>=getpriority(ele) && topopr!=-1)
{
popele=popopr();
pushout(popele);
}
}
pushopr(ele);
}
else{
pushout(ele);
}
displayopr();
displayout();
i++;
}
if(topopr!=-1)
{
while(topopr!=-1)
{
popele=popopr();
pushout(popele);
}
}
printf("\nPostfix expression %s\n",out);
}
P a g e 13 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
else{
topopr++;
opr[topopr]=ele;
}
}
char popopr()
{
if(topopr!=-1)
{
ch=opr[topopr];
topopr--;
}
return ch;
}
char peepopr()
{
if(topopr!=1)
{
ch=opr[topopr];
}
return ch;
}
void displayopr()
{
int i;
printf("\nOperator stack=");
for(i=0;i<=topopr;i++)
{
printf("| %c",opr[i]);
}
}
void pushout(char ele)
{
if(topout==24)
P a g e 14 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
{
printf("\n Output stack is full");
}
else{
topout++;
out[topout]=ele;
}
}
void displayout()
{
int i;
printf("Output stack is full");
for(i=0;i<=topout;i++)
{
printf("| %c",out[i]);
}
}
Output:
int i,l;
//clrscr();
printf("Insert a postfix notation :: ");
gets(post);
l=strlen(post);
for(i=0;i<l;i++)
{
if(post[i]>='0' && post[i]<='9')
}
if(post[i]=='+' || post[i]=='-' || post[i]=='*' ||
post[i]=='/' || post[i]=='^')
{
evaluate(post[i]);
}
}
printf("\n\nResult :: %d",stack[top]);
getch();
}
void evaluate(char c)
{
int a,b,ans;
a=stack[top];
stack[top]='\0';
top--;
b=stack[top];
stack[top]='\0';
top--;
switch(c)
{
case '+':
ans=b+a;
break;
case '-':
ans=b-a;
break;
case '*':
ans=b*a;
break;
P a g e 16 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
case '/':
ans=b/a;
break;
case '^':
ans=b^a;
break;
default:
ans=0;
}
top++;
stack[top]=ans;
}
Output:
5.Decimal to Binary
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int arr[20],top=-1;
int push(int x)
{
if(top==19)
{
printf("Stack overflow");
return -1;
}
top++;
arr[top]=x;
return 1;
}
int pop()
{
int x;
if(top==-1)
P a g e 17 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
{
printf("Stack Underflows");
return -1;
}
x=arr[top];
top--;
return x;
}
void DecimaltoBinary(int d)
{
top=-1;
int r,x;
printf("\nDecimal Equivalent of %d is:=> ",d);
while(d>0)
{
r=d%2;
push(r);
d=d/2;
}
while(top!=-1)
{
x=pop();
printf("%d",x);
}
printf("\n");
}
int main()
{
top=-1;
int decimal;
printf("Enter Decimal No: ");
scanf("%d",&decimal);
DecimaltoBinary(decimal);
return 0;
}
Output:
P a g e 18 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
6.String Reverse
#include<stdio.h>
#include<string.h>
char stack[50];
int top=-1;
void push(char c)
{
top=top+1;
stack[top]=c;
}
void pop()
{
char c;
c=stack[top];
top=top-1;
printf("%c",c);
}
int main()
{
char str[30];
int i,len;
printf("Enter string\n");
scanf("%s",str);
len=strlen(str);
for(i=0;i<len;i++)
{
push(str[i]);
}
printf("\nreverse of a string is:");
for(i=0;i<len;i++)
{
pop();
}
return 0;
}
Output:
P a g e 19 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
QUEUE
1.Array Implementation
#include<stdio.h>
#define size 10
int front=-1,rear=-1;
int q[size];
void dequeue()
{
int x;
if(front==-1 && rear==-1)
{
printf("Queue is empty");
}
else if(front==rear)
{
printf("\nDeleted item: %d",q[front]);
front++;
}
x=q[front];
front=front+1;
printf("\nDeleted item: %d",x);
}
void display()
{
int i;
printf("\n\nQueue is ");
for(i=front;i<=rear;i++)
{
printf("%d ",q[i]);
}
printf("\n");
}
void enqueue(int x)
{
if(rear==size-1 )
{
printf("Queue is full");
}
if(front=-1)
{
front=front+1;
}
rear=rear+1;
P a g e 20 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
q[rear]=x;
printf(“\nEnqueued element is:%d”,q[rear]);
}
int main()
{
int front=-1,rear=-1;
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
dequeue();
dequeue();
display();
return 0;
}
Output:
void enqueue()
{
int data;
new=(struct node *)malloc(sizeof(struct node));
printf("\nEnter Data: ");
scanf("%d",&data);
new->data=data;
new->next=NULL;
if(front==NULL && rear ==NULL)
P a g e 21 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
{
front=new;
rear=new;
}
else{
rear->next=new;
rear=new;
}
}
void dequeue()
{
temp=front;
if(front==NULL && rear ==NULL)
{
printf("Queue is empty");
}
else
{
printf("Deleted element is: %d",front->data);
front=front->next;
temp->next=NULL;
free(temp);
}
}
void display()
{
P a g e 22 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
int main()
{
int c;
while(1)
{
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit");
printf("\nEnter choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choice");
}
}
}
Output:
P a g e 23 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
3.Circular Queue
#include<stdio.h>
# define size 5
int queue[size];
int rear=-1;
int front=-1;
void enqueue()
{
int item;
if((front==0 && rear==size-1)||(front==rear+1))
{
printf("Queue Overflow");
}
else
{
rear=(rear+1)%size;
printf("\nEnter an item");
P a g e 24 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
scanf("%d",&item);
queue[rear]=item;
if(front==-1)
{
front=0;
}
}
}
void dequeue()
{
int x;
if(front==-1)
{
printf("\nQueue Underflows");
}
else if(rear==front)
{
printf("\n Deleted element is %d",queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\n Deleted element is %d",queue[front]);
front=(front+1)%size;
}
}
void display()
{
int i;
if(front==-1)
{
printf("\nQueue Underflows");
}
else
{
for(i=front;i!=rear;i=(i+1)%size)
{
printf("%d\t",queue[i]);
}
printf("%d\t",queue[i]);
}
}
P a g e 25 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
int main()
{
int choice;
while(1)
{
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit");
printf("\nEnter choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong choice");
}
}
return 0;
}
Output:
P a g e 26 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
}
void dequeue()
{
}
}
}
void enqueue_f()
{
int data;
if(f==0)
{
printf("Queue is full");
}
else
{
printf("Enter data: ");
scanf("%d",&data);
if(f==-1 && r==-1)
{
f++;
r++;
queue[f]=data;
P a g e 28 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
else{
f--;
queue[f]=data;
}
}
}
void dequeue_r()
{
if(f==-1 && r==-1)
{
printf("Queue is underflow");
}
else
{
if(f==r)
{
printf("\nDeleted item: %d",queue[r]);
f=-1;
r=-1;
}
else
{
printf("\nDeleted item: %d",queue[r]);
r--;
}
}
}
void display()
{
int i;
if(f==-1 && r==-1)
{
printf("Queue is underflow");
}
else{
printf("Status of Queue: ");
for(i=f;i<=r;i++)
{
printf("%d\t",queue[i]);
}
}
}
P a g e 29 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
int main()
{
int c;
while(1)
{
printf("\n1.Enqueue from rear\t2.Dequeue from front\t3.Enqueue from front\t4.Dequeue
from rear\t5.Display\t6.Exit");
printf("\nEnter choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
enqueue_f();
break;
case 4:
dequeue_r();
break;
case 5:
display();
break;
case 6:
exit(0);
default:
printf("Failed");
}
}
return 0;
}
Output:
P a g e 30 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
5.Priority Queue
#include<stdio.h>
#include<stdlib.h>
#define size 5
int PQ[size];
int f=-1,r=-1;
void enqueue_pri();
void dequeue_pri();
void check(int);
void display();
int main()
{
int c;
while(1)
{
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4Exit");
printf("\nEnter choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
enqueue_pri();
break;
case 2:
dequeue_pri();
break;
case 3:
display();
break;
case 4:
P a g e 31 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
exit(0);
default:
printf("Failed");
}
}
return 0;
void enqueue_pri()
{
int no;
if(r==(size-1))
{
printf("Queue overflow");
}
else{
printf("Enter data: ");
scanf("%d",&no);
if(r==-1 && f==-1)
{
f++;
r++;
PQ[r]=no;
}
else{
check(no);
r++;
}
}
}
P a g e 32 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
PQ[i]=data;
return;
}
PQ[i]=data;
}
}
void dequeue_pri()
{
if(f==-1 && r==-1)
{
printf("Queue is underflow");
}
else{
printf("Dequeued element: %d",PQ[f]);
if(f==r)
{
f=-1;
r=-1;
}
else{
f++;
}
}
}
void display()
{
int i;
if(f==-1 && r==-1)
{
printf("Queue is underflow");
}
else{
printf("Status of Queue: ");
for(i=f;i<=r;i++)
{
printf("%d\t",PQ[i]);
}
}
}
P a g e 33 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
Output:
Linked List
1.Singly Linked List
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
}*head,*temp,*new,*d;
void create()
{
int value;
char ch;
do
{
new=(struct node*)malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d",&value);
new->data=value;
new->next=NULL;
P a g e 34 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
if(head==NULL)
{
head=new;
temp=new;
}
else{
temp->next=new;
temp=temp->next;
}
printf("Do you want to add one more node to the list[y/n]");
fflush(stdin);
scanf("%c",&ch);
} while (ch=='y');
}
void insertbegin()
{
int value;
new=(struct node*)malloc(sizeof(struct node));
printf("\nEnter value");
scanf("%d",&value);
new->data=value;
new->next=head;
head=new;
}
void insertend()
{
temp=head;
int value;
new=(struct node*)malloc(sizeof(struct node));
printf("\nEnter value");
scanf("%d",&value);
new->data=value;
new->next=NULL;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
}
void insertat()
{
int pos,no;
temp=head;
printf("\nEnter the position: ");
P a g e 35 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
scanf("%d",&pos);
printf("Enter the element");
scanf("%d",&no);
for(int i=0;i<pos-1;i++)
{
temp=temp->next;
}
new=(struct node*)malloc(sizeof(struct node));
new->data=no;
new->next=temp->next;
temp->next=new;
}
void deletebegin()
{
temp=head;
printf("Deleted node is %d ",temp->data);
head=head->next;
temp->next=NULL;
free(temp);
}
void deletelast()
{
temp=head;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
d=temp->next;
printf("Deleted node is %d ",d->data);
temp->next=NULL;
free(d);
}
void deleteat()
{
int pos;
temp=head;
printf("\nEnter the position: ");
scanf("%d",&pos);
for(int i=0;i<pos-1;i++)
{
temp=temp->next;
}
P a g e 36 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
d=temp->next;
printf("Deleted node is %d ",d->data);
temp->next=d->next;
d->next=NULL;
free(d);
}
void search()
{
int key;
int flag=0;
printf("Enter key Element: ");
scanf("%d",&key);
temp=head;
while(temp!=NULL)
{
if(key==temp->data)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\nKey is found");
}
else{
printf("key element is not found");
}
}
void reverse()
{
struct node *current,*nextnode;
struct node *prev=NULL;
current=nextnode=head;
while(nextnode!=NULL)
{
nextnode=nextnode->next;
current->next=prev;
prev=current;
current=nextnode;
}
P a g e 37 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
head=prev;
printf("\nReversed ");
}
void display()
{
temp=head;
printf("List is: ");
while(temp!=NULL)
{
printf(" %d",temp->data);
temp=temp->next;
}
}
int main()
{
int c;
while(1)
{
printf("\n\n1.Create\n2.Insert at begin\n3.Insert at end\n4.insert at
position\n5.Display\n6.Delete from begin\n7.Delete from end\n8.Delete at
position\n9.Search\n10.Reverse List\n11.Exit");
printf("\nEnter choice:");
scanf("%d",&c);
switch (c)
{
case 1:
create();
break;
case 2:
insertbegin();
break;
case 3:
insertend();
break;
case 4:
insertat();
break;
case 5:
display();
break;
case 6:
deletebegin();
break;
case 7:
deletelast();
P a g e 38 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
break;
case 8:
deleteat();
break;
case 9:
search();
break;
case 10:
reverse();
display();
break;
case 11:
exit(0);
break;
default:printf("Wrong choice");
}
}
return 0;
}
Output:
P a g e 39 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}*new1,*head1,*temp1,*new2,*head2,*temp2;
struct node* create()
{
int value;
char ch;
do
{
new1=(struct node*)malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d",&value);
new1->data=value;
new1->next=NULL;
if(head1==NULL)
{
head1=new1;
temp1=new1;
}
else{
temp1->next=new1;
temp1=temp1->next;
}
printf("Do you want to add one more node to the list[y/n]");
fflush(stdin);
scanf("%c",&ch);
} while (ch=='y');
return head1;
}
int value;
char ch;
do
{
P a g e 40 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
nptr->next=head2;
}
void display(struct node* head1,struct node* head2)
{
temp1=head1;
printf("List is: ");
while(temp1!=NULL)
{
printf(" %d",temp1->data);
temp1=temp1->next;
}
}
P a g e 41 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
int main()
{
struct node *headmain1,*headmain2;
int choice;
while(1)
{
printf("\n1.create linked list1.\n2.create linked list 2\n 3.conacat\n4.display\n5.exit");
printf("\nEnter choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
headmain1=create();
break;
case 2:
headmain2=create2();
break;
case 3:
conacat(headmain1,headmain2);
break;
case 4:
display(headmain1,headmain2);
break;
case 5:
exit(0);
return 0;
}
Output:
P a g e 42 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
4.Polynomial Addition
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int coef;
int expo;
struct node *next;
}*new1,*head1,*p1,*new2,*head2,*p2;
void createpoly1()
{
char ch;
printf("\nEnter first polynomial: ");
do
{
new1=(struct node *)malloc(sizeof(struct node));
printf("\nEnter Coefficient");
scanf("%d",&new1->coef);
printf("Enter exponent: ");
scanf("%d",&new1->expo);
new1->next=NULL;
if(head1==NULL)
{
P a g e 43 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
head1=new1;
p1=new1;
}
else
{
p1->next=new1;
p1=new1;
}
printf("Do you want to add one more term to the poly[y/n] ");
fflush(stdin);
scanf("%c",&ch);
} while (ch=='y');
}
void createpoly2()
{
char ch;
printf("\nEnter second polynomial: ");
do
{
new2=(struct node *)malloc(sizeof(struct node));
printf("\nEnter Coefficient");
scanf("%d",&new2->coef);
printf("Enter exponent: ");
scanf("%d",&new2->expo);
new2->next=NULL;
if(head2==NULL)
{
head2=new2;
p2=new2;
}
else
{
p2->next=new2;
p2=new2;
}
printf("Do you want to add one more term to the poly[y/n] ");
fflush(stdin);
scanf("%c",&ch);
} while (ch=='y');
}
void display1()
P a g e 44 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
{
p1=head1;
printf("\n1st polynomial is: ");
while(p1!=NULL)
{
printf("%dx^%d",p1->coef,p1->expo);
p1=p1->next;
if(p1!=NULL)
{
printf(" + ");
}
}
}
void display2()
{
p2=head2;
printf("\n2nd polynomial is: ");
while(p2!=NULL)
{
printf("%dx^%d",p2->coef,p2->expo);
p2=p2->next;
if(p2!=NULL)
{
printf(" + ");
}
}
}
void addition()
{
p1=head1;
p2=head2;
while(p1!=NULL && p2!=NULL)
{
if(p1->expo==p2->expo)
{
printf("%dx^%d",p1->coef+p2->coef,p1->expo);
p1=p1->next;
p2=p2->next;
}
else if(p1->expo>p2->expo)
{
printf("%dx^%d",p1->coef,p1->expo);
p1=p1->next;
}
P a g e 45 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
else{
printf("%dx^%d",p2->coef,p2->coef);
p2=p2->next;
}
if(p1!=NULL||p2!=NULL)
{
printf(" + ");
}
}
while(p1!=NULL)
{
printf("%dx^%d",p1->coef,p1->expo);
p1=p1->next;
if(p1!=NULL)
{
printf(" + ");
}
}
while(p2!=NULL)
{
printf("%dx^%d",p2->coef,p2->expo);
p2=p2->next;
if(p2!=NULL)
{
printf(" + ");
}
}
}
int main()
{
int choice;
while(1)
{
printf("\n1.create polynomial1\n2.Create polynomial2\n3.Display polynomial1\n4.Display
polynomial2\n5.Add polynomial\n6.Exit");
printf("\nEnter Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
createpoly1();
break;
case 2:
createpoly2();
P a g e 46 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
break;
case 3:
display1();
break;
case 4:
display2();
break;
case 5:
addition();
break;
case 6:
exit(0);
break;
default:
printf("\nWrong choice");
}
}
return 0;
}
Output:
P a g e 47 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
5.Polynomial Evaluation
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int coef;
int expo;
struct node *next;
}*new1,*head1,*p1;
void createpoly1()
{
char ch;
printf("\nEnter first polynomial: ");
do
{
new1=(struct node *)malloc(sizeof(struct node));
printf("\nEnter Coefficient");
scanf("%d",&new1->coef);
printf("Enter exponent: ");
scanf("%d",&new1->expo);
new1->next=NULL;
if(head1==NULL)
{
head1=new1;
p1=new1;
}
else
{
p1->next=new1;
p1=new1;
}
printf("Do you want to add one more term to the poly[y/n] ");
fflush(stdin);
scanf("%c",&ch);
} while (ch=='y');
void display1()
{
p1=head1;
P a g e 48 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
void evaluation1()
{
int eval,v;
int sum=0;
p1=head1;
printf("\nEnter variable value: ");
scanf("%d",&v);
while(p1!=NULL)
{
eval=sum + p1->coef * pow(v,p1->expo);
sum=eval;
p1=p1->next;
}
printf("\nEvaluation is: %d",eval);
int main()
{
int choice;
printf("\n1.create polynomial\t2.Display polynomial1\t3.Evaluate polynomial1\t4.Exit");
while(1)
{
printf("\nEnter Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
createpoly1();
break;
P a g e 49 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
case 2:
display1();
break;
case 3:
evaluation1();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong choice");
}
}
return 0;
}
Output:
P a g e 50 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}*new,*temp,*head,*d;
void create()
{
int value;
char ch;
do
{
new =(struct node*)malloc(sizeof(struct node));
new->prev=NULL;
printf("\nEnter a data");
scanf("%d",&value);
new->data=value;
new->next=NULL;
if(head==NULL)
{
head=new;
temp=new;
}
else
{
temp->next=new;
new->prev=temp;
temp=new;
}
printf("Do you want to add one more node to the list(y/n)");
fflush(stdin);
scanf("%c",&ch);
} while(ch=='y');
void display()
{
temp=head;
P a g e 51 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("\n");
}
void insertbegin()
{
int value;
new =(struct node*)malloc(sizeof(struct node));
new->prev=NULL;
printf("\nEnter a value: ");
scanf("%d",&value);
new->data=value;
new->next=head;
head->prev=new;
head=new;
}
void insertend()
{
temp=head;
int value;
while(temp->next!=NULL)
{
temp=temp->next;
}
new =(struct node*)malloc(sizeof(struct node));
new->prev=temp;
temp->next=new;
printf("\nEnter a value: ");
scanf("%d",&value);
new->data=value;
new->next=NULL;
}
void insertat()
{
int i,pos,value;
temp=head;
printf("Enter position: ");
scanf("%d",&pos);
for(i=0;i<pos-1;i++)
{
P a g e 52 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
temp=temp->next;
}
new =(struct node*)malloc(sizeof(struct node));
printf("\nEnter a value: ");
scanf("%d",&value);
new->data=value;
new->next=temp->next;
temp->next->prev=new;
temp->next=new;
new->prev=temp;
}
void deletebegin()
{
temp=head;
printf("Deleted node is %d",head->data);
head=head->next;
head->prev=NULL;
// temp->next=NULL;
free(temp);
}
void deleteend()
{
temp=head;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
d=temp->next;
printf("Deleted node is %d :",d->data);
temp->next=NULL;
d->prev=NULL;
free(d);
}
void deleteat()
{
int i,pos;
temp=head;
printf("\nEnter position: ");
scanf("%d",&pos);
for(i=0;i<pos-1;i++)
{
temp=temp->next;
}
d=temp->next;
printf("Deleted node is %d",d->data);
P a g e 53 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
temp->next=d->next;
d->next->prev=temp;
d->prev=NULL;
d->next=NULL;
free(d);
}
int main()
{
int choice;
printf("1.Create node\t2.insert at begin\t3.insert at end\t4.insert at position\t5.Delete from
begin\t6.Delete from end\t7.Delete from position\t8.Display\t9.Exit");
while(1)
{
printf("\nEnter choice: \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
insertbegin();
break;
case 3:
insertend();
break;
case 4:
insertat();
break;
case 5:
deletebegin();
break;
case 6:
deleteend();
break;
case 7:
deleteat();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
P a g e 54 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
default:
printf("\nwrong choice");
}
}
return 0;
}
Output:
void create()
{
int value;
char ch;
do
{
P a g e 55 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
void display()
{
temp=head;
while(temp->next!=head)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d->",temp->data);
}
void insertbegin()
{
int value;
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
new=(struct node *)malloc(sizeof(struct node));
temp->next=new;
printf("\nEnter a value: ");
scanf("%d",&value);
new->data=value;
P a g e 56 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
new->next=head;
head=new;
}
void insertend()
{
int value;
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
new=(struct node *)malloc(sizeof(struct node));
temp->next=new;
printf("\nEnter a value: ");
scanf("%d",&value);
new->data=value;
new->next=head;
}
void insertat()
{
temp=head;
int i,pos,value;
printf("\nEnter position: ");
scanf("%d",&pos);
for(i=0;i<pos-1;i++)
{
temp=temp->next;
}
new=(struct node *)malloc(sizeof(struct node));
printf("\nEnter a value: ");
scanf("%d",&value);
new->data=value;
new->next=temp->next;
temp->next=new;
}
void deletebegin()
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
d=head;
P a g e 57 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
while(1)
{
printf("\nEnter choice: \n");
scanf("%d",&choice);
P a g e 58 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
switch(choice)
{
case 1:
create();
break;
case 2:
insertbegin();
break;
case 3:
insertend();
break;
case 4:
insertat();
break;
case 5:
deletebegin();
break;
case 6:
deleteend();
break;
case 7:
deleteat();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("\nwrong choice");
}
}
return 0;
P a g e 59 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
Output:
TREE
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left,*right;
};
return 0;
}
else{
new->data=value;
printf("Enter left child of %d",value);
new->left=create();
printf("Enter right child of %d",value);
new->right=create();
}
return new;
}
P a g e 61 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
int main()
{
struct node *root;
root=0;
root=create();
printf("\nPreorder is: ");
preorder(root);
printf("\nInorder is: ");
Inorder(root);
printf("\nPostorder is: ");
postorder(root);
return 0;
}
Output:
P a g e 62 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
#include <stdio.h>
#include <stdlib.h>
#define max 20
struct node
{
char data;
struct node *left;
struct node *right;
};
struct node *stack[max];
struct node *createNode(int data)
{
struct node *root = (struct node *)malloc(sizeof(struct node));
root->data = data;
root->left = NULL;
root->right = NULL;
return root;
}
void inorder(struct node *root)
{
int top = -1;
struct node *cur = root;
while (top > -1 || cur != NULL)
{
if (cur != NULL)
{
stack[++top] = cur;
cur = cur->left;
}
else
{
cur = stack[top--];
printf("%c ", cur->data);
cur = cur->right;
}
}
}
void preorder(struct node *root)
{
int top =
P a g e 63 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
-1;
struct node *cur = root;
stack[++top] = root;
while (top > -1)
{
cur = stack[top--];
printf("%c ", cur->data);
if (cur->right != NULL)
{
stack[++top] = cur->right;
}
if (cur->left != NULL)
{
stack[++top] = cur->left;
}
}
}
void postorder(struct node *root)
{
int top = -1;
struct node *cur = root;
do
{
while (cur != NULL)
{
if (cur->right != NULL)
{
stack[++top] = cur->right;
}
stack[++top] = cur;
cur = cur->left;
}
cur = stack[top--];
if (cur->right != NULL && stack[top] == cur->right)
{
--top;
stack[++top] = cur;
cur = cur->right;
}
else
{
printf("%c ", cur->data);
P a g e 64 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
cur = NULL;
}
} while (top != -1);
}
int main()
{
struct node *root = createNode('A');
root->left = createNode('B');
root->right = createNode('C');
root->left->left = createNode('D');
root->left->right = createNode('E');
/*
D
/ \
B C
/\
D E
*/
printf("Inorder traversal : ");
inorder(root);
printf("\nPreorder traversal : ");
preorder(root);
printf("\nPostorder traversal : ");
postorder(root);
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
if(root==NULL)
{
root=create(d);
}
else
{
new=root;
if(d==0)
{
return root;
break;
}
while(1)
{
if(d<new->data)
{
if(new->left==NULL)
{
new->left=create(d);
break;
}
else
{
new=new->left;
}
P a g e 66 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
else
{
if(new->right==NULL)
{
new->right=create(d);
break;
}
else
{
new=new->right;
}
}
}
}}
}
void Inorder(struct node *root)
{
if(root!=NULL)
{
Inorder(root->left);
printf(" %d",root->data);
Inorder(root->right);
}
}
int main()
{
int choice,value,key;
char ch;
struct node *p,*min,*max,*r;
do
{
printf("\n1.create tree\n2.Inorder traversal\n3.search\n4.Find Minimum item\n5.find maximum
item");
printf("\nEnter choice: ");
scanf("%d",&value);
switch(value)
P a g e 68 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
{
case 1:
p=createtree(root);
break;
case 2:
printf("\nInorder Traversal is: ");
Inorder(p);
break;
case 3:
printf("\nEnter element to search: ");
scanf("%d",&key);
r=search(p,key);
if(r==NULL)
{
printf("\nkey not found");
}
break;
case 4:
min=find_min(p);
printf("\nMinimum item is %d",min->data);
break;
case 5:
max=find_max(p);
printf("\nMaximum item is %d",max->data);
break;
default:
printf("wrong choice");
}
printf("\nDo you want to continue?[y/n]");
scanf(" %c",&ch);
} while(ch=='y');
return 0;
}
Output:
P a g e 69 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
P a g e 70 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
GRAPH
#include<stdio.h>
#include<stdlib.h>
#define V 100
struct node
{
int data;
struct node *next;
};
struct node *adjList[V];
void init()
{
int i;
for(i=0;i<V;i++)
{
adjList[i]=NULL;
}
}
void addEdge(int src,int dest)
{
struct node*newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=dest;
newnode->next=NULL;
if(adjList[src]==NULL)
{
adjList[src]=newnode;
}
else
{
struct node* list=adjList[src];
while(list->next!=NULL)
{
list=list->next;
}
list->next=newnode;
}
}
void printGraph()
{
int i=0;
P a g e 71 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
la:
while(i<V)
{
struct node*temp=adjList[i];
if(temp==NULL)
{
i++;
goto la;
}
else
{
printf("adjList[(%d)]->",i);
while(temp!=NULL)
{
printf(" %d -> ",temp->data);
temp=temp->next;
}
printf("NULL\n\n");
}
i++;
}
}
void bfs(struct node*adjList[],int start,int visited[])
{
int queue[V],front=0,rear=-1;
struct node* ptr;
queue[++rear]=start;
//front++;
printf("BSF traversal\n");
while(front<=rear)
{
start=queue[front++];
ptr=adjList[start];
visited[start]=1;
printf(" %d\t",start);
while(ptr!=NULL)
{
if(visited[ptr->data]!=1)
{
queue[++rear]=ptr->data;
visited[ptr->data]=1;
}
ptr=ptr->next;
P a g e 72 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
}
}
int main()
{ int src,dest,data,n,visited[V],choice=0;
printf("REPRESENTING GRAPH USING ADJENCY LIST AND PERFORM BFS.....\n");
init();
printf("Enter the number of entries you make while making graph :");
scanf("%d",&n);
while(choice<n)
{
printf("Enter the two adjacent vertice\n");
scanf("%d%d",&src,&dest);
addEdge(src,dest);
addEdge(dest,src);
choice++;
}
printGraph();
printf("bfs traversal\n");
printf("Enter the vertex which you are going to start\n");
scanf("%d",&data);
for(int i=0;i<V;++i)
{
visited[i]=0;
}
printf("The BFS traversal from %d is ",data);
bfs(adjList,data,visited);
return 0;
}
Output:
P a g e 73 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
#include<stdio.h>
void bfs(int v)
{
int i;
visited[v] = 1;
for(i=1;i<=n;i++)
if(G[v][i] && !visited[i])
q[++rear]=i;
if(front <= rear)
bfs(q[front++]);
}
int main()
{
int v,i,j;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&G[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The nodes which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n %d is not reachable",i);
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
#define V 100
struct node
{
int data;
struct node *next;
};
struct node* adjList[V];
void init()
{
P a g e 75 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
int i;
for(i=0;i<V;i++)
{
adjList[i]=NULL;
}
}
void addEdge(int src,int dest)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=dest;
newnode->next=NULL;
if(adjList[src]==NULL)
{
adjList[src]=newnode;
}
else
{
struct node *list=adjList[src];
while(list->next!=NULL)
{
list=list->next;
}
list->next=newnode;
}
}
void printGraph()
{
int i=0;
la:
while(i<V)
{
struct node *temp=adjList[i];
if(temp==NULL)
{
i++;
goto la;
}
else
{
printf(" adjList[%d]=",i);
while(temp!=NULL)
{
printf(" %d -> ",temp->data);
temp=temp->next;
P a g e 76 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
}
printf("NULL\n\n");
}
i++;
}
}
void dfs(struct node *adjList[],int start,int visited[])
{
visited[start]=1;
printf("%d\t",start);
struct node*ptr=adjList[start];
while(ptr!=NULL)
{
if(visited[ptr->data]!=1)
{
dfs(adjList,ptr->data,visited);
}
ptr=ptr->next;
}
}
int main()
{
int n,src,dest,start,visited[V],choice=0;
init();
printf("//*......REPRESENTING GRAPH USING ADJENCY LIST AND PERFORM
DFS.....*//\n");
printf("Enter the number of entries you make for graph\n");
scanf("%d",&n);
while(choice<n)
{
printf("Enter the two adjacent vertices\n");
scanf("%d%d",&src,&dest);
addEdge(src,dest);
addEdge(dest,src);
choice++;
}
printGraph();
printf("DFS TRAVERSAL\n");
printf("\nEnter the node you want to start the DFS traversal\n");
scanf("%d",&start);
for(int i=0;i<V;i++)
{
P a g e 77 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
visited[i]=0;
}
printf("DFS traversal from %d\n",start);
dfs(adjList,start,visited);
}
Output:
#include <stdio.h>
#include <stdlib.h>
/* ADJACENCY MATRIX */
int source,V,E,time,visited[20],G[20][20];
void DFS(int i)
{
int j;
visited[i]=1;
printf(" %d->",i+1);
for(j=0;j<V;j++)
{
if(G[i][j]==1&&visited[j]==0)
DFS(j);
}
}
int main()
{
P a g e 78 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
int i,j,v1,v2;
printf("\t\t\tGraphs\n");
printf("Enter the no of edges:");
scanf("%d",&E);
printf("Enter the no of vertices:");
scanf("%d",&V);
for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
G[i][j]=0;
}
for(i=0;i<E;i++)
{
printf("Enter the edges (format: V1 V2) : ");
scanf("%d%d",&v1,&v2);
G[v1-1][v2-1]=1;
for(i=0;i<V;i++)
{
for(j=0;j<V;j++)
printf(" %d ",G[i][j]);
printf("\n");
}
printf("Enter the source: ");
scanf("%d",&source);
DFS(source-1);
return 0;
}
Output:
P a g e 79 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
SEARCHING
1.Linear Search
#include<stdio.h>
int main()
{
int arr[6]={4,7,6,2,1,8};
int key,flag=0,i;
printf("\nEnter element to search: ");
scanf("%d",&key);
for( i=0;i<6;i++)
{
if(key==arr[i])
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\n%d is found at index %d\n",key,i);
}
else
{
printf("\n%d is not found\n",key);
}
return 0;
}
Output:
P a g e 80 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
2.Binary Search
#include<stdio.h>
int main()
{
int arr[6]={1,2,3,4,5,6};
int mid,key,i,start=0,end=5;
printf("\nEnter element to search: ");
scanf("%d",&key);
while(start<=end)
{
mid=(start+end)/2;
if(arr[mid]==key)
{
printf("%d is found",arr[mid]);
break;
}
else if(arr[mid]<key)
{
start=mid+1;
}
else
{
end=mid-1;
}
}
return 0;
}
Output:
P a g e 81 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
SORTING
1.Bubble Sort
#include<stdio.h>
int main()
{
int arr[5]={7,5,8,2,1};
int temp;
for(int i=0;i<5;i++)
{
for(int j=0;j<5-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("Sorted array is: ");
for(int i=0;i<5;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
return 0;
}
Output:
P a g e 82 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
2.Selection Sort
#include<stdio.h>
void selection(int arr[],int n)
{
int i,j,t,m,p;
for(i=0;i<n;i++)
{
m=i;
for(j=i+1;j<n;j++)
{
if(arr[m]>arr[j])
{
m=j;
}
}
t=arr[i];
arr[i]=arr[m];
arr[m]=t;
}
}
int main()
{
int arr[6]={4,7,5,1,2,8};
selection(arr,6);
printf("Sorted array: ");
for(int i=0;i<6;i++)
{
printf("%d ",arr[i]);
}
return 0;
Output:
P a g e 83 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
3.Insertion Sort
#include<stdio.h>
arr[j+1]=t;
}
}
void display(int arr[],int n)
{
printf("Sorted Array: ");
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}
int main()
{
int arr[5]={8,4,3,2,1};
insertion(arr,5);
display(arr,5);
return 0;
}
Output:
P a g e 84 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
4.Merge Sort
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
P a g e 85 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main()
{
int arr[] = { 4,7,9,8,3 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
}
Output:
5.Shell Sort
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr[]={5,3,9,6,8,7};
int n=sizeof(arr)/sizeof(arr[0]);
printf("\nArray after sorting: \n");
shellsort(arr,6);
printarray(arr,6);
P a g e 87 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
printf("\n");
return 0;
}
Output:
6.Quick Sort
#include<stdio.h>
int partition(int arr[],int s,int e)
{
int pivot=arr[s];
int i,j,temp;
i=s;
j=e;
while(i<j)
{
while(arr[i]<=pivot)
{
i++;
}
while(arr[j]>pivot)
{
j--;
}
if (i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
temp=arr[s];
arr[s]=arr[j];
arr[j]=temp;
return j;
}
P a g e 88 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
if(s<=e)
{
int p=partition(arr,s,e);
quicksort(arr,s,p-1);
quicksort(arr,p+1,e);
}
int main()
{
int arr[5]={5,2,9,8,7};
quicksort(arr,0,5);
printf("\nSorted array");
for(int i=0;i<5;i++)
{
printf(" %d",arr[i]);
}
printf("\n")
}
Output:
7.Heap Sort
#include<stdio.h>
P a g e 89 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
// Print an array
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}
// Driver code
int main() {
int arr[] = {6,3,2,7,1,9};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
Output:
P a g e 90 | 91
Name:Snehal Satappa Mhasavekar PRN:22620009
P a g e 91 | 91