Dsa Files
Dsa Files
Dsa Files
2CSE-B
JMIT Radaur
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];
getch();
OUTPUT:
#include<conio.h>
int i;
for(i=0;i<n;i++)
if(arr[i]==key)
return i;
return -1;
int main()
int key=40;
int n =sizeof(arr)/sizeof(arr[0]);
int result=findElement(arr,n,key);
clrscr();
if(result==-1)
else
OUTPUT:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
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++)
printf("%3d",arr[i]);
printf("\n");
insert=40;
pos=5;
n++;
for(i=n;i>=pos;i--)
arr[i]=arr[i-1];
for(i=0;i<n;i++)
printf("%3d",arr[i]);
getch();
OUTPUT:
#include<conio.h>
if (pos == - 1)
return size;
int i;
return size - 1;
int i;
if (array[i] == keyToBeDeleted)
return i;
return - 1;
int main()
printf("%d ",array[i]);
return 0;
// 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]);
OUTPUT:
#include<conio.h>
// swapping
int temp;
temp=*xp;
*xp=*yp;
*yp=temp;
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();
OUTPUT:
OUTPUT:
#include<process.h>
#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");
scanf("%d",&ch);
switch(ch)
case 1:push();
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
void push()
int value;
if(top==MAX-1)
printf("\nstack is full.");
else
scanf("%d",&value);
top=top+1;
stack[top]=value;
void pop()
if(top==-1)
else
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];
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 2;
return 0;
int main()
char exp[100];
char *e, x;
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c ",*e);
push(*e);
else
printf("%c ",pop());
push(*e);
e++;
while(top != -1)
printf("%c ",pop());
OUTPUT:
#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;
scanf("%s",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;
#include<conio.h>
*x = *y;
*y = temp;
i++;
swap(&array[i], &array[j]);
return (i + 1);
int i;
int main()
display(array, size);
return 0;
OUTPUT:
#include<stdio.h>
#include<conio.h>
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
i = 0;
j = 0;
k = l;
i++;
else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
if (l < r)
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int i;
printf("\n");
int main()
printArray(arr, arr_size);
printArray(arr, arr_size);
return 0;
OUTPUT:
#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("4.Quit \n");
scanf("%d", &choice);
switch(choice)
case 1:
insert();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
void insert()
int item;
else
if(front== - 1)
front = 0;
scanf("%d", &item);
rear = rear + 1;
queue_array[rear] = item;
void deletion()
return;
else
front = front + 1;
void display()
int i;
if(front == - 1)
else
printf("Queue is : \n");
printf("\n");
#include<stdio.h>
#include<conio.h>
# define MAX 50
void insert();
void display();
int cqueue_arr[MAX];
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 ;
if(front == rear)
front = -1;
rear=-1;
if(front == MAX-1)
front = 0;
else
front = front+1;
if(front == -1)
printf("Queue is emptyn");
return;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
front_pos = 0;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
printf("\n");
int main()
int choice,item;
while (1)
printf("4.Exit \n");
scanf("%d", &choice);
switch (choice)
case 1:
scanf("%d", &item);
insert(item);
break;
case 2:
display();
break;
case 3:
deletion();
break;
default:
while(choice !=4);
OUTPUT: