Satyug Darshan Institute of
Engineering & Technology
Faridabad
DATA STRUCTURES & ALGORITHMS
BACHELOR OF TECHNOLOGY
COMPUTER SCIENCE ENGINEERING
Submitted by: Submitted to:
Kushal lawaniya Ms. AMITA
CSE – 22/054 Asst. Prof.
(Dept. of CSE)
TABLE OF CONTENTS
S. no. The topic of the Program Date Signature
1. WAP on c to find out Average, Minimum and
Maximum Marks of a class.
2. WAP to implement insertion operation on
arrays
3. WAP to implement deletion on arrays.
4. WAP to implement Linear Search operation
on arrays.
5. WAP to implement Binary Search operations
on array.
6. WAP for Selection Sort operation on array
7. WAP for Bubble Sort operation on array
8. WAP for Stack Operation
Program number 1.
WAP on c to find out Average, Minimum and Maximum Marks of a class.
Algorithm
Step 1: Initialize the min and max = A[0]
Step 2: Use for loop in which
a. Set i = 0
b. Repeat step (2.b) to until i < n Step
3: If mark < minimum:
minimum = mark
Else mark > maximum:
maximum = mark
Step 4: Use for loop in which
a. Set i=0
b. Repeat step (4.b) to until i < n
totalMarks = totalMarks + mark
Step 5: average = totalMarks / n
Input
#include <stdio.h> int main() { int
i ,n,a[100],min,max,sum=0; float avg;
printf("Enter the no. of students:\n");
scanf("%d",&n); printf("Enter the marks of
the students:\n"); for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
min=a[0];
max=a[0];
for(i=0;i<n;i++)
sum=sum+a[i];
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
avg=sum/n;
printf("Average marks: %f\nMinimum marks: %d\nMaximum marks: %d\n",avg,min,max);
return 0;
Output
Program number 2.
WAP to implement insertion operation on arrays
Algorithm
Step 1: [initialize the value of i]
Set i = len
Step 2: Repeat for I = len to position
Step 3: [shift the element down by position
Set a [i+1] = a [i]
[end of loop]
Step 4: [insert the element at required position]
Set a [len] set len = len + 1
Step 5: Display new list of array
Step 6: Exit
Input
#include <stdio.h>
int main() {
int array[50], position, c, n, value;
printf("Enter number of elements in the array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Please enter the location where you want to insert an new element\n");
scanf("%d", &position);
printf("Please enter the value\n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c]; array[position-
1] = value;
printf("Resultant array is\n");
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);
return 0;
}
Output
Program number 3.
WAP to implement deletion on arrays.
Algorithm
Step 1: Set item = a[pos]
Step 2: Repeat for j = pos to n-1
[ shihting element 1 poistion upward]
Set a[j] = a[j+1]
End of loop
Step 3: Rest n = n-1
Step 4: Display the new list
Step 5: Exit
Input
#include <stdio.h>
int main() {
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
return 0;
}
Output
Program number 4.
WAP to implement Linear Search operation on arrays.
Algorithm
Step 1: Set pos = -1
Step 2: Set i
Step 3: Repeat step 4
while Step 4: If a [i] = val
set pos = i
print pos
Step 5: go to step 6 [exit]
[end of loop]
Set i = i + 1
Step 6: If pos = -1
Printf (“ value not found”)
Step 7: Exit
Input
#include<stdio.h>
#include<conio.h
> void main() {
int a[10], i, n, m, loc=0;
printf("Enter the size of array:");
scanf("%d",&n); printf("\n Enter
the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Array: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\nEnter the number to search:");
scanf("%d",&m); for(i=0;i<n;i+
+)
{
if(a[i]==m)
{
printf("Element %d is found at %d poistion\n",m,i);
loc=1;
}
}
}
Output
Program number 5.
WAP to implement Binary Search operations on array.
Algorithm
Step 1: [initialize segment variables]
Set BEG = LB, END = UB and
Step 2: Repeat steps 3 to 4 while BEG ≤ END
MID = INT(( BEG + END ) / 2)
And DATA [MID] ≠ ITEM
Step 3: If ITEM < DATA [MID] then;
Set END = MID – 1
Else BEG = MID + 1
End of if
Step 4: set MID = (( BEG + END) / 2)
End of step 2
Step 5 : If DATA [MID] = ITEM then
Set loc = MID
Else set loc = NULL
Step 6: Exit
Input
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,mid,low,high,val,i;
printf("Enter the no. of element in the list:\n");
scanf("%d",&n);
printf("Enter the elements in the list:\n");
for(i=0;i<n;i++) scanf("%d",&a[i]);
printf("Enter the number you want to search"); scanf("%d",&val);
low=0; high=n-
1;
mid=(low+high)/2;
while(low<=high)
{
if(a[mid]<val)
{
low=mid+1;
}
else if(a[mid]==val)
{
printf("%d found at location
%d",val,mid+1); break; } else
high=mid+1;
mid=(low+high)/2;
} if(low>high)
printf("not found");
}
Output
Program number 6.
WAP for Selection Sort operation on array
Algorithm
Step 1: Use for loop in which
a. Set i = 0
b. Repeat step (1.b) to 4 until i < n – 1
Step 2: Use one more for loop inside of step 1 loop
a. Set j = k = i
b. Repeat step (2.b) to (2.d) until j < n
c. If A [j] < A [k] , then
Set k = j
[ end of if statement]
d. Set j = j + 1
[end of step 2 loop] Step
3: Set T = A [i]
Set A [i] = A [i+1]
Set A [i+1] = T
Step 4: Set i = i + 1
[end of step 1 loop] Step
5: Exit
Input
#include<stdio.h> int
main(){
int i, j, count, temp, number[25];
printf("How many numbers u are going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=0;i<count;i++)
{ for(j=i+1;j<count;j++)
{ if(number[i]>number[j])
{ temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
printf("Sorted elements: "); for(i=0;i<count;i+
+)
printf(" %d",number[i]);
return 0;
}
Output
Program number 7.
WAP for Bubble Sort operation on array
Algorithm
Step 1: Use for loop in which
a. Set i = 1
b. Repeat step (1.b) to 6 until i < n
Step 2: Set j = i - 1
Step 3: Set x = A [i]
Step 4: Repeat step 4 for j > -1 && A[j] > x
a. Set A[j+1] = A[j]
b. Set j = j – 1
[end of step 4 loop]
Step 5: Set A [j+1] = x
Step 6: Set i = i + 1
[end of step 1 loop]
Step 7: Exit
Input
#include<stdio.h>
void main()
{
int a[10],n,i,j,temp; printf("Enter
number of element:");
scanf("%d",&n); printf("Enter the
element:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("sorted list in ascending order:\n");
for(i=0;i<n;i++) printf("%d\t",a[i]);
}
Output
Program
Program number 8.
WAP for Stack Operation
Algorithm
Push()
Let STACK(SIZE) is an array for implementing the stack, SIZE represents the maximum
size of array STACK. X is the element to be pushed in stack & TOP is the index number of the
element at the top of Stack.
Step 1: [check for Stack Overflow]
If TOP = SIZE – 1 : then
Write “ Stack Overflow” and Return
[end of if structure]
Step 2: Read X to be pushed in stack
Step 3: Set TOP = TOP + 1
Step 4: Set STACK(TOP) = X
Step 5: Exit
Pop()
Let STACK(SIZE) is an array for implementing the stack, SIZE represents the maximum
size of array STACK. X is the element to be popped from stack & TOP is the index number of
the element at the top of Stack. Step 1: [check for Stack Underflow]
If TOP = – 1 : then
Write “ Stack Underflow” and Return
[end of if structure]
Step 2: Set X = STACK(TOP)
Step 3: Write ‘Element popped from stack is :’ X
Step 4: Set TOP = TOP - 1
Step 4: Exit
Input
#include<stdio.h>
#include<stdlib.h>
# define max 20
int top=-1,s[max];
void push(int n)
{
if(top==max-1)
printf("Stack overflow");
else top=top+1;
s[top=n];
} void
pop() {
int data; if
(top==-1) {
printf("stack underflow");
}
data=s[top];
printf("popped element is %d",data);
top=top-1;
}
void display()
{
int i;
if(top==-1)
{
printf("stack is empty");
}
else
{
for(i=top;i>=top;i--)
printf("\t %d",s[i]);
}
} int main()
{ int
choice,n;
do{
printf("\n 1. push");
printf("\n 2. pop"); printf("\
n 3. display"); printf("\n 4.
exit"); printf("Enter your
choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter any element to
push"); scanf("%d",&n);
push(n); break;
case 2:
pop();
break; case 3:
display();
break; case 4:
exit(0);
break;
}
}
while(1);
return 0;
}
Output