Sie sind auf Seite 1von 67

School of Computer Applications

LAB MANUAL

MC9217
PROGRAMMING AND
DATA STRUCTURES LAB

Year / Sem : I Year / I Sem

Prepared By
A. ABDUL FAZITH MCA, M.Phil
Lecturer

0
Contents Page No
1. Stack and Queue
1.a Stack 1
1.b Infix to postfix 8
1.c Evaluation of expression 13
1.d Stack using Linked list 17
1.e Queue 21
1.f Circular Queue 27
1.g Queue using Linked list 34

2. Binary tree Traversals 39

3. Merge Sort 44

4. a. Depth First Search (DFS) 46


b. Breath First Search (BFS) 49

5. Warshalls Algorithm 52

6. Dijkstras Algorithm 54

7. Huffmans Algorithm 58

8. Insertion Sort 64

1
1.a STACK

AIM

To know the working principle of Stack and implement the operation on Stack in C

ALGORITHM:

Start the Program


Declare the required variable and functions, initialize the variables if needed
Perform the push operation for insertion, in which a data item is placed at the location pointed
to by the stack pointer, and the address in the stack pointer is adjusted by the size of the data item
Perform the Pop operation for Deletion, in which data item at the current location pointed to
by the stack pointer is removed, and the stack pointer is adjusted by the size of the data item.
view the status of elements in Stack.
stop the program.

PROGRAM:

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

#define MAXSIZE 10
int top=-1,stack[MAXSIZE],item;
void main()
{
int ch;
void push();
void pop();
void view();
clrscr();
do
{
puts("\n________________\n");
puts("\nSTACK OPERATIONS\n");
puts("\n________________\n");
puts("\n1.INSERT\n2.DELETE\n3.VIEW\n4.EXIT\n");
puts("\nEnter your choice:\n");
scanf("%d",&ch);

switch(ch)

2
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
view();
break;
case 4:
exit(0);
}
}while(ch!=4);
getch();
}
void push()
{
if(top==MAXSIZE-1)
puts("\nSTACK IS OVERFLOW\n");
else
{
puts("\nEnter the element to push:\n");
scanf("%d",&item);
top=top+1;
stack[top]=item;
printf("\nThe element %d was pushed.\n",item);
}
}
void pop()
{
if(top==-1)
puts("\nSTACK IS UNDERFLOW\n");
else
{
item=stack[top];
top=top-1;
printf("\nThe element %d was deleted.\n",item);
}
}

3
void view()
{
if(top==-1)
puts("\nSTACK IS EMPTY\n");
else
{
int i;
printf("\nThe elements of the stack are:\n");
for(i=0;i<=top;i++)
printf("\n%d\n",stack[i]);
}
}

/*

OUTPUT:
________________

STACK OPERATIONS
______________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:


1
Enter the element to push:
2
The element 2 was pushed.

4
________________
STACK OPERATIONS
_______________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:


1
Enter the element to push:
3
The element 3 was pushed.
________________

STACK OPERATIONS
________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:


3
The elements of the stack are:
2
3
________________
STACK OPERATIONS
_______________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

5
Enter your choice:
2
The element 3 was deleted.
________________
STACK OPERATIONS
_______________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:


3
The elements of the stack are:
2
________________
STACK OPERATIONS
_______________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:

*/
*/

6
Question and answers
1.What is the basic principle of Stack?
The basic Principle of stack is Last in First out.
2.How to push an element in Stack?
Increment the top pointer , and place an element in Stack .
3.How to pop an element from Stack.?
Decrement the top pointer in order to pop an element from Stack
4.why the stack is best choice in implementation of recursion?
Because of its LIFO (Last In First Out) property it remembers its caller so knows whom
to return when the function has to return. Recursion makes use of system stack for storing the
return addresses of the function call.
5.what is the use of stack in Microprocessor?
Stack is used largely during a function call but depending on the language and level of
programming it may be used to temporarily store processor register data or other variables.

Mainly used for

Return address
return value
parameters to called function
local variables in the called function
programor registers that will be reused in the called function .

7
1.b INFIX TO POSTFIX
AIM
To convert arithmetic expression from an infix notation to postfix notation in C.

PROGRAM:
1.Start the program
2.At a time read one character from the infix expression until it encounters delimiter #.
If the character = operand, place it on the to the output.
If the character =operator, push it onto the stack. If stack has high or equal priority than
current input operator then pop that operator from the stack and place it onto the stack.
If the character is left parenthesis, push it onto the stack.
If the character is right parenthesis ,pop all the operators from the stack till it encounters left
parenthesis, discard both the parenthesis in the ouput
3.Stop the Program .

//Program for Infix to Postfix

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<program.h>

char stack[20];
int t=-1;
void topest(char infix[]);
void push(char);
char pop();

void main()
{
char in_fix[25];
clrscr();
puts("\nEnter infix expression: ");
gets(in_fix);
puts("\nPostfix Expression: ");
topest(in_fix);
}

void push(char s)
{

8
if(t>=19)
{
puts("\nStack Full");
exit(0);
}
else
{
t=t+1;
stack[t]=s;
}
}

char pop()
{
char num;
if(t==-1)
{
puts("\nStack empty");
return(0);
}
else
{
num=stack[t];
t--;
}
return(num);
}

int prefix(char ch)


{
if(ch=='/')
return(5);
else if(ch=='*')
return(4);
else if(ch=='+')
return(2);
else
return(2);
}

9
void topest(char in_fix[])
{
int len;
static int index=0,pos=0;
char s,t;
char postfix[40];
len=strlen(in_fix);
while(index<len)
{
s=in_fix[index];
switch(s)
{
case '(':
push(s);
break;
case ')':
t=pop();
while(t!='(')
{
postfix[pos]=t;
pos++;
t=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(prefix(stack[t])>prefix(s))
{
t=pop();
postfix[pos]=t;
pos++;
}
push(s);
break;
default:
postfix[pos++]=s;
break;

10
}
index++;
}
while(t>0)
{
t=pop();
postfix[pos++]=t;
}
postfix[pos++]='\0';
puts(postfix);

getch();
}

/*

output:

Enter infix expression:


(a+b*c)

Postfix Expression:

Stack empty
abc*+

*/

11
QUESTIONS
1.what are the ways we can represent an arithmetic expression in Stack?
We can represent an expression three ways in stack.
a.Infix notation
b.Prefix notation
c.Postfix notation
2. How to convert the infix notation to the postfix notation?
At a time read one character from the infix expression until it encounters delimiter #.
If the character = operand, place it on the to the output.
If the character =operator, push it onto the stack. If stack has high or equal priority than
current input operator then pop that operator from the stack and place it onto the stack.
If the character is left parenthesis, push it onto the stack.
If the character is right parenthesis ,pop all the operators from the stack till it encounters left
parenthesis, discard both the parenthesis in the ouput.
3. Write down the postfix notation of an given arithmetic expression a+b*c+(d*e+f)*g.
abc*+de*f+g

4.why we need prefix and Postfix notation instead of infix for evaluation of an arithemetic
expression?.
Because converting a prefix or postfix expression back to infix would result in the
same original infix expression without the paranthesis, which would ruin the result of the
expression.Only the Prefix and Postfix forms are capable of preserving the priority of operations
and are hence used for evaluating expressions instead of infix
5.what is the difference between reverse polish notation and postfix notation?
Infact there is no difference between postfix notation and reverse polish notation because both
are same. Reverse polish notation is another name to postfix notation.

12
1.c EVALUATION OF ARITHMETIC EXPRESSION
AIM
To implement the evaluation of postfix arithmetic expression in C.

ALGORITHM:
1. Start the program
2. Declare the required variables and functions, initialize variables if needed.
3. At a time read one character of the postfix expression until it encounter the delimiter #
or null
If the character is an operand ,push its associated value onto the stack.
If the character is an operator, pop two values from the stack, apply the operator
to them and push result onto the stack.
4. Return the result of the expression
5. Stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>

float stack[10];
int top=-1;
void push(char);
float pop();
float exp_eval(char[],float[]);

void main()
{
int j=0;
char s_infix[20];
float number[20],res;
clrscr();
printf("\nEnter the postfix expression: ");
gets(s_infix);
while(s_infix[j]!='\0')
{
if(isalpha(s_infix[j]))

13
{
fflush(stdin);
printf("\nEnter no for %c: ",s_infix[j]);
scanf("%f",&number[j]);
}
j++;
}
res=exp_eval(s_infix,number);
printf("\nThe result is %s=%f",s_infix,res);
getch();
}

float exp_eval(char s_fix[],float data[])


{
int j=0;
float opa,opb,fs;
char ch;
while(s_fix[j]!='\0')
{
ch=s_fix[j];
if(isalpha(s_fix[j]))
{
push(data[j]);
}
else
{
opb=pop();
opa=pop();
switch(ch)
{
case '+':
push(opa+opb);
break;
case '-':
push(opa-opb);
break;
case '*':
push(opa*opb);
break;
case '/':

14
push(opa/opb);
break;
case '^':
push(pow(opa,opb));
break;
}
}
j++;
}
fs=pop();
return(fs);
}
void push(char data)
{
top=++top;
stack[top]=data;
}
float pop()
{
float n;
n=stack[top];
top=--top;
return(n);
}

/*
output:

Enter the postfix expression: abc*+

Enter no for a: 2

Enter no for b: 3

Enter no for c: 2

The result is abc*+=8.000000

*/

15
QUESTION
1.what is the data structure is used in evaluation of an expression?
Stack is the data structure is used in evaluation an expression
2.Compute the value of the expression 10+15*20
Postfix form of the above expression is 10 15 20 * +
First it computes 15 20 *
i.e 15*20=300
Second it computes 10 300+
i.e 300+10=310
3.simplify the given expression 10*12+21/3+7-4*2 based on operator precedence
relation
Given expression is 10*12+21/3+7-4*2
Operator precedence in given expression is /,*,+,-
10*12+7+7-4*2 (/ operator )
120+7+7-8 (* operator )
120+14-8 (+ operator )
134-8 (- operator )
126

4.How to evaluate the expression in postfix form?.


At a time read one character of the postfix expression until it encounter the
delimiter #
If the character is an operand ,push its associated value onto the stack.
If the character is an operator, pop two values from the stack, apply the operator to them
and push result onto the stack.

5. Evaluate the value of the expression AB* CDE/-+ if the numerical values of
A,B,C,D and E are 1,2,3,4,2 respectively.
if A=1,B=2 then AB* is 2
AB*=2,C=3,D=4,E=2
If DE/=2,C=3 then
CDE/-=1
Finally we obtained AB*CDE/-+ as 3.

16
1.d STACK IN LINKED LIST

ALGORITHM:

Start the program


Declare the required variable and functions, initialize the variables if needed.
Create Linked List data structure.
Represent Stack in Linked List data structure.
Perform push operation for insertion, in which a data item is placed at the location pointed to
by the stack pointer, and the address in the stack pointer is adjusted by the size of the data item.
Perform the Pop operation for Deletion, in which data item at the current location pointed to
by the stack pointer is removed, and the stack pointer is adjusted by the size of the data item
View the status of the elements in Stack.
Stop the program.

PROGRAM
//Implementation of Stack Using linked list
#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct list_element
{
int item;
struct list_element *next;
};
typedef struct list_element node;

void main()
{
node *start;
int key,choice;
void create();
node *push();
node *pop();
void display();
start=NULL;
clrscr();
do
{
printf("\n\nMain Menu:\n");
printf("\n1.Push\n2.Pop\n3.Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);

17
switch(choice)
{
case 1:
start=push(start);
printf("\n\nSTACK after Pushing:\n");
display(start);
break;
case 2:
start=pop(start);
break;
case 3:
exit(0);
default:
printf("\nInvalid Option Try Again\n");
}
}while(choice!=3);
}

void display(record) node *record;


{
if(record!=NULL)
{
printf("->%d",record->item);
display(record->next);
}
return;
}

node *push(first) node *first;


{
node *newrecord;
int newitem;
printf("\nEnter the new data: ");
scanf("%d",&newitem);
newrecord=(node *)malloc(sizeof(node));
newrecord->item=newitem;
newrecord->next=first;
first=newrecord;
return(first);
}

node *pop(first)
node *first;
{
node *temp;

18
int target;
if(first==NULL)
printf("\nSTACK EMPTY\n");
else
{
printf("\nElement deleted is: %d",first->item);
temp=first->next;
free(first);
first=temp;
printf("\n\nSTACK after Poping: \n");
display(first);
if(first==NULL)
printf("\nStack is empty now\n");
}
return(first);
}

/*
output:

Main Menu:

1.Push
2.Pop
3.Exit

Enter your choice: 1

Enter the new data: 2

STACK after Pushing:


->2

Main Menu:

1.Push
2.Pop
3.Exit

Enter your choice: 1

Enter the new data: 3

19
STACK after Pushing:
->3->2

Main Menu:

1.Push
2.Pop
3.Exit

Enter your choice: 2

Element deleted is: 3

STACK after Poping:


->2

Main Menu:

1.Push
2.Pop
3.Exit

Enter your choice: 3

*/

Question
1.How to implement the stack in Linked List?
We can represent the stack in linked list structure , insert the element and delete the element
from one end of the linked list. Thus we can do stack operation in linked list.
2. How to increment the top pointer in a Linked representation of stack ?
Top pointer is incremented by obtaining the address of the new record, store the new value in
this address.
3. How to pop an element from a Linked representation of stack?
By deleting the address of the last node .
4. Why do we use linked list data structure for implementation of stack?
Generally Size of the Stack is fixed , so we cannot store the elements more than size of
stack. But the linked list provides required memory space dynamically. so linked list for the best
data structure for implementation of stack.
5. What is the advantage of Stack in Linked List?
Linked List overcome the limitation of the requirement in size of the Stack.

20
1.e QUEUE
AIM
To know the working principle of Queue and implement the operation on Queue in C

ALGORITHM
Start the program
Declare the required variables and function, initialize queue size, rear and front
pointer in Queue.
Perform the push operation on Queue, in which the rear pointer is incremented.
Perform the Pop operation on Queue, in which the front pointer is incremented.
View the status of the elements in Queue.
Stop the program.

PROGRAM

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

#define MAXSIZE 10
int f=-1,r=-1,queue[MAXSIZE],item;
void main()
{
int ch;
void push();
void pop();
void view();
clrscr();
do
{
puts("\n________________\n");
puts("\nQUEUE OPERATIONS\n");
puts("\n________________\n");
puts("\n1.INSERT\n2.DELETE\n3.VIEW\n4.EXIT\n");
puts("\nEnter your choice:\n");
scanf("%d",&ch);

switch(ch)
{

21
case 1:
push();
break;
case 2:
pop();
break;
case 3:
view();
break;
case 4:
exit(0);
}
}while(ch!=4);
getch();
}
void push()
{
if(r==MAXSIZE-1)
puts("\nQUEUE IS OVERFLOW\n");
else
{
puts("\nEnter the element to push:\n");
scanf("%d",&item);
r=r+1;
queue[r]=item;
if(f==-1)
f=0;
printf("\nThe element %d was pushed.\n",item);
}
}
void pop()
{
if(f==-1)
puts("\nQUEUE IS UNDERFLOW\n");
else
{
item=queue[f];
if(f==r)
f=r=-1;
else

22
f=f+1;
printf("\nThe element %d was deleted.\n",item);
}
}
void view()
{
if(f==-1)
puts("\nQUEUE IS EMPTY\n");
else
{
int i;
printf("\nThe elements of the stack are:\n");
for(i=f;i<=r;i++)
printf("\n%d\n",queue[i]);
}
}

/*

output:
________________

QUEUE OPERATIONS
________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:

Enter the element to push:

23
The element 1 was pushed.

________________
QUEUE OPERATIONS
________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:


1
Enter the element to push:

2
The element 2 was pushed.
________________

QUEUE OPERATIONS
________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:


3
The elements of the stack are:

2
________________
QUEUE OPERATIONS
________________

24
1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:


2

The element 1 was deleted.

________________
QUEUE OPERATIONS
________________
1.INSERT
2.DELETE
3.VIEW
4.EXIT
Enter your choice:
3
The elements of the stack are:
2
_______________
QUEUE OPERATIONS
________________
1.INSERT
2.DELETE
3.VIEW
4.EXIT
Enter your choice:
4

*/

25
QUESTION AND ANSWERS
1.what
what is the principle of Queue?
The basic principle of Queue is First in First Out.
2.How to use Queue in OS Scheduling?
At a time CPU can execute only one program, it is necessary to maintain queue when the
more than one programes are getting ready for execution. Based On scheduling policy
,programes are dispatched one by one from Queue for execu
execution.
tion. Thus Queue is used in OS
scheduling.
3.write down array and linked representation of Queue?
Array Representation.

The limitations of using an array for a stack are the same as using an array for a queue.

Linked Representation

4.what is the role of Queue in telecommunication?


When placed on hold for telephone operators. For example, when you phone the toll-free
toll
number for your bank, you can get a recording that says, "Thank you for calling A-1 A Bank. Your
call will be answered by the next avai
available
lable operator. Please wait." This is a queuing system.
5.what is minimum number of queue needed in implementation of Priority Queue?
Two. One queue is used for actual storing of data and another for storing priorities.

26
1. f CIRCULAR QUEUE
AIM
To know the working principle of Circular queue and implement the operation on Circular
Queue in C.

ALGORITHM
Start the program
Declare the required variables and function, initialize queue size, rear and front
pointer in Queue.
Perform the push operation on Queue, in which the rear pointer is incremented
and find rear= (rear+1) MOD queue size
Perform the Pop operation on Queue, in which the front pointer is incremented
and find
front=(front+1) MOD queue size
View the status of the elements in Circular Queue.
Stop the program .

PROGRAM
#include<stdio.h>
#include<conio.h>

#define MAXSIZE 10
int f=-1,r=-1,cqueue[MAXSIZE],item;
void main()
{
int ch;
void push();
void pop();
void view();
clrscr();
do
{
puts("\n_________________________\n");
puts("\nCIRCULAR QUEUE OPERATIONS\n");
puts("\n_________________________\n");
puts("\n1.INSERT\n2.DELETE\n3.VIEW\n4.EXIT\n");
puts("\nEnter your choice:\n");
scanf("%d",&ch);

27
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
view();
break;
case 4:
exit(0);
}
}while(ch!=4);
getch();
}
void push()
{
if(r==MAXSIZE-1)
puts("\nCIRCULAR QUEUE IS OVERFLOW\n");
else
{
puts("\nEnter the element to push:\n");
scanf("%d",&item);
r=(r+1)%MAXSIZE;
cqueue[r]=item;
if(f==-1)
f=0;
printf("\nThe element %d was pushed.\n",item);
}
}
void pop()
{
if(f==-1)
puts("\nCIRCULAR QUEUE IS UNDERFLOW\n");
else
{
item=cqueue[f];
if(f==r)

28
f=r=-1;
else
f=(f+1)%MAXSIZE;
printf("\nThe element %d was deleted.\n",item);
}
}
void view()
{
if(f==-1)
puts("\nCIRCULAR QUEUE IS EMPTY\n");
else
{
int i;
printf("\nThe elements of the stack are:\n");
for(i=f;i<=r;i++)
printf("\n%d\n",cqueue[i]);
}
}

/*

output:
_________________________

CIRCULAR QUEUE OPERATIONS


_________________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:

Enter the element to push:

29
1

The element 1 was pushed.


_________________________

CIRCULAR QUEUE OPERATIONS


_________________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:


1
Enter the element to push:
2
The element 2 was pushed.
_________________________

CIRCULAR QUEUE OPERATIONS


_________________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT
Enter your choice:
3
The elements of the stack are:
1
2

30
_______________________

CIRCULAR QUEUE OPERATIONS


______________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:

The element 1 was deleted.


_________________________

CIRCULAR QUEUE OPERATIONS


_________________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:

Enter the element to push:

The element 3 was pushed.

31
_________________________

CIRCULAR QUEUE OPERATIONS


_________________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT
Enter your choice:
3
The elements of the stack are:

2
3
_________________________

CIRCULAR QUEUE OPERATIONS


_________________________

1.INSERT
2.DELETE
3.VIEW
4.EXIT

Enter your choice:

*/

32
QUESTIONS
1.What is Circular Queue?
Circular Queue is a Queue in the elements are stored in circular manner, in this queue ,the
first element is stored immediately after the last element.
2.what is advantage of Circular Queue?
Circular Queue overcome the limitation simple Queue by utilizing full space of the Queue.
3.How to know the position of an element when performing insertion in Circular Queue ?
To perform the insertion of an element to the queue , the position is calculated as
Rear=(Rear+1)%maxsize
Queue[Rear]=value

4.How to know the position of an element in Circular Queue when performing deletion ?
To perform the deletion,the position of the front pointer calculated by the relation.

Value=Queue[Front]

Front=(Front+1)%maxsize

5.When will circular Queue become empty?

if the value of the front and rear are equal ,then queue will become empty.

33
1.g QUEUE IN LINKED LIST

ALGORITHM:

Start the program


Declare the required variable and functions, initialize the variables if needed.
Create Linked List data structure.
Represent Queue in Linked List data structure.
Perform push operation for insertion, in which a data item is stored and the rear pointer is
incremented by obtaining the address of the new record.
Perform the Pop operation for Deletion, in which data item is removed, and the front pointer
is incremented by deleting the address of the last record.
View the status of the elements in Queue.
Stop the program.

PROGRAM:
//Implementation of Queue Using Linked List

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

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

void enqueue(front,rear,i)
struct node **front,**rear;
int i;
{
struct node *new;
new=(struct node *)malloc(sizeof(struct node));
new->next=NULL;
new->info=i;
if((*front)==NULL)
{
(*front)=new;
(*rear)=new;

34
}
else
{
(*rear)->next=new;
(*rear)=new;
}
}

void del_queue(front)
struct node **front;
{
struct node *temp;
if((*front)!=NULL)
{
temp=*front;
(*front)=(*front)->next;
free(temp);
}
}

void display(front)
struct node *front;
{
while(front!=NULL)
{
printf("%d->",(*front).info);
front=(*front).next;
}
printf("NULL\n");
}

void main()
{
struct node *front=NULL,*rear=NULL;
int choice,data;
clrscr();
do
{
printf("\n\nQueue Operations\n");
printf("\n1.Enqueue\n2.Dequeue\n3.Exit\n");

35
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the new element: ");
scanf("%d",&data);
enqueue(&front,&rear,data);
break;
case 2:
del_queue(&front);
break;
case 3:
exit(0);
default:
printf("\nInvalid Option Try Again\n\n");
}
printf("\nThe queue is\n");
display(front);
if(front==NULL)
printf("\nThe queue is empty\n");
}while(choice!=3);
printf("\n\n");
while(front!=NULL)
{
rear=front->next;
free(front);
front=rear;
}
getch();
}

/*
OUTPUT:

Queue Operations
1.Enqueue
2.Dequeue
3.Exit
Enter your choice: 1

36
Enter the new element: 2

The queue is
2->NULL

Queue Operations

1.Enqueue
2.Dequeue
3.Exit

Enter your choice: 1

Enter the new element: 3

The queue is
2->3->NULL

Queue Operations

1.Enqueue
2.Dequeue
3.Exit

Enter your choice: 2

The queue is
3->NULL

Queue Operations

1.Enqueue
2.Dequeue
3.Exit

Enter your choice: 3

*/

37
Question:
1.How to implement the Queue in Linked List?
We can represent the Queue in linked list structure , insert an element in one end of the
linked list and delete the element from other end of the linked list. Thus we can do Queue
operation in linked list.
2. How to insert an element in a Linked representation of Queue ?
Rear pointer is incremented by obtaining the address of the newrecord , store the new value in
this address.
3. How to pop an element from a Linked representation of Queue?
Front pointer is incremented by deleting the address of the last node . thus the element is
deleted from a linked representation of Queue.
4. Why do we use linked list data structure for implementation of Queue?
Generally Size of the Queue is fixed , so we cannot store the elements more than size of
Queue. But the linked list provides required memory space dynamically. so linked list for the
best data stucture for implementation of Queue.
5. What is the advantage of Stack in Linked List?
Linked List overcome the limitation of the requirement in size of the Queue.

38
2.BINARY TREE TRAVERSAL

AIM:
To implement the binary tree traversal.

ALGORITHM:
1.start the program
2.create a binary tree
3.To traverse a non empty binary tree in preorder
a.visit the root
b.Traverse the left subtree in preorder.
c.Traverse the right subtree in preorder.
4.To traverse a non empty binary tree in inorder
a.Traverse the left subtree in inorder.
b.Visit the root.
c.Traverse the right subtree inorder.
5.To traverse a non empty binary tree in postorder
a.Traverse the left subtree in postorder.
b.Traverse the right subtree in postorder.
c.Visit the root.
6.stop the program.

Program:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *right, *left;
}*root,*p,*q;

struct node *make(int y)


{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=y;
newnode->right=newnode->left=NULL;
return(newnode);
}

void left(struct node *r,int x)


{

39
if(r->left!=NULL)
printf("\n Invalid !");
else
r->left=make(x);
}

void right(struct node *r,int x)


{
if(r->right!=NULL)
printf("\n Invalid !");
else
r->right=make(x);
}

void inorder(struct node *r)


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

void preorder(struct node *r)


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

void postorder(struct node *r)


{
if(r!=NULL)
{
postorder(r->left);
postorder(r->right);

40
printf("\t %d",r->data);
}
}

void main()
{
int no;
int choice;
clrscr();
printf("\n Enter the root:");
scanf("%d",& no);
root=make(no);
p=root;
while(1)
{
printf("\n Enter another number:");
scanf("%d",& no);
if(no==-1)
break;
p=root;
q=root;
while(no!=p->data && q!=NULL)
{
p=q;
if(no<p->data)
q=p->left;
else
q=p->right;
}
if(no<p->data)
{
printf("\n Left branch of %d is %d",p->data,no);
left(p,no);
}
else
{
right(p,no);
printf("\n Right Branch of %d is %d",p->data,no);
}
}

41
while(1)
{
printf("\n 1.Inorder Traversal \n 2.Preorder Traversal \n 3.Postorder Traversal \n 4.Exit");
printf("\n Enter choice:");
scanf("%d",&choice);
switch(choice)
{
case 1 :inorder(root);
break;
case 2 :preorder(root);
break;
case 3 :postorder(root);
break;
case 4 :exit(0);
default:printf("Error ! Invalid Choice ");
break;
}
getch();
}
}

Question
1.what is binary Search Tree?
binary search tree (BST), which may sometimes also be called an ordered or sorted
binary tree, is a node-based binary tree data structure which has the following properties

42
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.

2.Draw the binary Search tree for the given set of elements 1,8,4,6,7,3,10,13,14.

3.Explain
Explain searching operation in Binary search Tree.
If the tree is null, the value we are searching for does not exist in the tree. Otherwise, if
the value equals the root, the search is successful. If the value is less than the root, search the left
subtree. Similarly, if it is greater than the root, searc
search
h the right subtree. This process is repeated
until the value is found or the indicated subtree is null. If the searched value is not found before a
null subtree is reached, then the item must not be present in the tree
tree.
4.Explain
Explain insertion operation in Bi Binary search Tree.
First visit the root and recursively insert the new node to the left subtree if the new value
is less than the root, or the right subtree if the new value is greater than or equal to the root.
5.write down Big o notation for the space and d time of complexity in Binary search tree.tree

Operation Average Worst case

Space O(n) O(n)

Search O(log n) O(n)

Insert O(log n) O(n)

Delete O(log n) O(n)

43
3 .MERGESORT

AIM
To implement the Mergesort in C.

ALGORITHM

start
If the list is of length 0 or 1, then it is already sorted. Otherwise:
Divide the unsorted list into two sublists of about half the size.
Sort each sublist recursively by re-applying the merge sort.
Merge the two sublists back into one sorted list.
stop

PROGRAM

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

#define MAXARRAY 10

void mergesort(int a[], int low, int high);

void main() {
int array[MAXARRAY];
int i=0;
clrscr();
printf("\nEnter the %d elements: ",MAXARRAY);
for(i=0;i<MAXARRAY;i++)
scanf("%d",&array[i]);
printf("Before :");
for(i=0;i<MAXARRAY;i++)
printf(" %d", array[i]);
printf("\n");
mergesort(array,0,MAXARRAY-1);
printf("Mergesort :");
for(i=0;i<MAXARRAY;i++)
printf(" %d", array[i]);

getch();

44
}

void mergesort(int a[], int low, int high)


{
int i=0;
int length=high-low+1;
int pivot=0;
int merge1=0;
int merge2=0;
int working[100];

if(low==high)
return;
pivot=(low+high)/2;
mergesort(a,low,pivot);
mergesort(a,pivot+1,high);
for(i = 0; i < length; i++)
working[i] = a[low + i];
merge1 = 0;
merge2 = pivot - low + 1;
for(i = 0; i < length; i++) {
if(merge2 <= high - low)
if(merge1 <= pivot - low)
if(working[merge1] > working[merge2])
a[i + low] = working[merge2++];
else
a[i + low] = working[merge1++];
else
a[i + low] = working[merge2++];
else
a[i + low] = working[merge1++];
}
}

45
4.a DEPTH FIRST SEARCH ALGORITHM
AIM
To implement Depth First Search Algorithm in C.

ALGORITHM

