DS File-Anmol (It-3)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 88

RD

DS FILE – 3 SEMESTER

ANMOL GANDHI
IT-3
01276803120
INDEX

No. Date Experiment


1. 1.9.21 Write a program to insert an element at kth location in an array
2. 1.9.21 Write a program to delete an element from kth location and print
the
deleted value also
3. 1.9.21 Program to implement Linear Searching
4. 1.9.21 Program to implement Binary Searching
5. 1.9.21 Make a program to implement Bubble Sort
6 1.9.21 Program to check whether the input matrix is sparse matrix or not
7. 1.9.21 Write a program to implement matrix multiplication
8. 15.9.21 Program to implement push, pop and traversal operations on a
stack
9. 15.9.21 Program to implement insertion, deletion and traversal operations
on a linear queue
10. 15.9.21 Program to implement insertion, deletion and traversal operations
on a circular queue
11. 15.9.21 Make a program to implement insertion, deletion and traversal
operations on a linked list
12. 15.9.21 Build a program to implement insert, deletion and traversal
operations on a circular linked list
13. 22.9.21 Make a program to implement stack using linked list
14. 22.9.21 Make a program to implement queue using linked list
15. 6.10.21 Construct a program implementing preorder, inorder and
postorder traversals of a binary tree recursively
16. 6.10.21 Write a program to implement Heap Sort
17. 12.10.21 Write a program to implement Quick Sort
18. 19.10.21 Write a program to implement Selection Sort
19. 19.10.21 Write a program to implement Insertion Sort
20. 27.10.21 Write a program to implement Merge Sort
QUES 1. Write a program to insert an element at kth location in an
array.
CODE :
#define MAX 10
#include<stdio.h>
#include<conio.h>

void Insert(int[],int,int,int);

int main()
{

int A[MAX],N,k,item;
printf("Enter the number of elements of 1-D array:\n");
scanf("%d",&N);
for(int i=1;i<=N;i++)
{
printf("Enter the element at A[%d]:\n",i);
scanf("%d",&A[i]);
}

for(int j=1;j<=N;j++)
{
printf("%d ",A[j]);
}
printf("\nEnter the element which is to be inserted in 1-D array:\n");
scanf("%d",&item);
printf("Enter the position at which insertion is to be done:\n");
scanf("%d",&k);
Insert(A,N,k,item);
return 0;
}

void Insert(int A[],int N,int K,int D)


{
for(int j=N;j>=K;j--)
{
A[j+1]=A[j];
}
A[K]=D;
N++;

for(int i=1;i<=N;i++)
{
printf("%d ",A[i]);
}
}
OUTPUT:

QUES 2. Write a program to delete an element from kth location


and print the deleted value also.

CODE:
#define MAX 10
#include<stdio.h>
#include<conio.h>

void Delete(int[],int,int);

int main()
{
int A[MAX],N,k,item;
printf("Enter the number of elements of 1-D array:\n");
scanf("%d",&N);
for(int i=1;i<=N;i++)
{
printf("Enter the element at A[%d]:\n",i);
scanf("%d",&A[i]);
}

for(int j=1;j<=N;j++)
{
printf("%d ",A[j]);
}
printf("\nEnter the position from which deletion is to be done:\n");
scanf("%d",&k);
Delete(A,N,k);
return 0;
}

void Delete(int A[],int N,int K)


{
for(int j=K;j<N;j++)
{
A[j]=A[j+1];
}
N--;
for(int i=1;i<=N;i++)
{
printf("%d ",A[i]);
}
}

OUTPUT:

QUES 3. Write a program to implement Linear Searching.

CODE:
#define MAX 10
#include<stdio.h>
#include<conio.h>

int Lin_Search(int [], int );

int main()
{
int A[MAX],N;
int pos;
printf("Enter the number of elements of 1-D array\n");
scanf("%d",&N);
for(int i=0;i<N;i++)
{
printf("Enter the element A[%d]:",i);
scanf("%d",&A[i]);
}
pos=Lin_Search(A,N);
if(pos==-1)
printf("Element not found \\\n");
else
printf("Element found at position %d\n" ,pos+1);
getch();

int Lin_Search(int A[],int N)


{
int data,loc=-1;
printf("Enter the element that is to be searched\n");
scanf("%d",&data);
for(int k=0;k<N;k++)
{
if(A[k]==data)
loc=k;
}

return loc;
}

OUTPUT:
QUES 4. Write a program to implement Binary Searching.

CODE:
#include<iostream>
using namespace std;

int binary_search(int arr[],int n,int key){


//Implement binary search
int s = 0;
int e = n - 1;

while(s<=e){
int mid = (s+e)/2;
if(arr[mid] == key){
return mid;
}
else if(arr[mid] > key){
e = mid - 1;
}
else{

s = mid + 1;
}
}

return -1;
}

int main(){

int arr[1000] ;
int n;
printf("Enter the number of elements of 1-D array::\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the element B[%d]:",i);
scanf("%d",&arr[i]);
}
int key;
printf("Enter the data to be searched:");
scanf("%d",&key);
printf("\n");

int index = binary_search(arr,n,key);


if(index!=-1){
printf("%d found at location :%d", key, index+1);
}
else{
printf("Not found! %d element isn't present in the list", key);
}

return 0;
}

OUTPUT:
QUES 5. Write a program to implement Bubble Sort.
CODE:
#include<stdio.h>
#include<conio.h>

#define MAX 10

void BSORT(int[],int);

int main ()
{

int Arr[MAX],N;
printf("Enter the number of elements of 1-D array\n");
scanf("%d",&N);
for(int i=0;i<N;i++)
{
printf("Enter the element Arr[%d]:",i);
scanf("%d",&Arr[i]);
}

BSORT(Arr,N);
printf("Elements after bubble sorting :\n");
for( int k=0; k<N ; k++)
{
printf("%d ",Arr[k]);
}
return 0;
}

void BSORT(int A[], int n)


{
int temp;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(A[j] > A[j+1])
{
temp=A[j];
A[j]= A[j+1];
A[j+1]=temp;
}
}
}
}

OUTPUT:
QUES 6. Write a program to check whether the input matrix is
sparse matrix or not.

CODE:
#include<stdio.h>
#include<conio.h>

void Check_Sparse(int[10][10],int,int);
int main ()
{

int Arr[10][10],M,N;
printf("Enter the number of rows and columns of 1-D array\n");
scanf("%d\n%d",&M,&N);
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
printf("Enter the element Arr[%d][%d]:",i,j);
scanf("%d",&Arr[i][j]);
}
}

for(int k=0;k<M;k++)
{
for(int l=0;l<N;l++)
{
printf("%d ",Arr[k][l]);
}
printf("\n");
}
Check_Sparse(Arr,M,N);
return 0;
}

void Check_Sparse(int A[][10], int R, int C)


{
int sparse_ctr=0;
int total=R*C;
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
if(A[i][j] == 0 )
{
++sparse_ctr;
}
}

if( sparse_ctr > (total - sparse_ctr) )


{
printf("The given matrix is a sparse-matrix\n");
}
else
printf("The given matrix is not a sparse-matrix\n");

printf("%d",sparse_ctr);

OUTPUT:
QUES 7. Program to implement matrix multiplication.

CODE:
#define MAX 10
#include<stdio.h>
#include<conio.h>

void Multiply(int [][MAX],int[][MAX],int[][MAX],int,int);

int main()
{

int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX],R,C,i,j;
printf("Enter the number of rows and columns for 2-D array \n");
scanf("%d\n%d",&R,&C);
printf("--------FIRST ARRAY----------\n");
for(i=0;i<R;i++)
{
for(j=0;j<C;j++)
{
printf("Enter element -> A[%d][%d]:",i,j);
scanf("%d",&A[i][j]); //inputting the array elements
}
}
printf("\n");
printf("-------SECOND ARRAY----------\n");
for(i=0;i<R;i++)
{
for(j=0;j<C;j++)
{
printf("Enter element -> B[%d][%d]:",i,j);
scanf("%d",&B[i][j]); //inputting the array elements
}
}

Multiply(A,B,C,R,C);
getch();
}

void Multiply(int Arr1[][MAX],int Arr2[][MAX],int Arr3[][MAX],int M,int N)


{
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
Arr3[i][j] = Arr1[i][j] * Arr2[i][j]; //multiplying element by element
}
}
printf("The resultant array obtained by multiplication of A and B is : \n");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
printf("%d ",Arr3[i][j]);
}
printf("\n");
}
getch();
}

OUTPUT:
QUES 8. Implementation of some operations on a stack
 Push
 Pop
 Traversal

CODE:
#include<stdio.h>
#include<conio.h>

void push();
void pop();
void display();
int stack[100],n,i,x;
int top=-1; // first element of Stack

int main()
{

printf("Enter the size of STACK\n");


scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t ----------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT\n");
int choice;
do
{
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : push(); break;
case 2 : pop(); break;
case 3 : display(); break;
case 4 : printf("Exit..\n");

default : printf("Enter a valid choice 1/2/3/4\n");


}
}while(choice!=4);
}

void push()
{
if(top==n-1)
{
printf("Overflow...STACK is FULL\n");
}
else
{
printf("Enter a value to pe pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}

void pop()
{
if(top==-1)
{
printf("Underflow....STACK is empty\n");
}
else
{
printf("The popped element is %d\n",stack[top]);
top--;
}
}

void display()
{
if(top>=0)
{
printf("The elements are as follows:\n");
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
printf("Enter another choice\n");
}
else
{
printf("Stack is empty.....\n");
}
}
OUTPUT:

QUES 9. : Implementation of some operations on a linear queue


 Insertion
 Deletion
 Traversing
CODE:
#include<stdio.h>
#include<conio.h>

void insert();
void delete();
void display();
int queue[100],n,i,x;
int front=-1,rear=-1; // first and last elements of Queue respectively

void main()
{
printf("Enter the size of QUEUE\n");
scanf("%d",&n);
printf("\n\t QUEUE OPERATIONS USING ARRAY");
printf("\n\t ----------------------------");
printf("\n\t 1.INSERT\n\t 2.DELETE\n\t 3.DISPLAY\n\t 4.EXIT\n");
int choice;
do
{
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : insert(); break;
case 2 : delete(); break;
case 3 : display(); break;
case 4 : printf("Exit..\n");

default : printf("Enter a valid choice 1/2/3/4\n");


}
}while(choice!=4);
}

void insert()
{
if(rear==n-1)
{
printf("Overflow...Queue is FULL\n");
}
else if ( rear == -1 && front == -1)
{
front=rear=0; // first insert
printf("Enter a value to pe inserted:");
scanf("%d",&x);
queue[rear]=x;
}
else
{
rear++;
printf("Enter a value to pe inserted:");
scanf("%d",&x);
queue[rear]=x;

}
}

void delete()
{
if(front==-1 && rear==-1)
{
printf("Underflow...Queue is empty\n");
}
else if( front == rear)
{
front=rear=-1;
printf("The deleted element is %d\n",queue[front]);
}
else
{
front++;
printf("The deleted element is %d\n",queue[front]);
}
}

void display()
{
if(rear>=0)
{
printf("The elements are as follows:\n");
for(i=front;i<=rear;i++) // FIFO
{
printf("%d\n",queue[i]);
}
printf("Enter another choice\n");
}
else
{
printf("Queue is empty.....\n");
}
}

OUTPUT:
QUES 10. Implementation of operations on a circular queue
 Insertion
 Deletion
 Traversing

CODE:
#include<stdio.h>
#include<conio.h>

int insert();
int delete();
int display();
int queue[100],n,i,x;
int front=-1,rear=-1; // first and last elements of Queue respectively

int main()
{
printf("Enter the size of CIRCULAR QUEUE\n");
scanf("%d",&n);
printf("\n\t QUEUE OPERATIONS USING ARRAY");
printf("\n\t ----------------------------");
printf("\n\t 1.INSERT\n\t 2.DELETE\n\t 3.DISPLAY\n\t 4.EXIT\n");
int choice;
do
{
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : insert(); break;
case 2 : delete(); break;
case 3 : display(); break;
case 4 : printf("Exit..\n");

default : printf("Enter a valid choice 1/2/3/4\n");


}
}while(choice!=4);
}

int insert()
{
if(front==(rear+1)%n)
{
printf("Overflow...Queue is FULL\n");
}
else if (front== -1 && rear == -1)
{
front=rear=0; // first insert
printf("Enter a value to pe inserted:");
scanf("%d",&x);
queue[rear]=x;
}
else
{
rear=(rear+1)%n;
printf("Enter a value to pe inserted:");
scanf("%d",&x);
queue[rear]=x;

}
}

int delete()
{
if(front==-1)
{
printf("Underflow...Queue is empty\n");
}
else if( front == rear)
{
front=rear=-1;
printf("The deleted element is %d\n",queue[front]);
}
else
{
printf("The deleted element is %d\n",queue[front]);
front=(front+1)%n;
}
}

int display()
{
if(front==-1 && rear==-1)
{
printf("Underflow...No queue inserted\n");
}
else
{
for(int i=front;;i=i+1%n)
{
printf("%d\n",queue[i]);
if(i==rear)
break;
}
printf("%d\n",queue[i]);
}
return 0;
}

OUTPUT:
QUES 11. Implement the following operations on a linked list ->
 Insert a node at beginning
 Insert a node at end
 Delete a node from beginning
 Delete a node from end
 Traversing through the list

CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};

struct node * start=NULL;

void insert_at_begin(int);
void insert_at_end(int);
void traverse();
void delete_from_begin();
void delete_from_end();
int count=0;
int main()
{
int i,data;
for(;;)
{
printf("1.Insertion at begin\n");
printf("2.Insertion at end\n");
printf("3.Deletion from begin\n");
printf("4.Deletion from end\n");
printf("5.Traversing thorugh the linked list\n");
printf("6.Exit\n");
scanf("%d",&i);

if(i==1)
{
scanf("%d",&data);
insert_at_begin(data);
}

else if(i==2)
{
scanf("%d",&data);
insert_at_end(data);
}

else if(i==3)
delete_from_begin();

else if(i==4)
delete_from_end();

else if( i==5 )


traverse();

else if(i==6)
break;

else
printf("Enter valid input\n");
}

return 0;
}

void insert_at_begin(int x)
{
struct node* t;
t=(struct node*)malloc(sizeof(struct node));
t-> data = x;
count++;

if(start==NULL)
{
start=t;
start->next= NULL;
return;
}

else
{
t -> next = start;
start=t;
}
}

void insert_at_end ( int x )


{
struct node *t,*temp;
t=(struct node*) malloc(sizeof(struct node));
t->data=x;
count++;

if(start==NULL)
{
start=t;
start->next=NULL;
return;
}

temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
temp->next=t;
t->next=NULL;
}

void delete_from_begin()
{
struct node*t;
int n;
if(start==NULL)
{
printf("Linked list is empty...\n");
return;
}

n= start->data;
t=start->next;
free(start);
start=t;
count--;

printf("%d deleted from beginning of linked list\n",n);


}

void delete_from_end()
{
struct node *t,*u;
int n;
if(start==NULL)
{
printf("Linked list is empty...\n");
return;
}
count--;

if(start->next==NULL)
{
n=start->data;
free(start);
start=NULL;
printf("%d deleted successfully\n",n);
}

t=start;

while(t->next!=NULL)
{
u=t;
t=t->next;
}

n=t->data;
u->next=NULL;
free(t);
printf("%d deleted successfully\n",n);
}

void traverse()
{
struct node *t;
t=start;
if ( t==NULL)
{
printf("Empty linked list...nothing to search\n");
return;

}
printf("%d nodes are present in the linked list\n",count);

while(t-> next !=NULL)


{
printf("%d\n", t->data);
t=t->next;
t->next=NULL;
}
}
OUTPUT:
QUES 12. Implement the given operations on a circular linked list ->
 Insert a node at beginning
 Insert a node at end
 Delete a node from beginning
 Delete a node from end
 Traversing through the list
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};

struct node * start=NULL;

void insert_at_begin(int);
void insert_at_end(int);
void traverse();
void delete_from_begin();
void delete_from_end();
int count=0;
int main()
{
int i,data;
for(;;)
{
printf("1.Insertion at begin\n");
printf("2.Insertion at end\n");
printf("3.Deletion from begin\n");
printf("4.Deletion from end\n");
printf("5.Traversing thorugh the linked list\n");
printf("6.Exit\n");
scanf("%d",&i);

if(i==1)
{
scanf("%d",&data);
insert_at_begin(data);
}

else if(i==2)
{
scanf("%d",&data);
insert_at_end(data);
}

else if(i==3)
delete_from_begin();

else if(i==4)
delete_from_end();

else if( i==5 )


traverse();

else if(i==6)
break;

else
printf("Enter valid input\n");
}

return 0;
}

void insert_at_begin(int x)
{
struct node* t;
t=(struct node*)malloc(sizeof(struct node));
t-> data = x;
count++;

if(start==NULL)
{
start=t;
start->next= NULL;
return;
}

else
{
t -> next = start;
start=t;
}
}

void insert_at_end ( int x )


{
struct node *t,*temp;
t=(struct node*) malloc(sizeof(struct node));
t->data=x;
count++;

if(start==NULL)
{
start=t;
start->next=NULL;
return;
}

temp=start;
while(temp->next!=start)
{
temp=temp->next;
}
temp->next=t;
t->next=NULL;

}
void delete_from_begin()
{
struct node*t;
int n;
if(start==NULL)
{
printf("Linked list is empty...\n");
return;
}

n= start->data;
t=start->next;
free(start);
start=t;
count--;

printf("%d deleted from beginning of linked list\n",n);


}

void delete_from_end()
{
struct node *t,*u;
int n;
if(start==NULL)
{
printf("Linked list is empty...\n");
return;
}
count--;

if(start->next==NULL)
{
n=start->data;
free(start);
start=NULL;
printf("%d deleted successfully\n",n);
}

t=start;

while(t->next!=start)
{
u=t;
t=t->next;
}

n=t->data;
u->next=NULL;
free(t);
printf("%d deleted successfully\n",n);
}

void traverse()
{
struct node *t;
t=start;
if ( t==NULL)
{
printf("Empty linked list...nothing to search\n");
return;

}
printf("%d nodes are present in the linked list\n",count);

while(t-> next !=start)


{
printf("%d\n", t->data);
t=t->next;
t->next=NULL;
}
}
OUTPUT:
QUES 13. Program to implement a stack using linked list.

CODE:
#include<stdio.h>
#include<stdlib.h>

struct node
{
int info;
struct node* next;
};
struct node *top;

void push();
void pop();
void display();
int n;

void main()
{
printf("\n\t LINKED LIST IMPLEMENTATION USING STACK");
printf("\n\t ----------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT\n");
int choice=0;
while(choice!=4)
{
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : push(); break;
case 2 : pop(); break;
case 3 : display(); break;
case 4 : printf("Exit..\n"); break;

default : printf("Enter a valid choice 1/2/3/4\n");


}
}
}

void push()
{
int info;
struct node * ptr= (struct node*) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("Overlow memory is full\n");
}

else
{
printf("Enter the value\n");
scanf("%d",&info);
if(top == NULL)
{
ptr->info=info;
ptr->next=NULL; // first insertion
top=ptr;
}

else
{
ptr->info=info;
ptr->next=top;
top=ptr;
}
printf("Value pushed\n");
}

void pop()
{

struct node* ptr;


int item;
if(top == NULL )
{
printf("Underflow memory is empty\n");
}

else
{
item=top->info;
ptr=top;
top=top->next;
free(ptr);
printf("Value popped\n");
}
}

void display()
{
struct node*ptr;
ptr=top;
if(ptr==NULL)
{
printf("Empty stack...");
}

else
{
printf("Stack elements are as follow\n");
while(ptr!=NULL)
{
printf("%d\n",ptr->info);
ptr=ptr->next;
}
}

}
OUTPUT:
QUES 14. Program to implement a queue using linked list.

CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node* next;
}*front,*rear;

int insert();
int delete();
int display();
int n;

int main()
{
printf("\n\t LINKED LIST IMPLEMENTATION USING QUEUE");
printf("\n\t ----------------------------");
printf("\n\t 1.INSERT\n\t 2.DELETE\n\t 3.DISPLAY\n\t 4.EXIT\n");
int choice=0;
while(choice!=4)
{
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : insert(); break;
case 2 : delete(); break;
case 3 : display(); break;
case 4 : printf("Exit..\n"); break;
default : printf("Enter a valid choice 1/2/3/4\n");
}
}
}

