Ds Lab
Ds Lab
ENGINEERING COLLEGE
R.S.M. NAGAR, KAVARAIPETTAI - 601 206.
GUMMIDIPOONDI TALUK, THIRUVALLUR DISTRICT.
Certified that this is a bonafide record work done by Padiri Venkat Sai with Roll /
Register number 111519104301 of III semester Computer Science and Engineering
Department during the academic year 2020-2021.
LINEAR SEARCH
10a 15/10/2020 55
BINARY SEARCH
10b 15/10/2020 57
BUBBLE SORT
11a 19/10/2020 59
SHELL SORT
11b 24/10/2020 61
3
QUICK SORT
11c 29/10/2020 64
HASHING
12 02/11/2020 67
Ex.No.1a
Date : 17/08/2020
Algorithm:
1. Create a Stack[ ] with MAX size as your wish.
2. Write function for all the basic operations of stack - PUSH(), POP() and
DISPLAY().
3. By using Switch case, select push() operation to insert element in the stack.
• Step 1: Check whether stack is FULL. (top == SIZE-1)
• Step 2: If it is FULL, then display "Stack is FULL!!! Insertion is not
possible!!!" and terminate the function.
• Step 3: If it is NOT FULL, then increment top value by one (top++) and
set stack[top] to value (stack[top] = value).
4. Similarly, By using Switch case, select pop() operation to delete element
from the stack.
• Step 1: Check whether stack is EMPTY. (top == -1)
• Step 2: If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not
possible!!!" and terminate the function.
• Step 3: If it is NOT EMPTY, then delete stack[top] and decrement top
value by one (top--).
5. Similarly, By using Switch case, select display() operation to display all
element from the stack.
• Step 1: Check whether stack is EMPTY. (top == -1)
• Step 2: If it is EMPTY, then display "Stack is EMPTY!!!" and terminate
the function.
• Step 3: If it is NOT EMPTY, then define a variable 'i' and initialize with
top. Display stack[i] value and decrement i value by one (i--).
• Step 3: Repeat above step until i value becomes '0'.
6. Close the program #include <stdio.h>
int main()
4
{
push(10);
push(20);
push(30);
display();
pop();
display();
return 0; }
void push(int x)
{ if(top==size-1)
printf("Stack Full\n");
else
{
top=top+1;
s[top]=x;
}
}
void pop()
{ if(top==-1)
printf("Stack Empty\n");
else
{
printf("popped element : %d \n",s[top]);
top--;
}}
}}
OUTPUT:
Stack Elements are :
30
20
10 popped element
: 30 Stack Elements
are :
5
20
10
Result:
Thus the C program to implement Stack ADT by using array is executed successfully and the
output is verified.
Ex. No: 1b
Date: 17/08/2020
1.(b) ARRAY IMPLEMENTATION OF QUEUE ADT
Aim:
To write a C program to implement Queue ADT by using arrays
Algorithm:
1. Create a Queue[ ] with MAX size as your wish.
2. Write function for all the basic operations of stack - Enqueue(), Dequeue()
and Display().
3. By using Switch case, select Enqueue() operation to insert element in to
the rear/back end of the queue.
• Check whether queue is FULL. (rear == SIZE-1)
• If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!"
and terminate the function.
• If it is NOT FULL, then increment rear value by one (rear++) and set
queue[rear] = value
4. Similarly, by using Switch case, select Dequeue() function is used to
remove the element from the front end of the queue.
• Check whether queue is EMPTY. (front == rear)
• If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not
possible!!!" and terminate the function.
• Step 3: If it is NOT EMPTY, then increment the front value by one (front
++). Then display queue[front] as deleted element. Then check
whether both front and rear are equal (front == rear), if it TRUE, then
set both front and rear to '-1' (front = rear = -1).
6
5. Similarly, by using Switch case, select display() operation to display all
element of the queue.
• Step 1: Check whether queue is EMPTY. (front == rear)
• Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate
the function.
• Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i =
front+1'.
• Step 3: Display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until 'i' value is equal to rear (i <= rear)
6. Close the program
Program #include
<stdio.h>
int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}
void enqueue(int x)
{ if(rear==size-1)
printf("Queue Full\n");
else
{
if((front==-1) &&(rear==-1))
{
front=0;
rear=0;
} else
rear=rear+1;
q[rear]=x;
}
}
7
void dequeue()
{ if((front==-1)&&(rear==1))
printf("Queue Empty\n");
else
{
if(front==rear)
{
printf("Deleted element :
%d\n",q[front]); front=-1;
rear=-1;
}
else
{
printf("Deleted element : %d\n",q[front]);
front++;
}
}
}
void display()
{ if((front==-1)&&(rear==-1))
printf("Queue Empty\n");
else
{
printf("Queue Elements are :\n");
for(i=front; i<=rear; i++)
printf("%d \n",q[i]);
}
}
OUTPUT:
Queue Elements are :
10
20
30
Deleted element : 10
Queue Elements are :
20
30
Result
Thus the C program to implement Queue ADT by using array is executed
successfully and the output is verified.
8
EX No.2
Date : 20/08/2020
2. ARRAY IMPLEMENTATION OF LIST ADT
Aim:
1. Start
2. Create a list of n elements
3. Display list operations as a menu
4. Accept user choice
5. If the user wants insert an element in the list
6. Get position of element to be inserted. Increment length of the list.
7. Move elements one position downwards there on Store the new element in corresponding
position.
8. If the user wants to delete an element in the list.
9. Get position of element to be deleted.Move elements one position upwards there on.
Decrement length of the list
10. If the user wants to search an element in list.Traverse the list and inspect each element
Report position if it exists.
11. Stop
Program:
#include<stdio.h> int
a[10]={23,34,56},n=3;
void delete(int p)
{ int item,i; item=a[p-
1];
printf("he deleted elements is %d",item);
for(i=p-1;i<n-1;i++)
a[i]=a[i+1];
n=n-1;
}
void display()
{ int
i;
printf("The elements in list are:\n");
9
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
int main()
{ int x,p;
display();
printf("Enter the element & position to be inserted");
scanf("%d%d",&x,&p);
insert(p,x);
display();
printf("Enter the position to be deleted");
scanf("%d",&p); delete(p); display();
return 0;
}
OUTPUT:
The elements in list are:
23
34
56
Enter the element & position to be inserted25
2
The elements in list are:
23
25
34
56
Enter the position to be deleted3 he deleted
elements is 34The elements in list are:
23
25
56
Result
10
Thus various operations was successfully executed on list using array
implementation.
Aim:
To write C Program to implement Polynomial Addition.
Algorithm:
2: For addition of two polynomials if exponents of both the polynomials are same
then we ad the coefficients. For storing the result we will create the third linked
lists say P3.
#include<stdio.h>
#include<stdlib.h> struct
node
{ float coef; int
expo; struct
node *link;
};
main( )
{
struct node *start1=NULL,*start2=NULL;
11
printf("Enter polynomial 1 :\n");
start1=create(start1);
printf("Polynomial 1 is : ");
display(start1);
printf("Polynomial 2 is : ");
display(start2);
poly_add(start1, start2);
}/*End of main()*/
12
ptr->link=tmp;
}
return start;
}/*End of insert()*/
13
while(p1!=NULL && p2!=NULL)
{
if(p1->expo > p2->expo)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
else if(p2->expo > p1->expo)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
else if(p1->expo==p2->expo)
{
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link; p2=p2->link;
}
}
/*if poly2 has finished and elements left in poly1*/
while(p1!=NULL)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
/*if poly1 has finished and elements left in poly2*/
while(p2!=NULL)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
printf("Added polynomial is : ");
display(start3);
}/*End of poly_add() */
Output:
Enter polynomial 1 :
Enter the number of terms : 4
Enter coeficient for term 1 : 3
Enter exponent for term 1 : 3
Enter coeficient for term 2 : 4
Enter exponent for term 2 : 2
Enter coeficient for term 3 : 2
Enter exponent for term 3 : 1
Enter coeficient for term 4 : 4
Enter exponent for term 4 : 0
Enter polynomial 2 :
Enter the number of terms : 3
Enter coeficient for term 1 : 5
14
Enter exponent for term 1 : 3
Enter coeficient for term 2 : 6
Enter exponent for term 2 : 1
Enter coeficient for term 3 : 2
Enter exponent for term 3 : 0
Polynomial 1 is : (3.0x^3) + (4.0x^2) + (2.0x^1) + (4.0x^0)
Polynomial 2 is : (5.0x^3) + (6.0x^1) + (2.0x^0)
Added polynomial is : (8.0x^3) + (4.0x^2) + (8.0x^1) + (6.0x^0)
Result:
Aim:
Algorithm
1. Start
2. Define a array stack of size max = 20
3. Initialize top = -1
4. Read the infix expression character-by character If character is an operand print it If
character is an operator
Compare the operator’s priority with the stack[top] operator.
If the stack [top] has higher/equal priority than the input operator,
Pop it from the stack and print it.
Else
15
Push the input operator onto the stack
If character is a left parenthesis, then push it onto the stack.
If character is a right parenthesis, pop all operators from stack and print it
until a left parenthesis is encountered. Do not print the parenthesis.
If character = $ then Pop out all operators,
Print them and Stop
.
Program
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int a[100],top=-1;
void push(char);
char pop();
int main()
{ int
i;
char exp[100],c;
puts("Enter expression\n");
scanf("%s",exp);
for(i=0;exp[i]!='\0';i++)
{
if(isalpha(exp[i]))
putchar(exp[i]);
else
{
switch(exp[i])
{ case '*': case
'/': case '(':
push(exp[i]);
break; case '':
case '+':
if(a[top]=='*'||a[top]=='/')
{
putchar(pop());
push(exp[i]); }
else
push(exp[i]);
break; case ')':
while((c=pop())!='(')
16
putchar(c); break;
}
}
}
while(top!=-1)
putchar(pop());
return 0; } void
push(char ele) {
a[++top]=ele; }
char pop()
{ return a[top-
-]; }
OUTPUT:
Enter expression
A*B+C/D-E
AB*CD/E-+
Result:
Thus the C Program for applications of Stack is implemented Successfully.
Exno.4 IMPLEMENTATION OF BINARY TREE & ITS OPERATIONS
DATE:07/09/2020
Aim:
Algorithm:
1. Start the program
2. Create a tree
17
Program:
// C program for different tree traversals
#include <stdio.h>
#include <stdlib.h>
return(node);
}
18
printInorder(node->left);
getchar();
return 0;
}
Output:
19
Preorder traversal of binary tree is
12453
Inorder traversal of binary tree is
42513
Postorder traversal of binary tree is
45231
Result:
Thus C Program for Binary Tree is implemented Successfully.
Aim:
To write a C program to implement Binary Search Tree.
Algorithm:
20
5. Stop the program
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct bst
{ int
data;
struct bst *left,*right;
}node; void insert(node *,node
*); void inorder(node *); node
*search(node *,int,node **); void
del(node *,int);
void main()
{ int choice;
char
ans='N'; int
key;
node *New,*root,*tmp,*parent;
node *get_node();
root=NULL;
clrscr(); printf("\n \t Program for Binary
Search Tree");
do
{
printf("\n1 Create \n2. Search \n3.Delete \n4.Display\n 5.Exit");
printf("\n Enter ur choice :"); scanf("%d",&choice);
switch(choice)
{ case
1: do
{
New=get_node(); printf("\n
Enter the element");
scanf("%d",&New->data);
if(root==NULL) root=New; else
insert(root,New);
printf("\n Do u want to enter moreelements?(Y/N)");
ans=getch();; }while(ans=='Y'); break; case 2:
printf("\n Enter the element which u want
tosearch");
scanf("%d",&key);
tmp=search(root,key,&parent);
printf("\n Parent of the node %d is%d",tmp->data,parent->data);
break; case 3: printf("\n Enter the element u wish to
delete");
21
scanf("%d",&key);
del(root,key); break; case
4: if(root==NULL)
printf("Tree is not
created"); else
{ printf("\n The Tree is
:"); inorder(root);
}
break; case
5: exit(0);
}
}
while(choice!=5);
}
node *get_node()
{
node *temp; temp=(node
*)malloc(sizeof(node)); temp>left=NULL;
temp->right=NULL;
return temp;
}
void insert(node *root,node *New)
{
if(New->data<root->data)
{ if(root-
>left==NULL) root>left=New;
else
insert(root->left,New);
}
if(New->data>root->data)
{ if(root-
>right==NULL) root>right=New;
else
insert(root->right,New);
}
}
node *search(node *root,int key,node **parent)
{
node *temp;
temp=root;
while(temp!=NULL)
{
if(temp->data==key)
{
printf("\n %d Element is present",temp->data);
return temp;
}
22
*parent=temp; if(temp->data>key)
temp=temp->left;
else temp=temp-
>right;
}
return NULL;
}
void del(node *root,int key)
{
node *temp,*parent,*temp_succ;
temp=search(root,key,&parent); if(temp-
>left!=NULL&&temp->right!=NULL)
{
parent=temp; temp_succ=temp-
>right;
while(temp_succ->left!=NULL)
{
parent=temp_succ;
temp_succ=temp_succ->left;
}
temp->data=temp_succ>data;
parent->right=NULL; printf("\n
Now delete it!"); return;
}
if(temp->left!=NULL&&temp->right==NULL)
{ if(parent->left==temp)
parent->left=temp->left; else
parent->right=temp>left;
temp=NULL; free(temp);
printf("\n Now Delete it !");
return;
}
if(temp->left==NULL&&temp->right!=NULL)
{ if(parent->left==temp)
parent->left=temp->right; else
parent->right=temp-
>right; temp=NULL;
free(temp); printf("\n
Now Delete it
!"); return;
}
if(temp->left==NULL&&temp->right==NULL)
{ if(parent->left==temp) parent->left=NULL;
else parent->right=NULL; printf("\n Now
Delete it
!"); return;
}
23
}
void inorder(node *temp)
{
if(temp!=NULL)
{ inorder(temp->left);
printf("\n %d",temp>data);
inorder(temp->right);
}
}
Output
:
Output:
Program for BinarySearch Tree
1. Create
2. Search3. Delete
4. Display
5. Exit
Enter your choice: 1
Enter the Element: 100
Do you want to enter more element?(Y/N) : Y
Enter the Element: 200
Do you want to enter more element?(Y/N) : Y
Enter the Element: 300
Do you want to enter more element?(Y/N) : Y
Enter the Element: 400
Do you want to enter more element?(Y/N) : Y
Enter the Element: 500
Do you want to enter more element?(Y/N) : N
Program for Binary
Search Tree
1. Create
2. Search3. Delete
4. Display
5. Exit
Enter your choice: 2
Enter the element you wish to search: 200
200 Element is present
Parent node of 200 is 100
Program for Binary
Search Tree
1. Create
2. Search
3. Delete
4. Display
24
5. Exit
Enter your choice: 4
The Tree is
100
200
300
400
500
Program for Binary
Search Tree
1. Create
2. Search
3. Delete
4. Display
5. Exit
Enter your choice: 3
Enter the element you wish to delete: 300 300
element is present!
Now Deleted!!
Program for Binary
Search Tree
1. Create
2. Search
3. Delete
4. Display
5. Exit
Enter your choice: 4
The Tree is
100
200
400
500
Result:
Thus the C Program for Binary Search Tree is implemented successfully.
Ex.No.6 AVL TREE
Date:14/09/2020
25
Aim:
ALGORITHM:-
3. Check whether root is nullIf yes make the new value as root.
4. if yes, print “duplicate value”.Otherwise insert the value at its proper position and
balance the tree using rotations.
5. To display the tree values check whether the tree is null.If yes, print “tree is
empty”.Else print all the values in the tree form and in order ofthe tree.
7.End
26
struct node
{ int data;
struct node
*left,*right; int ht;
};
Typedef struct node node;
int main() {
node
*root=NULL
;
int x,n,i,op;
do
{
printf("\n1)Create:");
printf("\n2)Insert:");
printf("\n3)Delete:");
printf("\n4)Print:");
printf("\n5)Quit:");
printf("\n\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter no. of elements:");
scanf("%d",&n);
printf("\nEnter tree data:");
root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
27
}
break; case
2: printf("\nEnter a
data:");
scanf("%d",&x);
root=insert(root,x)
;
break;
return 0;
}
28
}
T->ht=height(T);
return(T);
}
if(T==NULL)
{
return NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T); else
T=LR(T);
} else if(x<T-
>data)
{
T->left=Delete(T->left,x); if(BF(T)==-
2) //Rebalance during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
//data to be deleted is found if(T-
>right!=NULL)
{ //delete its inorder succesor
p=T->right;
T->data=p->data;
T->right=Delete(T->right,p->data);
29
}
else
return(T->left);
}
T->ht=height(T);
return(T); }
if(T->right==NULL)
rh=0; else
rh=1+T->right->ht;
if(lh>rh)
return(lh);
return(rh);
}
return(y);
}
30
{
T=rotateright(T);
return(T);
}
return(T);
}
if(T->left==NULL)
lh=0; else lh=1+T->left-
>ht;
if(T->right==NULL)
rh=0; else
rh=1+T->right->ht;
return(lh-rh);
}
31
>data,BF(T)); inorder(T-
>right);
}
}
Output:
1) Create:
2) Insert:
3) Delete:
4) Print:
5) Quit:
1) Create:
2) Insert:
3) Delete:
4) Print: 5)Quit:
Enter Your Choice:4
Preorder sequence:
5(Bf=1)2(Bf=-1)1(Bf=0)4(Bf=1)3(Bf=0)8(Bf=1)7(Bf=0)
Inorder sequence:
1(Bf=0)2(Bf=-1)3(Bf=0)4(Bf=1)5(Bf=1)7(Bf=0)8(Bf=1)
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:2
Enter a data:6
1)Create:
2)Insert:
32
3) Delete:
4) Print: 5)Quit:
Enter Your Choice:4
Preorder sequence:
5(Bf=1)2(Bf=-1)1(Bf=0)4(Bf=1)3(Bf=0)7(Bf=0)6(Bf=0)8(Bf=0)
Inorder sequence:
1(Bf=0)2(Bf=-1)3(Bf=0)4(Bf=1)5(Bf=1)6(Bf=0)7(Bf=0)8(Bf=0)
1) Create:
2) Insert:
3) Delete:
4) Print:
5) Quit:
Enter a data:5
1)Create:
2) Insert:
3) Delete:
4) Print: 5)Quit:
Enter Your Choice:4
Preorder sequence:
6(Bf=1)2(Bf=-1)1(Bf=0)4(Bf=1)3(Bf=0)7(Bf=-1)8(Bf=0)
Inorder sequence:
1(Bf=0)2(Bf=-1)3(Bf=0)4(Bf=1)6(Bf=1)7(Bf=-1)8(Bf=0)
1) Create:
2) Insert:
3) Delete:
4) Print:
5) Quit:
Aim:
To write C Program to implement Binary Heap
Algorithm:
33
1. find the smallest among the left(data[pos]) and
right child(data[pos + 1]). Check whether the smallest 2.
among them is lesser than their parent. If so, swap
3. parent and the node with smallest value. Continue the
4. above processing from index "low" to "count"
5. 40
/ \
10 30
10 is smaller tha 30. 10 is comapared with its parent and it is smaller(10 <
40). So, swap parent and 10.
10
/ \
40 30
6. To construct a heap we always consider the
* nodes which are present above the leaf. Here,
* nodes present after data[n/2 - 1] are leaf nodes
* insertHeap() - used for restoring the heap
* data - holds node values & n - node count
*/
7. To bulid a heap
/* if parent of the current node has value
* greater than its child, then swap parent
* and child nodes. Traverse up the tree
* until u hit a condition where parent node
* is having lesser value than children
*/
8. Stop
#include <stdio.h>
#include <stdlib.h>
#define HEAPCOUNT 100
int count = 0;
data[low] = current;
34
return;
}
/*
*
35
printf("%d ", data[i]);
}
printf("\n\n");
}
int main() {
int n, i, *data, temp, ch, val; data = (int
*)malloc(sizeof(int) * HEAPCOUNT);
printf("Enter the no of elements(1-80):");
scanf("%d", &n);
36
}
printf("Sorted Data:\n");
for (i = 0; i < n; i++)
printf("%d ", data[i]);
printf("\n");
case 6:
exit(0);
break;
}
}
return 0;
}
Output:
Enter the no of elements(1-80):5
Input 1:34
Input 2:23
Input 3:46
Input 4:8
Input 5:12
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:4
Data in Binary Heap:
8 12 46 23 34
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:1
Enter your input:19
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:4
Data in Binary Heap:
8 12 19 23 34 46
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:2
8 is deleted from the heap
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:4
Data in Binary Heap:
12 23 19 46 34
37
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:3
Enter input value:27
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:4
Data in Binary Heap:
19 23 27 46 34
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:4
Data in Binary Heap:
19 23 27 46 34
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:5
Sorted Data: 46 34 27
23 19
Result:
Date:05/10/2020
AIM:
Algorithm:
1.Create a graph with getting no. of vertices and no. of edges
38
3. Implement adjacency list
39
#include<stdio.h>
max_edges,i,j,origin,destin; int
graph_type;
scanf("%d",&n);
if(graph_type==1)
max_edges = n*(n-1);
{
printf("\nInvalid vertex!\n");
40
i--;
} else
{ adj[origin][destin] = 1;
adj[destin][origin] = 1;
}/*End of for*/
printf("%4d",adj[i][j]);
printf("\n");
}/*End of main()*/
Output
Enter 1 for Undirected graph Enter
2 for Directed graph
41
Enter edge [ 7 ] ( -1 -1 to quit ) : 4 2 Enter
adjacency matrix is ::
0 1 1 1 0
0 0 0 1 0
0 0 0 1 0
0 0 0 0 1
0 0 1 0 0
Process returned 0
Result:
Aim
To create adjacency matrix of the given graph and to perform breadth first search
traversal.
Algorithm
1. Start
2. Obtain Adjacency matrix for the given graph
3. Define a Queue of size total number of vertices in the graph.
4. Select any vertex as starting point for traversal. Visit that vertex and insert it into the
Queue.
5. Visit all the adjacent vertices of the verex which is at front of the Queue which is not
visited and insert them into the Queue.
6. When there is no new vertex to be visit from the vertex at front of the Queue then
delete that vertex from the Queue.
7. Repeat step 5 and 6 until queue becomes empty.
42
8. When queue becomes Empty, then produce final spanning tree by removing unused
edges from the graph.
9. Stop
43
#include<stdio.h>
#include<stdlib.h>
int
cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
int main() { int m; printf("enterno of vertices");
scanf("%d",&n); printf("ente no of edges");
scanf("%d",&m); printf("\nEDGES \n");
for(k=1;k<=m;k++)
{
scanf("%d%d",&i,&j)
; cost[i][j]=1; } printf("enter
initial vertex");
scanf("%d",&v);
printf("Visitied vertices\n");
printf("%d ",v);
visited[v]=1; k=1;
while(k<n)
{
for(j=1;j<=n;j++) if(cost[v][j]!=0 &&
visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
printf("%d ",v); k++;
visit[v]=0; visited[v]=1;
}
}
Output: Enterno of
vertices7
enter no of edges8
EDGES
44
12
13
14
15
26
67
3 7 4 7 enter
initial vertex1
Visitied vertices 1
234567
Result:
Thus C Program for BFS is implemented successfully.
Ex. No. 9b Depth First Search Date:08/10/2020 Aim:
To create adjacency matrix of the given graph and to perform depth first search traversal.
45
Algorithm:
1. Start
2. Obtain Adjacency matrix for the given graph
3. Define a Stack of size total number of vertices in the graph.
4. Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
5. Visit any one of the adjacent vertex of the vertex which is at top of the stack which is not visited
and push it on to the stack.
6. Repeat step 5 until there are no new vertex to be visit from the vertex on top of the stack.
7. When there is no new vertex to be visit then use back tracking and pop one vertex from the stack.
8. Repeat steps 5, 6 and 7 until stack becomes Empty.
9. When stack becomes Empty, then produce final spanning tree by removing unused edges from the
graph.
10. Stop
46
#include<stdio.h>
#include<stdlib.h>
int
cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
int main() { int m; printf("enterno of
vertices"); scanf("%d",&n);
printf("ente no of edges");
scanf("%d", &m); printf("\nEDGES
\n");
for(k=1;k<=m;k++)
{
scanf("%d%d",&i,&j)
; cost[i][j]=1; } printf("enter
initial vertex");
scanf("%d",&v);
printf("ORDER OF VISITED VERTICES");
printf( "%d ",v); visited[v]=1; k=1;
while(k<n)
{ for(j=n;j>=1;j--
)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
} v=stk[--top];
printf( "%d
",v); k++;
visit[v]=0;
visited[v]=1;
}
}
Output: Enter no of
vertices 6
enter no of edges 8
EDGES
12 8
14 8
1 10
1 22
2 23
3 23
4
5
47
48
enter initial
vertex1
ORDER OF VISITED VERTICES 1 2 6 3 4
Successfully.
Exno.10(a) LINEAR
SEARCH Date:15/10/2020
Aim:
To perform linear search of an element on the given array.
Algorithm:
1. Start
2. Read number of array elements n
3. Read array elements Ai, i = 0,1,2,…n–1
4. Read search value
5. Assign 0 to found
6. Check each array element against search If Ai = search then
If found = 1
50
8. Stop
52
49
#include<stdio.h>
54
int main()
{ int a[10],item,size,pos,flag,i;
flag=1; printf("\nEnter the size
of array:"); scanf("%d",&size);
printf("\nEnter the elements:");
for(i=0;i<size;i++) scanf("%d",&a[i]);
printf("\nEnter the element to be
searched:"); scanf("%d",&item);
for(i=0;i<size;i++)
{
if(item==a[i])
{
pos=i;
flag=1;
break;
} else
continue;
} if(flag==1) printf("%d is at position
%d",item,pos+1); else printf("%d not
found",item); return 0;
}
OUTPUT:
Result:
Algorithm:
1. Start
2. Read number of array elements, say n
3. Create an array arr consisting n sorted elements
4. Get element, say key to be located
5. Assign 0 to lower and n to upper
6. While (lower < upper)
Determine middle element mid = (upper+lower)/2
If key = arr[mid] then
Print mid
Stop
Else if key > arr[mid] then
lower mid+ 1
else upper = mid – 1
while(low<=high)
{
mid=(low+high)/2;
if(item==a[mid])
{ flag=1; break;
}
else if(item<a[mid])
high=mid-1; else
low=mid+1;
}
if(flag==0)
printf("%d is not found",item);
else
56
printf("%d is at position %d",item,mid+1);
return 0;
}
OUTPUT:
Result:
Algorithm
1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Index i varies from 0 to n-2
5. Index j varies from i+1 to n-1
6. Traverse the array and compare each pair of elements
If Ai > Aj then Swap Ai
and A
7. Stop
program
#include <stdio.h>
int main()
{
int array[100], n, i, j, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter elements one by one \n");
57
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
for (i = 0 ; i < n-1; i++)
{
for (j= 0 ; j < n-1; j++)
{
if (array[j] > array[j+1]) /* For decreasing order use <
*/
{
swap = array[j]; array[j] = array[j+1];
array[j+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( i= 0 ; i< n ; i++ )
printf("%d\n", array[i]);
return 0;
}
OUTPUT :
Enter number of elements
4
Enter elements one by one
4
2
3
1
Sorted list in ascending order:
1
2
3
4
Result:
Thus a C program for the concept of bubble sort was implemented successfully
Ex no 11b Shell Sort
Date:19/10/2020 Aim:
Algorithm:
58
1− Initialize the value of h
59
#include <stdio.h> void
= array[i];
int j;
array[j] = temp;
printf("\n");
printArray(data, size);
}
Output:
Sorted Array:
60
13
56
78
Result:
Thus a C program for the concept of shell sort was implemented successfully.
ALGORITHM:
1: Start.
All elements less than the pivot must be in the first partition.
All elements greater than the pivot must be in the second partition.
4: Use recursion to sort both partitions.
5: Join the first sorted partition, the pivot, and the second sorted partition.
61
6: Stop
Program
#include<stdio.h> void
quicksort(int a[],int,int);
Int main()
{
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++) scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
62
}
}
OUTPUT:
Result: Thus a C program for the concept of Quick sort was implemented
successfully.
Exno. 12 HASHING
Date:2/11/2020
Aim:
To Write a C Program to implement Collision Resolution Techniques in
Hashing
Algorithm:
1. Create a structure, data (hash table item) with key and value as data.
2. Now create an array of structure, data of some certain size (10, in this case). But, the
size of array must be immediately updated to a prime number just greater than initial
array capacity (i.e 10, in this case).
3. A menu is displayed on the screen.
63
4. User must choose one option from four choices given in the menu
5. Perform all the operations
6. Stop
64
Program
#include<stdio.h
> int tsize; int
hasht(int key)
{ int
i;
i = key%tsize ;
return i;
}
//-------LINEAR PROBING ----- int
rehashl(int key)
{
int i ;
i = (key+1)%tsize ;
return i ;
}
//-------QUADRATIC PROBING------ int rehashq(int
key, int j)
{ int i ; i =
(key+(j*j))%tsize ;
return i ;
} int
main()
{
int key,arr[20],hash[20],i,n,s,op,j,k ;
printf ("Enter the size of the hash table: ");
scanf ("%d",&tsize);
do
{
printf("\n\n1.Linear Probing\n2.Quadratic Probing \n3.Exit \nEnter your option:
"); scanf("%d",&op);
switch(op)
{
case 1:
65
for (i=0;i<tsize;i++)
hash[i]=-1 ; for(k=0;k<n;k++)
{
key=arr[k] ; i
= hasht(key);
while (hash[i]!=-1)
{
i = rehashl(i);
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;
case 2:
for (i=0;i<tsize;i++) hash[i]=-
1;
for(k=0;k<n;k++)
{ j=1;
key=arr[k] ; i =
hasht(key); while
(hash[i]!=-1)
{ i=
rehashq(i,j); j++
;
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;
}
}while(op!=3);
}
Output:
Enter the size of the hash table: 10
Enter the number of elements: 7
Enter Elements: 12
66
23
42
11
45
38
59
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 1 The
elements in the array are:
Element at position 0: -1
Element at position 1: 11
Element at position 2: 12
Element at position 3: 23
Element at position 4: 42
Element at position 5: 45
Element at position 6: -1
Element at position 7: -1
Element at position 8: 38
Element at position 9: 59
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 2 The
elements in the array are:
Element at position 0: -1
Element at position 1: 11
Element at position 2: 12
Element at position 3: 23
Element at position 4: -1
Element at position 5: 45
Element at position 6: -1
Element at position 7: 42
Element at position 8: 38
Element at position 9: 59
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 3 Result:
Thus hashing has been performed successfully.
67