0% found this document useful (0 votes)
9 views23 pages

DSA Lab Practical File with Programs

The document is a practical file for a DSA lab at the Advanced Institute of Technology and Management, containing various programming assignments related to data structures and algorithms. It includes implementations of linear search, binary search, sorting algorithms, and data structures like queues and stacks. Each program is accompanied by its source code and expected output.

Uploaded by

mohanroy8989
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views23 pages

DSA Lab Practical File with Programs

The document is a practical file for a DSA lab at the Advanced Institute of Technology and Management, containing various programming assignments related to data structures and algorithms. It includes implementations of linear search, binary search, sorting algorithms, and data structures like queues and stacks. Each program is accompanied by its source code and expected output.

Uploaded by

mohanroy8989
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ADVANCED INSTITUTE OF TECHNOLOGY AND MANAGEMENT

(10 K.M., DELHI-MATHURA, V.P.O. AURANGABAD, TEHSIL – HODAL, DISTT. PALWAL, HARYANA -121005)

PRACTICAL FILE

DSA LAB

NAME :
FACULTY :
ROLL NO. :
COURSE :
SEM :
INDEX

[Link] PROGRAMS REMARK


1. WAP of linear search in array

2. WAP of Binary search in array

3. WAP of traversing in array

4. WAP of To find sum of array element

5. WAP of Insertion in array

6. WAP of Deletion in array

7. WAP for print fabonacci series

8. WAP for tower of Hanoi

9. WAP to implement queue using array

10. WAP for implement of stack using linked list

11. WAP for Bubble Sort

12. WAP for insertion sort

13. WAP for selection sort

14. WAP for quick sort


Program 1: WAP OF LINEAR SEARCH IN ARRAY:-
#include<stdio.h>
int main()
{
int arr[30],n,find,i;
int search = 0;
printf("enter number of element in array:\n");
scanf("%d",&n);
printf("enter element of array");
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("enter find no");
scanf("%d",&find);
for(i=0;i<n;i++){
if(arr[i]==find){
printf("%d is found at position %d",find,i+1);
search = 1;
break;
}
}
if(search == 0){
printf("enter element is not found");
}

return 0;
}

OUTPUT:-
Program 2: WAP OF INSERTION IN ARRAY:-
#include<stdio.h>
int main()
{
int a[30],size,i,pos,num;
printf("enter number of element\n");
scanf("%d",&size);
printf("enter element of array \n");
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
printf("enter postion");
scanf("%d",&pos);
printf("enter num you want to insert");
scanf("%d",&num);

for(i=size-1;i>=pos-1;i--){
a[i+1]=a[i];
}
a[pos-1] = num;

size++;

for(i=0;i<size;i++){
printf("%d\t",a[i]);
}
return 0;
}
OUTPUT:-
Program 3: WAP OF TRAVERSING IN ARRAY
#include<stdio.h>
int main()
{
int a[30],size,i;
printf("enter number of element\n");
scanf("%d",&size);
printf("enter element of array \n");
for(i=0;i<size;i++){
scanf("%d",&a[i]);
}
printf("element in array are : \n");
for(i=0;i<size;i++){
printf("%d\t",a[i]);
}
return 0;
}

OUTPUT :-
Program 4: WAP of To find sum of array element

#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;

//Calculate length of array arr


int length = sizeof(arr)/sizeof(arr[0]);

//Loop through the array to calculate sum of elements


for (int i = 0; i < length; i++) {
sum = sum + arr[i];
}
printf("Sum of all the elements of an array: %d", sum);
return 0;
}

OUTPUT :-
Program 5: WAP of Insertion in array

#include<stdio.h>
int main()
{
int arr[40],pos,i,size,value;
printf("enter no of elements in array:");
scanf("%d",&size);
printf("enter %d elements are:\n",size);
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
printf("enter the position where you want to insert the element:");
scanf("%d",&pos);

printf("enter the value into that poition:");


scanf("%d",&value);

for(i=size-1;i>=pos-1;i--)
{
arr[i+1]=arr[i];
arr[pos-1]= value;
}
printf("final array after inserting the value is\n");
for(i=0;i<=size;i++){
printf("%d\n",arr[i]);}
return 0;
}
OUTPUT :-
Program 6 : WAP of Deletion in array