int insert()
{
struct node *ptr=(struct node*)malloc(sizeof(struct node));
int item;
if(ptr==NULL)
{
printf("Overflow..memory is full");
}

else
{
printf("Enter the item\n");
scanf("%d",&item);
ptr->item=item;
if( front == NULL && rear==NULL)
{
front=rear=ptr; // first insertion
front->next=NULL;
rear->next=NULL;
}
else
{
rear->next=ptr;
rear=ptr;
rear->next=NULL;
}
printf("Item inserted\n");
}
}

int delete()
{
struct node *ptr;
if(front == NULL && rear==NULL)
{
printf("Underflow..memory is empty");
}

else
{
if(front==rear)
{
ptr=front;
front=rear=NULL;
free(ptr);
}

else
{
ptr=front;
front=front->next;
free(ptr);
}
printf("Item deleted\n");
}
}

int display()
{
struct node* ptr;
ptr=front;
if(front == NULL && rear==NULL )
{
printf("Queue is empty...");
}

else
{
printf("Queue elements are as follows\n");
while(ptr!=NULL)
{
printf("%d\n",ptr->item);
ptr=ptr->next;
}
}
return 0;
}

OUTPUT:
Ques 15: Program showing different tree traversals of a tree
 Preorder tree traversal
 Inorder tree traversal
 Postorder tree traversal
Code:
#include<stdio.h>
#include<stdlib.h>

typedef struct node


{
int data;
struct node *left;
struct node *right;
}node;

node* create()
{
node *p;
int x;
printf("Enter data(-1 for no data ) \n");
scanf("%d",&x);

if(x==-1)
return NULL;

p=(node*)malloc(sizeof(node));
p->data=x;

printf("Enter the left child of %d\n",x);


p->left=create(); //recursion
printf("Enter the right child of %d\n",x);
p->right=create(); //recursion

return p;
}

void preorder( node *t)


{
if(t!=NULL)
{
printf("%d\n",t->data);
preorder(t->left);
preorder(t->right);
}
}

void inorder(node * t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%d\n",t->data);
inorder(t->right);
}
}
void postorder(node *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("%d\n",t->data);
}
}

int main ()
{
node *root;
root=create();
int choice;
do
{

printf("\t\t----------MENU-----------\n");
printf("\t\t1.Preorder tree traversal\n");
printf("\t\t2.Inorder tree traversal\n");
printf("\t\t3.Postorder tree traversal\n");
printf("\t\t4.Exit\n");

printf("Enter your choice\n");


scanf("%d",&choice);
switch(choice)
{
case 1 : printf("The required tree traversal is\n");
preorder(root);break;
case 2 : printf("The required tree traversal is\n");
inorder(root);break;
case 3 : printf("The required tree traversal is\n");
postorder(root);break;
}
}while(choice!=4);

return 0;
}

OUTPUT:
Ques 16: Write a program to implement Heap Sort.
Code:
#include<stdio.h>
void heapify(int arr[], int n, int i)
{
int largest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && arr[left]>arr[largest])
largest=left;
if(right<n && arr[right]>arr[largest])
largest=right;
if (largest!=i)
// if root node is not largest then swap a[i] and a[largest]
{
int temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;

heapify(arr,n,largest);
}
}

void Heapsort(int a[], int n)


{
for(int i=n/2-1;i>=0;i--)
heapify(a,n,i);
// extracting elements from heap
for(int i=n-1;i>=0;i--)
{
// move root node to the end

int temp=a[0];
a[0]=a[i];
a[i]=temp;

heapify(a,i,0);
}
}

void printArray(int A[],int N)


{
for(int i=0;i<N;i++)
{
printf("%d ",A[i]);
}
}

int main()
{
int a[]={23,45,44,87,90,12,36,77};
int n=sizeof(a)/sizeof(a[0]);
printf("Array before sorting:-\n");
printArray(a,n);
Heapsort(a,n);
printf("\nArray after sorting:-\n");
printArray(a,n);
return 0;
}
OUTPUT:

QUES 17.Write a program to implement Quick Sort.


