Dsa Lab Manual Mca 22-23
Dsa Lab Manual Mca 22-23
Department of MCA
LABORATORY MANUAL
DSA Laboratory
(Subject Code: 310906)
PROGRAM OUTCOMES
PO No. Program Outcome Description
Apply knowledge of mathematics, computer science, computing specializations appropriate
PO 1 for real world applications.
Identify, formulate, analyze and solve complex computing problems using relevant domain
PO 2 disciplines.
Design and evaluate solutions for complex computing problems that meet specified needs with
appropriate considerations for real world problems.
PO 3
Find solutions of complex computing problems using design of experiments, analysis and
PO 4 interpretation of data.
Apply appropriate techniques and modern computing tools for development of complex
PO 5 computing activities.
Apply professional ethics, cyber regulations and norms of professional computing practices.
PO 6
Recognize the need to have ability to engage in independent and life-long learning in the
PO 7 broadest context of technological change.
Demonstrate knowledge and understanding of the computing and management principles and
PO 8 apply these to one’s own work, as a member and leader in a team, to manage projects and in
multidisciplinary environments.
Communicate effectively with the computing community and with society at large, such as,
PO 9 being able to comprehend and write effective reports and design documentation, make
effective presentations, and give and receive clear instructions.
Assess societal, environmental, health, safety, legal and cultural issues within local and global
contexts, and the consequent responsibilities relevant to the professional computing practices.
PO 10
Course Outcomes
Implement elementary data structures such as Arrays, linked lists
CO 1
CO 2 Implement representation & application of Linked List
EXPERIMENT NO. 01
CONCEPT:
Pattern matching is the problem of deciding whether or not a given string pattern P appears in a string text T.
We assume that the length P does not exceed the length of T.
ALGORITHM:
(Pattern Matching) P and T are the strings with lengths R and S respectively, and are stored as arrays with one
character per element. This algorithm finds the INDEX of P in T.
1. [Initialize.] Set K: =1 and MAX: = S-R+1.
2. Repeat Step 3 to 5 while K<= MAX:
3. Repeat for L=1 to R: [Tests each character of P.]
If P [L]! = T [K+L-1], then: Go to Step 5.
[End of inner loop.]
4. [Success.] Set INDEX = K, and Exit.
5. Set K: =K+1.
[End of Step 2 outer loop.]
6. [Failure.] Set INDEX =0.
7. Exit
CONCLUSION:
In this way, we can implement pattern matching algorithm.
CODE
#include<stdio.h>
#include<conio.h>
void main()
{
char P[80]={"bab"};
char T[80]={"aabbbabb"};
int R,S,K,L,MAX,INDEX;
clrscr();
R=strlen(P);
S=strlen(T);
K=1;
MAX=S-R+1;
while(K<=MAX)
{
for(L=0;L<R;L++)
if(P[L]!=T[K+L-1])
if(L==R)
{
INDEX=K;
break;
}
else
K=K+1;
}
if(K>MAX)
INDEX=-1;
printf("P=%s",P);
printf("\n\nT = %s",T);
if(INDEX!=-1)
printf("\n\nIndex of P in T is %d",INDEX);
else
printf("\n\n P does not exist in T");
getch();
OUTPUT:
EXPERIMENT NO. 02
TITLE: Write a program to search a given element in an array using Linear Search.
CONCEPT:
Suppose a linear array DATA contains n elements & suppose a specific ITEM of information is given by
user to search. We want to find location LOC of the ITEM in the linear array. If ITEM is not present in the array
then we set LOC=0 to indicate that ITEM is not present. In the linear search algorithm we compare ITEM one by
PRIOR CONCEPT:
One dimensional array, array index, loop structures.
NEW CONCEPTS:
Preposition 1: List
It is the listing of elements which are ordered or unordered.
Preposition 2: Searching
Searching is the process to find the given key element in the list with its position. The searching is also referred as
the locate operation.
Preposition 3: Result of searching
The result of searching is given as key element found or not found along with the position of the element in the list.
Preposition 4: Comparisons required
The number of Comparisons required indicates the number of element tested with key element to reach the result
ALGORITHM:
1) [Initialise] set K=1 & LOC=0
2) Repeart step 3 & 4 while LOC=0 & K<= N
3) If ITEM = DATA[K] then: set LOC = K
4) Set K= K+1 [Increment Counter]
[end of step 2 loop]
5) If LOC =0 , then:
Write : ITEM is not found in array DATA
else
Write : LOC is Location of ITEM
[end of if statement.]
6) Exit.
APPLICATION:
Linear search is usually very simple to implement, and is practical when the list has only a few elements, or
when performing a single search in an unordered list.
ADVANTAGES:
DISADVANTAGES:
• The linear search is inefficient - If the array being searched contains 20,000 elements, the algorithm will
have to look at all 20,000 elements in order to find a value in the last element. In an average case, an item is
just as likely to be found near the beginning of the array as near the end. Typically, for an array of N items,
the linear search will locate an item in N/2 attempts. If an array has 50,000 elements, the linear search will
make a comparison with 25,000 of them in a typical case. This is assuming that the search item is
consistently found in the array. N/2 is the average number of comparisons. The maximum number of
comparisons is always N.
CODE
#include<stdio.h>
#include<conio.h>
void main()
{
int i , ser , s;
int a[20];
clrscr();
printf("enter total no. of element");
scanf("%d",&s) ;
for (i=0;i<s;i++)
{
printf("enter the element");
scanf("%d",&a[i]);
}
printf("enter the element to be searched");
scanf("%d",&ser);
for(i=0;i<s;i++)
{
if(ser==a[i])
{
printf("element exist in array:%d",i+1);
break;
}
}
if(ser!=a[i])
printf("element does not exist in array");
getch();
}
OUTPUT:
TITLE: Write a program to search a given element in an array using Binary Search.
CONCEPT:
Suppose DATA is an array which is sorted in increasing numerical order or, equivalently, alphabetically.
Then there is an extremely efficient searching algorithm called Binary search, Which can be used to find the location
LOC of a given ITEM of information in DATA.
ALGORITHM:
(Binary Search) BINARY (DATA, UB, LB, ITEM, LOC)
Here DATA is sorted array with lower bound LB and upper bound UB, and IETM is a given item of
information. The variables BEG, END and MID denote respectively the beginning, end and the middle locations of a
segment of elements of DATA. This algorithm finds the location LOC of ITEM in DATA or sets LOC=NULL.
2) Repeat step 3 & 4 while BEG <= END and DATA [MID]! = ITEM
CONCLUSION:
In this way, we can search an element in an array using binary search.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,search,f=0,low,high,mid,a[20];
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the number in ascending order a[%d]=",i);
scanf("%d",&a[i]);
}
printf("Enter the search element:");
scanf("%d",&search);
low=1;
high=n;
while(low<=high)
{
mid=(low+high)/2;
if(search<a[mid])
{
high=mid-1;
}
else if(search>a[mid])
{
low=mid+1;
}
else
{
f=1;
printf("obtained in the position %d:",mid);
getch();
exit();
}
}
if(f==0)
{
printf("not present");
}
getch();
}
EXPERIMENT NO. 04
TITLE: Write a program to sort the elements in an array using Bubble Sort.
CONCEPT:
Proposition 1: List
It is the ordered list in which the elements are arranged in particular order by some criteria. The general order are
ascending order and descending order.
Proposition 2: Passes
The number of passes indicate the number of times the list is scanned to sort the list
Proposition 3: Exchanges
The number of exchanges indicates the number of elements swapped or exchanged till.
Proposition 4: Comparisons
The number of comparisons indicates the number of comparisons occurred in pass.
PRIOR CONCEPTS:
One dimensional array, loop structure, lists, comparisons.
ALGORITHM:
BUBBLE (DATA, N)
Here DATA is an array with N elements this algorithm sorts the elements in DATA.
1) Repeat steps 2 & 3 for K=1 to N-1
2) Set PTR = 1 [initialize pass pointer PTR]
3) Repeat while PTR <= N-K [execute pass]
a) If DATA[PTR] > DATA [PTR+1], then :
Interchange DATA [PTR] & DATA [PTR+1]
[end of if structure]
4) Exit.
• APPLICATION :
Among various other sorting algorithm, bubble sort algorithm is one of the popular and frequently used
algorithm to sort elements either in ascending or descending order.
• ADVANTAGES :-
Simple to write, easy to understand and it takes few lines of code. The data is sorted in place so there is
little memory overhead and once sorted, the data is in memory , ready for processing
• DISADVANTAGE :-
Bubble sort should be avoided in case of large collections. It will not be efficient in case of reverse
ordered collection.
Bubble sort is not a practical sorting algorithm when n is large.
The major advantage is the amount of time it takes to sort.
The average time increases almost exponentially as the no. of table elements increase. Ten times the no.
of items takes almost one hundred times as long to sort.
F(n) = O(n)
CONCLUSION:
In this way, we can sort the elements in an array using Bubble sort.
CODE
#include<stdio.h>
#include<conio.h>
int DATA[10]={54,64,95,21,13,24,72,66,42,32};
int n=10;
void BUBBLE();
{
int k , ptr ,temp;
for(k=1; k<=(n-1);k++)
{
for(ptr=0 ; ptr<=(n-k-1); ptr++)
{
if (DATA[ptr]> DATA[ptr+1])
{
temp=DATA[ptr];
DATA[ptr]=DATA[ptr+1];
DATA[ptr+1]=temp;
}
}
}
}
OUTPUT:
TITLE: Write a program to implement Linked list and perform operations like traversing a linked list.
OBJECTIVE: To implement the data structure singly linked list and perform operation of traversing and searching a
list.
CONCEPT:
The linked list is the ordered list in which each element is represented by node with explicit address of next element.
The use of link or pointer to refer to element of list implies that element, which are logically adjacent, need not to be
physically adjacent in the memory.
In this program we will traverse and search in the linked list.
ALGORITHM
(Traversing a Linked List) Let LIST be a linked list in memory. This algorithm traverses LIST, applying an
operation PROCESS to each element of LIST. The variable PTR points to the node currently being processed.
CONCLUSION:
In this way, we can perform operations like traversing in linked list.
CODE
#include<stdio.h>
#include<conio.h>
int LIST[20];
int LINK[20];
int START;
void main()
{
int PTR;
clrscr();
LIST[0]=22;LIST[2]=5;LIST[3]=19;LIST[5]=87;LIST[7]=29;LIST[8]=79;
LIST[9]=33;LIST[11]=2;LIST[13]=50;LIST[14]=8;LIST[16]=55;LIST[18]=99;
LINK[0]=3;LINK[2]=13;LINK[3]=2;LINK[5]=8;LINK[7]=14;LINK[8]=9;
LINK[9]=18;LINK[11]=16;LINK[13]=5;LINK[14]=-1;LINK[16]=0;LINK[18]=7;
PTR=START;
printf("\n\n LIST After Traversal: \n");
while(PTR!=-1)
{
printf("%d\t",LIST[PTR]);
PTR=LINK[PTR];
}
getch();
}
OUTPUT:
EXPERIMENT NO 6.
OBJECTIVE: To implement abstract data type Stack with two operations push and pop.
CONCEPT:
A stack is a last in first out (LIFO) abstract data type and data structure. A stack can have any abstract data
type as an element, but is characterized by only two fundamental operations, the push and the pop.
The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack
if it is empty. The pop operation removes an item from the top of the list, and returns this value to the caller. A pop
either reveals previously concealed items, or results in an empty list.
ALGORITHM:
PUSH (STACK, TOP, MAXSTK, ITEM)
This procedure pushes an ITEM onto a stack.
1. [Stack already filled?]
CONCLUSION:
In this way, we can implement abstract data type stack.
CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 10
struct stack
{
int arr[MAX];
int top;
};
EXPERIMENT NO. 07
TITLE: Write a program to implement Tower of Hanoi problem for n number of disks.
CONCEPT:
Tower of Hanoi is an example of recursion that can be used as tool in developing algorithm to solve a
particular problem.
Suppose three pegs labeled A, B and C are given and suppose on peg A there are placed a finite number n of
disks of decreasing size. The object of the game is to move the disks from peg A to peg C using peg B as an
auxiliary. The rules are as follows:
a. Only one disk may be moved at a time. Specifically only the top disks on any peg may be moved to any
other peg.
b. At no time can a larger disk be placed on a smaller disk.
ALGORITHM:
TOWER (N, BEG, AUX, END)
This procedure gives a recursive solution to the Tower of Hanoi problem for N disks.
1. If N=1 , then:
a. Write: BEG-> END.
b. Return.
[End of if structure.]
2. [Move N-1 disks from peg BEG to peg AUX.]
Call TOWER (N-1, BEG, END, AUX).
3. Write: BEG-> END.
4. [Move N-1 disks from peg AUX to peg END.]
Call TOWER (N-1, AUX, BEG, END).
5. Return.
CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void hanoi(int x, char from,char to,char aux)
{
if(x==1)
{
printf("Move Disk From %c to %c\n",from,to);
}
else
{
hanoi(x-1,from,aux,to);
printf("Move Disk From %c to %c\n",from,to);
hanoi(x-1,aux,to,from);
}
}
void main()
{
int disk;
int moves;
clrscr();
printf("Enter the number of disks you want to play with:");
scanf("%d",&disk);
moves=pow(2,disk)-1;
printf("\nThe No of moves required is=%d \n",moves);
hanoi(disk,'A','C','B');
getch();
}
EXPERIMENT NO. 08
TITLE: Write a program to sort the elements in an array using Selection Sort.
CONCEPT:
Proposition 1: Sorted list
It is the ordered list in which the elements are arranged in particular order by some criteria. The general order are
ascending order and descending order.
Proposition 2: Passes
The number of passes indicate the number of times the list is scanned to sort the list
Proposition 3: Exchanges
The number of exchanges indicates the number of elements swapped or exchanged till.
ALGORITHM:
ADVANTAGES:-
Extremely fast
DISADVANTAGE:-
Very complex, massively recursive
CONCLUSION:
In this way, we can sort the elements in an array using selection sort.
CODE
#include <stdio.h>
#include<conio.h>
Void main ()
{
int A[20], N, Temp, i, j;
printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
scanf("%d",&N);
printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
for(i=1; i<=N; i++)
{
scanf("\n\t\t%d", &A[i]);
}
for(i=1; i<=N-1; i++)
for(j=i+1; j<=N;j++)
OUTPUT:
6 Pizza parlour accepting maximum M orders. Orders are served in first come first served basis. Order
once placed cannot be cancelled. Write C/C++ program to simulate the system using circular queue
using array.
7 Represent graph using adjacency list/adjacency matrix and perform Depth First Search.
8 Create binary tree. Find height of the tree and print leaf nodes. Find mirror image, print original and
mirror image using level-wise printing.
9 Write C/C++ program to store first year percentage of students in array. Write function for sorting
array of floating-point numbers in ascending order using Insertion Sort.
12 Write a C/C++ program to implement Kruskal minimum cost spanning tree algorithm.