#include <stdio.h>
#include <conio.h>
int main ()
{
int arr[50], pos , i , num;
printf (" \n Enter the number of elements in an array: \n ");
scanf (" %d", &num);
printf (" \n Enter %d elements in array: \n ", num);
for (i = 0; i < num; i++ )
{
scanf (" %d", &arr[i]);
}
printf( "position of the array element where you want to delete: \n ");
scanf (" %d", &pos);
if (pos >= num+1)
{
printf (" \n Deletion is not possible in the array.");
}
else
{
for (i = pos - 1; i < num -1; i++)
{
arr[i] = arr[i+1];
}
printf (" \n The resultant array is: \n");
for (i = 0; i< num - 1; i++)
{
printf (" %d \n", arr[i]);
}
}
}
OUTPUT :-
Program 7 : WAP to print fibonacci series

#include<stdio.h>
int main()
{
int n ,i,firstterm = 0, secondterm=1 , nextterm;
printf("enter the number of terms");
scanf("%d",&n);

printf("fibonacci series: %d ,%d ,",firstterm,secondterm);


for(i=3;i<=n;i++){
nextterm = firstterm + secondterm;
printf("%d, ",nextterm);
firstterm = secondterm;
secondterm = nextterm;
}
printf("\n");
return 0;
}

OUTPUT :-
Program 8 : WAP for tower of Hanoi

#include<stdio.h>
void towerOfHanoi(int n, char source, char auxiliary, char destination){
if(n==1){
printf("move disk from %c to %c\n ",source,destination);
return;
}
towerOfHanoi(n-1, source, destination, auxiliary);
printf("move disk %d from %c\n", n, source, destination);
towerOfHanoi(n-1, auxiliary, source, destination);
}
int main()
{
int n;
printf("enter the number of disk:");
scanf("%d",&n);
towerOfHanoi(n, 'A', 'B', 'C');
return 0;
}

OUTPUT :-
Program 9 : WAP to implement queue using array
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
int front = -1, rear = -1;
int queue[maxsize];
void main ()
{
int choice;
while(choice != 4){
printf("\n**********Main Menu************\n");
printf("\n=======================================\n");
printf("\[Link] an element\[Link] an element\[Link] the queue\
[Link]\n");
printf("\nEnter your choice ?");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
int item;
printf("\nEnter the element\n");
scanf("\n%d",&item);
if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");

}
void delete()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;

}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}
}

void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values ..... \n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}

OUTPUT :-
Program 10: WAP for implementation of stack using linked
list
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;

void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n \n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\[Link]\[Link]\[Link]\[Link]");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exiting. ..");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;

}
printf("Item pushed");

}
}

void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");

}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
OUTPUT :-
Program 11: WAP for Bubble sort
#include<stdio.h>
int main()
{
int array[100], n, i, j, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d Numbers:\n", n);
for(i = 0; i < n; i++)
scanf("%d", &array[i]);
for(i = 0 ; i < n - 1; i++){
for(j = 0 ; j < n-i-1; j++){
if(array[j] > array[j+1]){
swap=array[j];
array[j]=array[j+1];
array[j+1]=swap;
}
}
}
printf("Sorted Array:\n");
for(i = 0; i < n; i++)
printf("%d\t", array[i]);
return 0;
}

OUTPUT :-
Program 12 : WAP for Insertion sort
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. Please enter a positive number of elements.\n");
return 1;
}
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n - 1; i++) {
int minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
printf("Sorted array in ascending order: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT :-
Program 13 : WAP for Selection sort

#include <stdio.h>
int main()
{
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. Please enter a positive number of elements.\n");
return 1;
}
int arr[n]; // Declare the array after you know the size
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Selection sort algorithm
for (i = 0; i < n - 1; i++) {
int minIndex = i;
// Find the index of the minimum element in the remaining unsorted part
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the element at position i
if (minIndex != i) {
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
printf("Sorted array in ascending order: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT :-
Program 14 :WAP for quick sort

#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
int j;
for (j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quicksort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. Please enter a positive number of elements.\n");
return 1;
}
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
quicksort(arr, 0, n - 1);
printf("Sorted array in ascending order: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

OUTPUT :-

You might also like