Dsa Files
Dsa Files
DSA
PRACTICAL FILE
2CSE-B
INDEX
Sr. Experiment Title Page Date Signature
No. No.
1 Write a program to find smallest and largest
element in array.
#include<stdio.h>
#include<conio.h>
int main() {
int a[100],i,n,lar,sma;
clrscr();
scanf("%d",&n);
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);
getch(); }
OUTPUT:
PROGRAM 2
int i;
for(i=0;i<n;i++)
if(arr[i]==key)
1; } int main() {
key=40;
int n =sizeof(arr)/sizeof(arr[0]);
int result=findElement(arr,n,key);
clrscr(); if(result==-1)
OUTPUT:
PROGRAM 3
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
OUTPUT:
#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");
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
if (pos == - 1)
return size;
int i;
int i;
for (i
= 0; i
<
size;
i++)
if
(array
[i] ==
keyTo
BeDel
return
i;
return
- 1;
} int
main() {
}
OUTPUT:
// 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:
#include<conio.h> //
*xp, int*yp)
{ int temp;
temp=*xp;
*xp=*yp;
*yp=temp;
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]);
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();
PROGRAM 8
OUTPUT:
PROGRAM 9
#include<process.h>
1,stack[MAX]; void
while(1) {
printf(".**stack menu*:\n");
printf("\n\n1.Push\n2.Pop\n3.Display\n.4.Exit");
} } } void
push() { int
value;
if(top==MAX-1)
{ printf("\nstack is
full.");
elements:");
scanf("%d",&value);
top=top+1; stack[top]=value;
element:"); top=top-1; } }
void display() {
int i;
if(top==-1) {
printf("stack is empty");
} else {
} printf("\nstack is:");
for(i=top;i>=0;--i) {
printf("%d ",stack[i]);
#include<ctype.h>
char stack[100];
push(char x)
stack[++top] = x;
pop() {
if(top == -1)
return stack[top--];
int priority(char x)
{ if(x == '(')
|| x == '-') return
1; if(x == '*' || x ==
'/') return 2;
{ 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);
if(*e == ')') {
while((x = pop()) !=
'(')
else
e++;
while(top != -1)
printf("%c ",pop());
OUTPUT:
#include<conio.h>
#include<ctype.h>
top=-1; void
push(int x)
stack[++top]=x;
} int
pop()
return stack[top--];
} int
main()
expression:"); scanf("%s",exp);
e=exp;
while(*e !='\0')
if(isdigit(*e))
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;
case '/':
n3=n2/n1;
break;
push(n3);
e++; }
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;
i++;
swap(&array[i], &array[j]);
}
}
return (i + 1);
{ if (low <
high)
} }
", array[i]);
} int
main() {
return 0;
OUTPUT:
#include<stdio.h> #include<conio.h>
{ int i, j, k; int n1 =
m - l + 1; int n2 = r -
= 0; j = 0; k = l;
{
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++;
} }
int m = l + (r - l) / 2;
} }
printArray(arr, arr_size);
OUTPUT:
#define MAX 50
deletion(); void
display(); int
queue_array[MAX];
int rear = - 1;
int front = - 1;
int main() {
int choice;
while (1) {
printf("4.Quit \n");
scanf("%d", &choice);
switch(choice) { case 1:
exit(1); default:
} } } void
insert() { int
item; if(rear
== MAX -
1)
printf("Que
ue
Overflow
\n"); else {
if(front== -
1) front = 0;
queue_array[rear] = item;
} } void
deletion()
return; } else {
= front + 1;
} } void
display() {
int i; if(front == - 1)
else {
printf("\n");
OUTPUT:
#include<stdio.h>
#include<conio.h> #
define MAX 50
display(); int
cqueue_arr[MAX]; int
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 ; }
== rear)
1; rear=-
1;
}
else { if(front ==
MAX-1) front = 0;
else front =
front+1;
} } void display() //
display
rear_pos)
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
front_pos++; } front_pos = 0;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
} }
printf("\n"); }
choice,item;
(1)
(choice)
case 1:
break; case 2:
break;
case 3:
deletion();
break;
default:
while(choice !=4);
}
OUTPUT: