SETH JAI PRAKASH MUKAND LAL INSTITUTE
OF ENGINEERING AND TECHNOLOGY
DSA
PRACTICAL FILE
Bachelor of Technology (B.Tech) in CSE Kurukshetra
University Kurukshetra
SUBMITTED BY: SUBMITTED TO:
Chhavi Kamboj ER.MEENAKSHI
1222040 (A.P., CSE DEPT.)
2CSE-B
Department of Computer Science & Engineering
JMIT Radaur
INDEX
Sr. Experiment Title Page Date Signature
No. No.
1 Write a program to find smallest and largest
element in array.
2 Write a program to implement linear search.
3 Write a program to implement Binary search.
4 Write a program to insert an element in an array.
5 Write a program to delete element in an array.
6 Write a program to implement Insertion Sort.
7 Write a program to implement Bubble Sort.
8 Write a program to implement Selection Sort.
9 Write a program to implement stack and its
operations.
Department: Computer Science & Engineering [1222040]
10 Program to convert Infix to Postfix expression.
11 Program for the Evaluation of Postfix expression.
12 Program to implement Quick Sort.
13 Program to implement Merge Sort.
14 Program to implement Linear Queue and its
Operations.
15 Program to implement Circular Queue and its
operations.
Department: Computer Science & Engineering [1222040]
PROGRAM 1
Write a program to find smallest and largest element in array.
// LARGEST AND SMALLEST ELEMENTS IN ARRAY
#include<stdio.h>
#include<conio.h>
int main() {
int a[100],i,n,lar,sma;
clrscr();
printf("the total number of elements:");
scanf("%d",&n);
printf("\nelements in array are:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
} lar=sma=a[0];
for(i=1;i<n;i++)
if(a[i]>lar)
lar=a[i];
if(a[i]<sma)
sma=a[i];
}
Department: Computer Science & Engineering [1222040]
printf("\n the larger no is %d",lar);
printf("\n the smaller no is %d",sma);
getch(); }
OUTPUT:
PROGRAM 2
Write a program to implement linear search
#include<stdio.h> #include<conio.h>
findElement(int arr[], int n,int key)
int i;
for(i=0;i<n;i++)
if(arr[i]==key)
Department: Computer Science & Engineering [1222040]
return i; return -
1; } int main() {
int arr[]={12,4,15, 34, 40}; int
key=40;
int n =sizeof(arr)/sizeof(arr[0]);
int result=findElement(arr,n,key);
clrscr(); if(result==-1)
printf("the element is not present:"); else
printf("element %d is at index %d",key,result);
getch(); }
OUTPUT:
PROGRAM 3
Write a program to implement Binary search
// program for binary search
#include <stdio.h> int main() { int c, first,
last, middle, n, search, array[100];
Department: Computer Science & Engineering [1222040]
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) { if (array[middle] < search)
first = middle + 1; else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
} if (first > last) printf("Not found! %d isn't present in
the list.\n", search);
return 0;
}
OUTPUT:
Department: Computer Science & Engineering [1222040]
PROGRAM 4
Write a program to insert an element in an array.
// INSERTION IN ARRAY
#include<stdio.h>
#include<conio.h>
int main() {
int arr[100]={1,2,3,4,5,6,7,8,9,10};
int i, insert,pos,n=10;
for(i=0;i<10;i++) arr[i]=i+1;
for(i=0;i<10;i++)
Department: Computer Science & Engineering [1222040]
printf("%3d",arr[i]); printf("\n");
insert=40; pos=5; n++;
for(i=n;i>=pos;i--)
arr[i]=arr[i-1];
}
arr[pos-1]=insert;
for(i=0;i<n;i++) {
printf("%3d",arr[i]);
} getch(); }
OUTPUT:
PROGRAM 5
Write a program to delete element in an array.
#include<stdio.h> #include<conio.h>
// Function to delete an element in array
int findElement(int array[], int size, int keyToBeDeleted); int
deleteElement(int array[], int size, int keyToBeDeleted)
Department: Computer Science & Engineering [1222040]
int pos = findElement(array, size, keyToBeDeleted);
if (pos == - 1)
printf("Element not found");
return size;
int i;
for (i = pos; i < size - 1; i++)
array[i] = array[i + 1]; return
size - 1;
int findElement(int array[], int size, int keyToBeDeleted)
int i;
for (i
= 0; i
<
size;
i++)
if
(array
[i] ==
keyTo
BeDel
Department: Computer Science & Engineering [1222040]
eted)
return
i;
return
- 1;
} int
main() {
int array[] = { 31, 27, 3, 54, 67, 31 }; int
size = sizeof(array) / sizeof(array[0]); int
i, keyToBeDeleted = 67; printf("n
Before Deletion: "); for (i = 0; i < size;
i++) printf("%d ", array[i]);
// Calling the function to delete an element from the array
size = deleteElement(array, size, keyToBeDeleted);
printf("n After Deletion: "); for (i = 0; i < size; i++)
printf("%d ",array[i]); return 0;
}
OUTPUT:
Department: Computer Science & Engineering [1222040]
PROGRAM 6
Write a program to implement Insertion Sort.
// program for insertion sort : find the smallest element from the array and check it
with previous element. #include<stdio.h> #include<conio.h> int main()
{ int i,j,s,temp,a[20];
printf("enter the total elements:");
scanf("%d",&s);
printf("\ninput the element:");
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)) // j is the previous elemt of i
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\nAfter sorting:");
for(i=0;i<s;i++)
{
printf("\t%d",a[i]);
}
return 0;
}
OUTPUT:
Department: Computer Science & Engineering [1222040]
PROGRAM 7
Write a program to implement Bubble Sort.
#include<stdio.h>
#include<conio.h> //
swapping void swap(int
*xp, int*yp)
{ int temp;
temp=*xp;
*xp=*yp;
*yp=temp;
Department: Computer Science & Engineering [1222040]
}
// bubble sort implementation void
BubbleSort(int arr[],int n)
int i,j;
for(i=0;i<n-1;i++) for(j=0;j<n-i-
1;j++) if(arr[j]>arr[j+1])
swap(&arr[j],&arr[j+1]);
// to print the array
void printElement(int arr[],int n)
int i;
for(i=0;i<n;i++)
printf("%3d",arr[i]);
printf("\n"); } int
main() {
int arr[]={64,34,25,12,22,11,90};
int n =sizeof(arr)/sizeof(arr[0]);
BubbleSort(arr,n); printf("Sorted
array:\n"); printElement(arr,n);
getch();
Department: Computer Science & Engineering [1222040]
OUTPUT:
PROGRAM 8
Write a program to implement Selection Sort.
#include<stdio.h>
#include<conio.h> void
swap(int *xp, int *yp)
{ int
temp=*xp;
*xp=*yp;
*yp=temp;
} void selectionSort(int arr[], int
n)
{ int i , j ,
min_idx;
for(i=0;i<n-1;i++)
{ min_idx=i;
for(j=i+1;j<n;j++)
if(arr[j]<arr[min_idx])
min_idx=j;
swap(&arr[min_idx],&arr[i]);
Department: Computer Science & Engineering [1222040]
}} void printArray(int arr[],
int n)
{
int i;
for(i=0;i<n;i++)
printf("%3d",arr[i]); printf("\n");
} void main() { int
arr[]={12,34,56,10,16}; int n =
sizeof(arr)/ sizeof(arr[0]);
selectionSort(arr,n); printf("
sorted array:");
printArray(arr,n); getch(); }
OUTPUT:
PROGRAM 9
Write a program to implement stack and its operations.
#include<stdio.h>
#include<process.h>
Department: Computer Science & Engineering [1222040]
#include<stdlib.h> #
define MAX 5 int top=-
1,stack[MAX]; void
push(); void pop();
void display(); int
main() { int ch;
while(1) {
printf(".**stack menu*:\n");
printf("\n\n1.Push\n2.Pop\n3.Display\n.4.Exit");
printf("\n\nenter yout choice[1-4]:");
scanf("%d",&ch); switch(ch) { case 1:push();
break; case 2:pop(); break; case
3:display(); break; case 4:exit(0);
default:printf("\n wrong choice");
} } } void
push() { int
value;
if(top==MAX-1)
{ printf("\nstack is
full.");
} else { printf("\nenter the
elements:");
scanf("%d",&value);
top=top+1; stack[top]=value;
Department: Computer Science & Engineering [1222040]
} } void pop() { if(top==-1) {
printf("\nthe stack is empty:");
} else { printf("\ndelete the
element:"); top=top-1; } }
void display() {
int i;
if(top==-1) {
printf("stack is empty");
} else {
printf("the stack is full");
} printf("\nstack is:");
for(i=top;i>=0;--i) {
printf("%d ",stack[i]);
Department: Computer Science & Engineering [1222040]
PROGRAM 10
Program to convert Infix to Postfix expression
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1; void
push(char x)
stack[++top] = x;
Department: Computer Science & Engineering [1222040]
} char
pop() {
if(top == -1)
return -1; else
return stack[top--];
int priority(char x)
{ if(x == '(')
return 0; if(x == '+'
|| x == '-') return
1; if(x == '*' || x ==
'/') return 2;
return 0; } int main()
{ char exp[100];
char *e, x;
printf("Enter the
expression : ");
scanf("%s",exp);
printf("\n"); e=
exp; while(*e !=
'\0') {
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
Department: Computer Science & Engineering [1222040]
push(*e); else
if(*e == ')') {
while((x = pop()) !=
'(')
printf("%c ", x);
else
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop()); push(*e); }
e++;
while(top != -1)
printf("%c ",pop());
OUTPUT:
Department: Computer Science & Engineering [1222040]
PROGRAM 11
Program for the Evaluation of Postfix expression
#include<stdio.h> // implement to evaluation of postfix expression
#include<conio.h>
#include<ctype.h>
int stack[20]; int
top=-1; void
push(int x)
stack[++top]=x;
} int
pop()
return stack[top--];
} int
main()
char exp[20]; char *e;
int n1,n2,n3,num; printf("enter the
expression:"); scanf("%s",exp);
e=exp;
while(*e !='\0')
if(isdigit(*e))
Department: Computer Science & Engineering [1222040]
{
num=*e-48;
push(num);
else
{
n1=pop();
n2=pop();
switch(*e)
case '+':
n3=n1+n2;
break;
case'-':
n3=n2-n1;
break;
}
case '*':
n3=n1*n1;
break;
Department: Computer Science & Engineering [1222040]
}
case '/':
n3=n2/n1;
break;
push(n3);
e++; }
printf("\nthe result of expression %s=%d\n\n",exp,pop());
return 0;
}
OUTPUT:
PROGRAM 12
Department: Computer Science & Engineering [1222040]
Program to implement Quick Sort
#include<stdio.h>
#include<conio.h> void
swap(int* x, int* y)
{ int temp =
*x; *x = *y;
*y = temp;
int partition (int array[], int low, int high)
int pivot = array[high];
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
if (array[j] < pivot)
{
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
Department: Computer Science & Engineering [1222040]
void quickSort(int array[], int low, int high)
{ if (low <
high)
int indexPI = partition(array, low, high);
quickSort(array, low, indexPI - 1);
quickSort(array, indexPI + 1, high);
} }
void display(int array[], int size)
{ int i; for (i=0; i <
size; i++) printf("%d
", array[i]);
} int
main() {
int array[] = {70, 90, 10, 30, 50, 20, 60};
int size = sizeof(array)/sizeof(array[0]);
quickSort(array, 0, size-1); printf("Array
after Quick Sorting: "); display(array, size);
return 0;
OUTPUT:
Department: Computer Science & Engineering [1222040]
PROGRAM 13
Program to implement Merge Sort
// Merge Sort
#include<stdio.h> #include<conio.h>
void merge(int arr[], int l, int m, int r)
{ int i, j, k; int n1 =
m - l + 1; int n2 = r -
m; int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i]; for (j
= 0; j < n2; j++)
R[j] = arr[m + 1 + j]; i
= 0; j = 0; k = l;
while (i < n1 && j < n2)
{
Department: Computer Science & Engineering [1222040]
if (L[i] <= R[j])
{
arr[k] = L[i];
i++; }
else { arr[k]
= R[j]; j++;
} k++;
} while (i
< n1)
arr[k] = L[i];
i++; k++;
} while (j <
n2)
arr[k] = R[j];
j++; k++;
} }
void mergeSort(int arr[], int l, int r) { if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m); mergeSort(arr,
m + 1, r); merge(arr, l, m, r);
} }
void printArray(int A[], int size)
Department: Computer Science & Engineering [1222040]
{ int i; for (i = 0; i
< size; i++)
printf("%d ", A[i]);
printf("\n"); } int main()
int arr[] = { 12, 11, 13, 5, 6, 7 }; int
arr_size = sizeof(arr) / sizeof(arr[0]);
printf("Given array is \n");
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size); return 0;
OUTPUT:
Department: Computer Science & Engineering [1222040]
PROGRAM 14
Program to implement Linear Queue and its Operations
// linear queue
#include <stdio.h> #include<stdlib.h>
#define MAX 50
void insert(); void
deletion(); void
display(); int
queue_array[MAX];
int rear = - 1;
int front = - 1;
int main() {
int choice;
while (1) {
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice) { case 1:
insert(); break; case 2:
Department: Computer Science & Engineering [1222040]
deletion(); break; case 3:
display(); break; case 4:
exit(1); default:
printf("Wrong choice \n");
} } } void
insert() { int
item; if(rear
== MAX -
1)
printf("Que
ue
Overflow
\n"); else {
if(front== -
1) front = 0;
printf("Inset the element in queue : ");
scanf("%d", &item); rear = rear + 1;
queue_array[rear] = item;
} } void
deletion()
if(front == - 1 || front > rear)
Department: Computer Science & Engineering [1222040]
printf("Queue Underflow \n");
return; } else {
printf("Element deleted from queue is : %d\n", queue_array[front]); front
= front + 1;
} } void
display() {
int i; if(front == - 1)
printf("Queue is empty \n");
else {
printf("Queue is : \n"); for(i =
front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
OUTPUT:
Department: Computer Science & Engineering [1222040]
PROGRAM 15
Program to implement Circular Queue and its operations.
// Circular queue
#include<stdio.h>
#include<conio.h> #
define MAX 50
void insert(); void
display(); int
cqueue_arr[MAX]; int
front = -1; int rear = -
1;
void insert(int item) // inserting element
if((front == 0 && rear == MAX-1) || (front == rear+1))
Department: Computer Science & Engineering [1222040]
{
printf("Queue Overflow n");
return; } if(front == -1)
{ front =
0; rear =
0; } else
{ if(rear
==
MAX-1)
rear = 0;
else rear
= rear+1;
cqueue_arr[rear] = item ;
} void
deletion()
{ if(front == -
1)
printf("Queue Underflown");
return ; }
printf("Element deleted from queue is : %d\n",cqueue_arr[front]); if(front
== rear)
Department: Computer Science & Engineering [1222040]
{ front = -
1; rear=-
1;
}
else { if(front ==
MAX-1) front = 0;
else front =
front+1;
} } void display() //
display
int front_pos = front,rear_pos = rear;
if(front == -1) { printf("Queue is
emptyn"); return; } printf("Queue
elements :\n"); if( front_pos <=
rear_pos ) while(front_pos <=
rear_pos)
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
Department: Computer Science & Engineering [1222040]
printf("%d ",cqueue_arr[front_pos]);
front_pos++; } front_pos = 0;
while(front_pos <= rear_pos)
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
} }
printf("\n"); }
int main() { int
choice,item;
printf("Input the element for insertion in queue : "); while
(1)
printf("1.Insert element to queue \n");
printf("2.Display all elements of queue \n");
printf("3.deletion of elements \n");
printf("4.Exit \n"); printf("Enter your choice
: "); scanf("%d", &choice); switch
(choice)
case 1:
printf("Input the element for insertion in queue :
"); scanf("%d", &item); insert(item);
break; case 2:
Department: Computer Science & Engineering [1222040]
display();
break;
case 3:
deletion();
break;
default:
printf("Wrong choice \n");
while(choice !=4);
}
OUTPUT:
Department: Computer Science & Engineering [1222040]
Department: Computer Science & Engineering [1222040]