1.Start the Program.


2.Declare the variables and function, initialize variables if needed.
3. create a function Depth First Traversal in which,
DFT(g)
Repeat for i=1 to n
Visited[i]=0
Repeat for i=1 to n
If visited[i]=0 then
Call DFS(i)
4. create a function Depth First Search in which,
DFS(i)
Visited[i]=1
Write(i)
For each node w adjacent to i
If visited[w]=0
Then call dfs(w)
5.DFS visits all the nodes in recursive way.

PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAX 20

int adj[MAX][MAX];
typedef enum boolean{false,true} bool;
bool visited[MAX];
int n;
void main()
{
int i,v,choice;
void create_graph();
void dfs_rec(int v);
clrscr();
create_graph();

46
printf("\nDepth First Search through recursion\n");
printf("Enter starting node for Depth First Search : ");
scanf("%d",&v);
for(i=1;i<=n;i++)
visited[i]=false;
dfs_rec(v);
getch();
}
void create_graph()
{
int i,max_edges,origin,destin;
printf("Enter number of nodes : ");
scanf("%d",&n);
max_edges=n*(n-1);
for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d( 0 0 to quit ) : ",i);
scanf("%d %d",&origin,&destin);
if((origin==0) && (destin==0))
break;
if( origin > n || destin > n || origin<=0 || destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
{
adj[origin][destin]=1;
}
}
}

void dfs_rec(int v)
{
int i;
visited[v]=true;
printf("%d ",v);
for(i=1;i<=n;i++)
if(adj[v][i]==1 && visited[i]==false)

47
dfs_rec(i);
}

/*

output:

Enter number of nodes : 5


Enter edge 1( 0 0 to quit ) : 1 2
Enter edge 2( 0 0 to quit ) : 1 3
Enter edge 3( 0 0 to quit ) : 2 4
Enter edge 4( 0 0 to quit ) : 3 5
Enter edge 5( 0 0 to quit ) : 0 0

Depth First Search through recursion


Enter starting node for Depth First Search : 1
12435

*/
Question
1.What is Graph traversal?
Traversing a Graph is visiting each of element in a systematic manner. Defining the traversal
of a Graph depends upon the structure of the Graph.
2.What are problem can create in Graph traversal?
a. In general , there is no natural first node in a graph from which the traversal should
start.
b. There is no natural order among the successor of a particular node.
c. Unlike a node of a list or tree , a node of a graph may have more than one
predecessor.
3.Can we use DFS to check whether an undirected graph is connected or not?
Yes, DFS can be used to determine if an undirected graph is connected and to identify the
connected components of an undirected graph.
4. Can we use DFS to check whether a graph is cyclic or not?
Yes , it can aslo be used to determine if a Graph is acyclic or not. In both directed and
undirected graphs, a cycle exists if and only if a back edge exists through a depth first traversal.
5. what is the running time of Depth First traversal?
If n is the number of nodes in Graph then traversing the successors of all the nodes is
O(n^2).

48
4.b BREATH FIRST SEARCH.
AIM:
To implement Breath First Search Algorithm in C.
ALGORITHM:
1.Start the program
2.Declare variables and function , initialize variables if needed.
3.Creat a function for graph G Breath First traversal BFT,
BFT(G)
Repeat for i=1 to n
Visited[i]=0
Repeat for i=1 to n
If(visited[i]=0)
Then call bfs(i)
Bfs(v)
u=v
visited[v]=1
Repeat through next step while(1)
For all nodes w adjacent to u
If (visited[w]=0)
Then visited[w]=1
Write(w)
Add w to queue
4.if que is empty
Then return
5. delete u from queue.
6.return.

PROGRAM
#include<stdio.h>
#include<conio.h>

#define N 10

void bfs(int adj[][N],int visited[],int start)


{
int q[N],rear=-1,front=-1,i;
q[++rear]=start;
visited[start]=1;
while(rear != front)
{

49
start = q[++front];
if(start==9)
printf("10\t");
else
printf("%c \t",start+49); //change to 65 in case of alphabets

for(i=0;i<N;i++)
{
if(adj[start][i] && !visited[i])
{
q[++rear]=i;
visited[i]=1;
}
}
}
}

void main()
{
int visited[N]={0};
int adj[N][N]={{0,1,1,0,0,0,0,0,0,1},
{0,0,0,0,1,0,0,0,0,1},
{0,0,0,0,1,0,1,0,0,0},
{1,0,1,0,0,1,1,0,0,1},
{0,0,0,0,0,0,1,1,0,0},
{0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,0,0,0,1,1,1},
{0,0,1,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,1,1,0}};
clrscr();
bfs(adj,visited,0);
getch();

50
Question
1.what is breath first search ?
Breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores
all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor
nodes, and so on, until it finds the goal.

2.what are the uses of Breadth First search in Graph theory?.

Breadth-first search can be used to solve many problems in graph theory, for example:

Finding all nodes within one connected component


Copying Collection, Cheney's algorithm
Finding the shortest path between two nodes u and v
Testing a graph for bipartiteness
FordFulkerson method for computing the maximum flow in a flow network
Serialization/Deserialization of a binary tree vs serialization in sorted order, allows the
tree to be re-constructed in an efficient manner.

3.Can we use BFS to check whether an undirected graph is connected or not?


Yes, DFS can be used to determine if an undirected graph is connected and to identify the
connected components of an undirected graph.
4. Can we use BFS to check whether a graph is cyclic or not?
Yes , it can aslo be used to determine if a Graph is acyclic or not. In both directed and
undirected graphs, a cycle exists if and only if a back edge exists through a Breadth first
traversal.
5.What is the space complexity of BFS in Worst case?
The worst case performance in space complexity of BFS is O( | V | + | E | ) = O(bd ).

51
5.WARSHALLS ALGORITHMS
AIM:
To implement the warshalls algorithm

Algorithm:-
1.Start
2.Copy the Adjacency matrix into another matrix called the Path matrix
3.Find in the Path matrix for every element in the Graph, the incoming and outgoing edges
For every such pair of incoming and outgoing edges put a 1 in the Path matrix
4.Stop the program.

#include<stdio.h>
#include<conio.h>
void main()
{
int adj[10][10],n,i,j,k;
clrscr();
printf("\n Enter the no of nodes \n");
scanf("%d",&n);
printf("\n Enter relation matrix if i&j related\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("\n Enter the %d adj[%d][%d]",i,j);
scanf("%d",&adj[i][j]);
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
adj[i][j]=adj[i][j]||(adj[i][k]&&adj[k][j]);
}
printf("\n Transitive closure of matrix \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
printf("%d",adj[i][j]);
getch();
}

52
Question
1.what is the purpose of warshalls algorithm?
It is used to find the transitive closure of matrix.
2.what is the difference between Floyd warshall and warshalls algorithm ?
Warshalls Algorithm :it is used to find the transitive closure of relation graph.
Floyds warshalls Algorithm :it is used to find the shortest path between any pair of vertices in
the graph.
3.What is the time complexity of warshalls algorithm?
Time complexity: o(n2)

53
6. DIJIKSTRA ALGORITHM
AIM
To implement the Dijikstra Algorithm in C.

ALGORITHM:
1.Start the program
2.Declare the variable and function, initialize variable if needed.
3.select singlesourcesshortestpath(c[][],d[],S)
4.S={1}
repeat for i=1 to n.
d[i]=c[1][i]
repeat k=2 to n-1
for each u such that u!=1 and u has atleast one coming edge
if (dist[u]> dist[i]+cost[i][u]) then
dist[u]=dist[i]+cost[i][u]
add u tp S
5.Return

PROGRAM:
#include<stdio.h>
struct edge
{
int vs,vd,wt;
}a[20];
void dj(int);
int g[20][20];
void main()
{
int n,i,j;
clrscr();
printf("\nEnter the no of vertices: ");
scanf("%d",&n);
printf("\nEnter the directed adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&g[i][j]);
printf("\nTHE MATRIX IS ");
for(i=1;i<=n;i++)
{
printf("\n");

54
for(j=1;j<=n;j++)
printf("%d\t",g[i][j]);
}
dj(n);
getch();
}
void dj(int n)
{
int i,p,j,k=0,st[20],l,sm,t=0,d,pred[20],dist[20],m[20],s;
for(i=1;i<=n;i++)
st[i]=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(g[i][j]>0)
{
k++;
a[k].vs=i;
a[k].vd=j;
a[k].wt=g[i][j];
}
}
}
printf("\nEnter the starting node ");
scanf("%d",&s);
st[s]=1;
pred[s]=s;
dist[s]=0;
for(i=1;i<=n;i++)
{
if(i!=s)
{
pred[i]=i;
dist[i]=9999;
}
}
for(p=1;p<=k;p++)
{
t=0;

55
for(i=1;i<=k;i++)
{
if((st[a[i].vs]==1)&&(st[a[i].vd]==0))
{
m[++t]=(a[i].wt+dist[a[i].vs]);
}
}
sm=m[1];
for(l=2;l<=t;l++)
{
if(m[l]<sm)
sm=m[l];
}
for(i=1;i<=k;i++)
{
if((st[a[i].vs]==1)&&(st[a[i].vd]==0))
{
if((a[i].wt+dist[a[i].vs])==sm)
{
st[a[i].vd]=1;
pred[a[i].vd]=a[i].vs;
dist[a[i].vd]=a[i].wt+dist[a[i].vs];
}
}
}
}
printf("\nPREDECESSOR \n\n");
for(i=1;i<=n;i++)
printf("%d",pred[i]);
printf("\nDISTANCE\n");
for(i=1;i<=n;i++)
printf("%d",dist[i]);
printf("\nENTER THE DESTINATION VERTEX\n");
scanf("%d",&d);
printf("\nSHORTEST PATH IS ");
printf("%d",d);
while(d!=s)
{
d=pred[d];
printf("<-%d",d);

56
}
}

/*

OUTPUT:
Enter the no of vertices:
2

Enter the directed adjacency matrix


1
2
3
4

THE MATRIX IS
1 2
3 4
Enter the starting node 1

PREDECESSOR
11
DISTANCE
02
ENTER THE DESTINATION VERTEX
4
SHORTEST PATH IS 4<-2<-1

*/

Question
1.what is the shortest path problem?
The shortest path problem is problem in which path is to be determined from s to t such
that the sum of the weights of the arcs on the path is minimum.
2.What is the use of shortest path problem?
It is used to find the optimal path from source to the destination.
3.Write any uses of shortest path problem in Optimization Technique?
It determines the total completion time of the project in PERT .
It determine the total completion and cost of the project in CPM.
4.What is Dijkstra's Algorithm?

57
This is single source shortest path algorithm in which the distance is calculated from a
single given source node.

5. What is the use Dijkstra's Algorithm ? How it is used in Computer Network?.


Dijkstra's Algorithm is that it is one of the most important and useful algorithms available for
generating (exact) optimal solutions to a large class of shortest path problems.
Dijkstra's Algorithm is very popular in computer network because It is used to determine
optimal routing path from the source node to the destination node.

7.HUFFMANS ALGORITHM
Aim:
to implement huffman algorithms.

Algorithm
1. start the program
2.read the symbols from the files
3.find the number of occurances of each symbol and store this value as frequency to each
symbol.
4.store the each symbol along with frequency in a priority queue.
5.now written all symbols in a sorted sequence according to the frequency.
6.find the sum of least two frequency and merge into a single one and put in a place where to be
in sorted sequence according to its new frequency(sum of two frequencies).
7.find again the sum of least two frequency and merge into a single and put it in place where it is
to be in sorted order.
8.proceeding in this manner until the entire frequencies are merged into a single value.
9.now this total frequency will be considered as root of binary tree and draw the left and write
branch according to summed frequency .
10. draw the binary tree continuously until reach single value frequency .now the single value
frequency will be considered as leaf node of binary tree.
11.place 0 in the left brach and place 1 in the right brach in the entire binary tree.
12.now read the binary sequence to each symbol from the root node to the leaf node of binary
tree.
13. thus the unique code is generated to each symbol.
14.stop the program.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define MAXBITS 32

58
#define MAXNODES 512
#define MAXSYMBS 256
int hmin(void);
void hinsert(int,int);
struct codetype
{
int bits[MAXBITS];
int startpos;
};
struct nodetype
{
int freq;
int father;
int isleft;
};
typedef struct hlist
{
int pos;
int hfreq;
struct hlist *next;
}hnode;
struct list
{
char alph;
int freq;
};
struct list a[256];
hnode *hroot=NULL,*traversal;
int hmin()
{
int p;
p=hroot->pos;
traversal=hroot;
hroot=traversal->next;
free(traversal);
return(p);
}
void hinsert(int p,int freq)
{
hnode *new1=(hnode*)malloc(sizeof(hnode));

59
new1->pos=p;
new1->hfreq=freq;
traversal=hroot;
if(hroot==NULL)
{
hroot=new1;
hroot->next=NULL;
return;
}
if(hroot->next==NULL)
{
if(hroot->hfreq>new1->hfreq)
{
new1->next=hroot;
hroot=new1;
traversal->next=NULL;
return;
}
else
{
hroot->next=new1;
new1->next=NULL;
return;
}
}
if(hroot->hfreq>=new1->hfreq)
{
new1->next=hroot;
hroot=new1;
return;
}
while(traversal->next->hfreq<new1->hfreq)
{
traversal=traversal->next;
if(traversal->next==NULL)
break;
}
if(traversal->next->hfreq>=new1->hfreq)
{
new1->next=traversal->next;

60
traversal->next=new1;
return;
}
new1->next=NULL;
traversal->next=new1;
}
void main()
{
struct codetype cd,code[MAXSYMBS];
struct nodetype node[MAXNODES];
int i,f,k,p,p1,p2,root,n;
char symb,alph[MAXSYMBS];
clrscr();
for(i=0;i<MAXSYMBS;i++)
alph[i]=' ';
printf("\n\t\t HUFFMAN'S ALGORITHM");
printf("\n How many unique symbols:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter %d symbol and Its frequency",i+1);
flushall();
scanf("%c %d ",&symb,&node[i].freq);
hinsert(i,node[i].freq);
alph[i]=symb;
}
for(p=n;p<(2*n-1);p++)
{
p1=hmin();
p2=hmin();
node[p1].father=p;
node[p1].isleft=1;
node[p2].father=p;
node[p2].isleft=0;
node[p].freq=node[p1].freq+node[p2].freq;
hinsert(p,node[p].freq);
}
root=hmin();
for(i=0;i<n;i++)
{

61
cd.startpos=MAXBITS;
p=i;
while(p!=root)
{
--cd.startpos;
if(node[p].isleft)
cd.bits[cd.startpos]=0;
else
cd.bits[cd.startpos]=1;
p=node[p].father;
}
for(k=cd.startpos;k<MAXBITS;k++)
code[i].bits[k]=cd.bits[k];
code[i].startpos=cd.startpos;
}
printf("\n Huffman code is:\n");
for(i=0;i<n;i++)
{
printf("\n %c:",alph[i]);
for(k=code[i].startpos;k<MAXBITS;k++)
printf("%d",code[i].bits[k]);
printf("\n");
}
getch();
}

Questions:
1.What is the use of huffman algorithm?
The Huffman algorithm is used to encode the given words.
2.what are the advantages of Huffman algorithms?
The Huffman algorithm is used to create a unique code for each symbols.
1.The most of telecommunication is done by using Huffman algorithm.
2.Huffman coding is an entropy encoding algorithm used for lossless data compression

62
3.what is the Huffman encoding for the word ERDBEERE?

Character Code

E 0

R 10

B 110

D 111

4. Draw the Huffman tree for the word ERDBEERE?

5.what is the time complexity of Huffman algorithm?


Time complexity :O(n*logn)

63
8. INSERTION SORT
Aim:
To implement insertion sort.

Algorithm:
1.start the program.
2.for i 1 to i length(a)-1
{
// a[ i ] is added in the sorted sequence a[0, .. i-1]
// save a[i] to make a hole at index ihole
item a[i]
ihole i
// keep moving the hole to next smaller index until a[ihole - 1] is <= item
while ihole > 0 and a[ihole - 1] > item
{
// move hole to next smaller index
a[ihole] a[ihole - 1]
ihole ihole - 1
}
// put item in the hole
a[ihole] item
}
3.stop the program.

Programs:
#include<stdio.h>
int main()
{
int i,j=0,s,temp,a[20];
clrscr();
//printf("j=%d",j);
printf("Enter total elements:");
scanf("%d",&s);
printf("Enter %d elemnts:",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=1;i<s;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];

64
j=j-1;
}
a[j+1]=temp;
}
printf("After sorting:");
for(i=0;i<s;i++)
printf("%d",a[i]);
return 0;
}

65
Queustions:
1.what is the sorting ?
Arranging the elements either in ascending order or in descending order.
2.what is insertion sort?

most common variant of insertion sort, which operates on arrays, can be described as follows:

1. Suppose there exists a function called Insert designed to insert a value into a sorted
sequence at the beginning of an array. It operates by beginning at the end of the sequence
and shifting each element one place to the right until a suitable position is found for the
new element. The function has the side effect of overwriting the value stored immediately
after the sorted sequence in the array.
2. To perform an insertion sort, begin at the left-most element of the array and invoke Insert
to insert each element encountered into its correct position. The ordered sequence into
which the element is inserted is stored at the beginning of the array in the set of indices
already examined. Each insertion overwrites a single value: the value being inserted

3.what is insertion sort?

37495261

37495261

37495261

34795261

34795261

34579261

23457961

23456791

12345679

4.what is the worst case complexity of insertion sort?


Worst case performance: (n2) comparisons
5.what is the best case complexity of insertion sort?
Best case performance :O(n) comparisons

66