CODE:
#include <stdio.h>
int partition(int[],int,int);
void quicksort(int[],int,int);
void printArray(int[],int);
int main()
{
int a[]={19,18,14,15,20,22,21,16,17};
int n=sizeof(a)/sizeof(a[0]);
printf("Array before sorting:- \n");
printArray(a,n);
quicksort(a,0,n-1);
printf("\nArray after sorting:- \n");
printArray(a,n);
return 0;
}
void quicksort(int a[],int start, int end)
{
if(start < end)
{
int p = partition(a,start,end);
quicksort(a,start,p-1);
quicksort(a, p+1, end);
}
}
int partition ( int a[], int start, int end)
{
int pivot=a[end];
int i=start-1;
for( int j=start;j<=end-1;j++)
{
if(a[j]<pivot)
{
i++;
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
int temp=a[i+1];
a[i+1]=a[end];
a[end]=temp;
return (i+1);
}

void printArray(int A[],int N)


{
for(int i=0;i<N;i++)
{
printf("%d ",A[i]);
}
}

OUTPUT:
QUES 18. Write a program to implement Selection Sort.
CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void SSORT(int[],int);
int main ()
{
int Arr[MAX],N;
printf("Enter the number of elements of 1-D array\n");
scanf("%d",&N);
for(int i=0;i<N;i++)
{
printf("Enter the element Arr[%d]:",i);
scanf("%d",&Arr[i]);
}

SSORT(Arr,N);
printf("Elements after selection sorting :\n");
for( int k=0; k<N ; k++)
{
printf("%d ",Arr[k]);
}
getch();
}
void SSORT(int A[], int n)
{
int i,j,pos,temp,small;

for(i=0;i<n-1;i++)
{
small=A[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(small>A[j])
{
small=A[j];
pos=j;
}
}
temp=A[i];
A[i]=A[pos];
A[pos]=temp;
}
}

OUTPUT:
QUES 19. Write a program to implement Insertion Sort.
CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void ISORT(int[],int);
int main ()
{
int Arr[MAX],N;
printf("Enter the number of elements of 1-D array\n");
scanf("%d",&N);
for(int i=0;i<N;i++)
{
printf("Enter the element Arr[%d]:",i);
scanf("%d",&Arr[i]);
}

ISORT(Arr,N);
printf("Elements after insertion sorting :\n");
for( int k=0; k<N ; k++)
{
printf("%d ",Arr[k]);
}
getch();
}

void ISORT(int A[],int N)


{
int temp,i,j;
for(i=1;i<N;i++)
{
temp=A[i];
for(j=i-1; j>=0 && temp<A[j]; j--)
{
A[j+1]=A[j];
}

A[j+1]=temp;
}
}
OUTPUT:

QUES 20. Write a program to implement Merge Sort.


CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void Merge(int a[],int beg, int mid , int end)
{
int i=0,j=0,k=beg;
int n1=mid-beg+1,n2=end-mid;
int left[n1];
int right[n2];
for(int i=0;i<n1;i++)
{
left[i]=a[beg+i];
}
for(int j=0;j<n2;j++)
{
right[j]=a[mid+1+j];
}
while(i<n1 && j<n2)
{
if(left[i]<right[j])
{
a[k]=left[i];
i++;
}
else
{
a[k]=right[j];
j++;
}
k++;
}
while(i<n1)
{
a[k]=left[i];
k++;
i++;
}
while(j<n2)
{
a[k]=right[j];
k++;
j++;
}
}

void MergeSORT(int A[],int beg,int end)


{
if(beg<end)
{
int mid=(beg+end)/2;
MergeSORT(A,beg,mid);
MergeSORT(A,mid+1,end);
Merge(A,beg,mid,end);
}
}
int main ()
{
int Arr[MAX],N;
printf("Enter the number of elements of 1-D array\n");
scanf("%d",&N);
for(int i=0;i<N;i++)
{
printf("Enter the element Arr[%d]:",i);
scanf("%d",&Arr[i]);
}
MergeSORT(Arr,0,N-1);
printf("Elements after merge sorting :\n");
for( int k=0; k<N ; k++)
{
printf("%d ",Arr[k]);
}
getch();
}

OUTPUT:

You might also like