DSA Practical File
DSA Practical File
DSA Practical File
Practical File
On
Affiliated to
Submitted to
Submitted by: Name: Priyanshu Kumar Dr. Rituraj Soni
University Roll No: 22EEBCS099 Assistant Professor
Department of CSE
DSA Practical File Session 2020-24
CO5 To introduce various techniques for representation of the data in the real world.
1
DSA Practical File Session 2020-24
INDEX
2
DSA Practical File Session 2020-24
Program 1: Write an C code for insertion and deletion of element in 1-D Array.
#include <stdio.h>
arr[position] = element;
return size + 1;
}
return size - 1;
}
int main() {
int array[MAX_SIZE] = {1, 2, 3, 4, 5};
int size = 5;
3
DSA Practical File Session 2020-24
displayArray(array, size);
// Insertion
size = insertElement(array, size, 10, 2);
displayArray(array, size);
// Deletion
size = deleteElement(array, size, 3);
displayArray(array, size);
return 0;
}
Output:
Array: 1 2 3 4 5
Array: 1 2 10 3 4 5
Array: 1 2 10 4 5
4
DSA Practical File Session 2020-24
#include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int a[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int b[ROWS][COLS] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int result[ROWS][COLS];
printf("Matrix A:\n");
displayMatrix(a);
printf("Matrix B:\n");
displayMatrix(b);
addMatrices(a, b, result);
return 0;
}
Output:
Matrix A:
Matrix:
123
456
789
Matrix B:
5
DSA Practical File Session 2020-24
Matrix:
987
654
321
Sum of Matrices A and B:
Matrix:
10 10 10
10 10 10
10 10 10
6
DSA Practical File Session 2020-24
#include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int a[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int b[ROWS][COLS] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int result[ROWS][COLS];
printf("Matrix A:\n");
displayMatrix(a);
printf("Matrix B:\n");
displayMatrix(b);
subtractMatrices(a, b, result);
return 0;
}
Output:
Matrix A:
Matrix:
123
456
789
Matrix B:
Matrix:
987
654
7
DSA Practical File Session 2020-24
321
Difference of Matrices A and B:
Matrix:
-8 -6 -4
-2 0 2
468
8
DSA Practical File Session 2020-24
#include <stdio.h>
void multiplyArrays2D(const int a[][COLS], const int b[][COLS], int result[][COLS], int rows, int cols) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result[i][j] = a[i][j] * b[i][j];
}
}
}
int main() {
int a[ROWS][COLS] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int result[ROWS][COLS];
printf("Matrix A:\n");
displayMatrix(a, ROWS, COLS);
printf("Matrix B:\n");
displayMatrix(b, ROWS, COLS);
return 0;
}
Output:
Matrix A:
123
456
789
9
DSA Practical File Session 2020-24
Matrix B:
10 11 12
13 14 15
16 17 18
Resultant matrix after multiplication:
10 22 36
52 70 90
112 136 162
10
DSA Practical File Session 2020-24
Program 3: Write algorithm and C program to implement for the following sorting. Also write the time
and space complexity of each algorithm.
a) Bubble Sort
Algorithm:
1. Start from the beginning of the array.
2. Compare each pair of adjacent elements.
3. If they are in the wrong order, swap them.
4. Continue this process until the entire array is sorted.
5. Repeat the process for each element in the array.
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
return 0;
}
Output:
Sorted array: 11 12 22 25 34 64 90
Time Complexity:
Best Case: O(n)
Average Case: O(n^2)
Worst Case: O(n^2)
Space Complexity:
O (1)
11
DSA Practical File Session 2020-24
b) Insertion Sort:
Algorithm:
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
Output:
Sorted array: 11 12 22 25 34 64 90
Time Complexity:
Best Case: O(n)
Average Case: O(n^2)
Worst Case: O(n^2)
Space Complexity:
O(1)
12
DSA Practical File Session 2020-24
c) Shell Sort
Algorithm:
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
shellSort(arr, n);
Output:
Sorted array: 11 12 22 25 34 64 90
Time Complexity:
Best case: O(n log n)(depends on the gap sequence)
Average Case: O(n^1.5)
Worst Case: O(n^2)
Space Complexity:
O(1)
13
DSA Practical File Session 2020-24
d) Selection Sort:
Algorithm:
#include <stdio.h>
swap(&arr[min_idx], &arr[i]);
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
Output:
Sorted array: 11 12 22 25 34 64 90
Time Complexity:
Best Case: O(n^2)
Average Case: O(n^2)
Worst Case: O(n^2)
Space Complexity:
O(1)
14
DSA Practical File Session 2020-24
e) Merge Sort:
Algorithm:
1. Divide the unsorted list into n sub-lists, each containing one element (base case).
2. Repeatedly merge sub-lists to produce new sorted sub-lists until there is only one sub-list remaining.
#include <stdio.h>
#include <stdlib.h>
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
Output:
Sorted array: 11 12 22 25 34 64 90
Time Complexity:
Best Case: O(n log n )
Average Case: O(n log n)
Worst Case: O(n log n)
Space Complexity:
O(n) (additional space for temporary arrays during merge)
16
DSA Practical File Session 2020-24
f) Quick Sort:
Algorithm:
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
Output:
Sorted array: 11 12 22 25 34 64 90
17
DSA Practical File Session 2020-24
Time Complexity:
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n^2) (when the pivot selection is poor)
Space Complexity:
18
DSA Practical File Session 2020-24
g) Heap Sort:
Algorithm:
#include <stdio.h>
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
Output:
Sorted array: 11 12 22 25 34 64 90
Time Complexity:
Space Complexity:
O(1)
20
DSA Practical File Session 2020-24
h) Radix Sort:
Algorithm:
1. Choose a digit place value (starting from the least significant digit).
2. Sort the array using stable sorting (like counting sort) based on that digit.
3. Repeat the process for each digit place value, moving from the least significant to the most significant.
#include <stdio.h>
int main() {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
radixSort(arr, n);
Output:
Time Complexity:
Space Complexity:
22
DSA Practical File Session 2020-24
i) Counting Sort:
Algorithm:
#include <stdio.h>
// Place each element at its correct position in the sorted output array
for (int i = n - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
int main() {
int arr[] = {4, 2, 2, 8, 3, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
countingSort(arr, n);
23
DSA Practical File Session 2020-24
Output:
Sorted array: 1 2 2 3 3 4 8
Time Complexity:
Space Complexity:
O(n+k)
24
DSA Practical File Session 2020-24
#include <stdio.h>
int main() {
int array[] = {2, 5, 8, 12, 16, 23, 31, 42, 50};
int size = sizeof(array) / sizeof(array[0]);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
Output:
25
DSA Practical File Session 2020-24
#include <stdio.h>
if (arr[mid] == target) {
return mid; // Element found at index mid
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
int main() {
int array[] = {2, 5, 8, 12, 16, 23, 31, 42, 50};
int size = sizeof(array) / sizeof(array[0]);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
Output:
26
DSA Practical File Session 2020-24
Program 6: Write a program in C to implement PUSH and POP in stack using arrays.
#include <stdio.h>
#define MAX_SIZE 10
struct Stack {
int arr[MAX_SIZE];
int top;
};
stack->arr[++stack->top] = value;
printf("Pushed %d to the stack.\n", value);
}
int main() {
struct Stack stack;
initializeStack(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
pop(&stack);
pop(&stack);
pop(&stack);
27
DSA Practical File Session 2020-24
return 0;
}
Output:
28
DSA Practical File Session 2020-24
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Stack {
struct Node* top;
};
newNode->data = value;
newNode->next = stack->top;
stack->top = newNode;
stack->top = poppedNode->next;
free(poppedNode);
int main() {
struct Stack stack;
initializeStack(&stack);
29
DSA Practical File Session 2020-24
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
pop(&stack);
pop(&stack);
pop(&stack);
pop(&stack); // Trying to pop from an empty stack
return 0;
}
Output:
30
DSA Practical File Session 2020-24
Program 8: Write a program for factorial calculation and Fibonacci series calculation using
recursion.
#include <stdio.h>
int main() {
// Calculate factorial
int num_factorial = 5;
printf("Factorial of %d is: %d\n", num_factorial, factorial(num_factorial));
return 0;
}
Output:
31
DSA Practical File Session 2020-24
Program 9: Write a C program to convert infix expression in postfix and prefix using stack.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stack {
int top;
char arr[MAX_SIZE];
};
stack->arr[++stack->top] = value;
}
return stack->arr[stack->top--];
}
int i, j;
i = j = 0;
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
}
postfix[j] = '\0';
}
char reversedPostfix[MAX_SIZE];
infixToPostfix(reversedInfix, reversedPostfix);
int j = 0;
for (int i = strlen(reversedPostfix) - 1; i >= 0; i--) {
prefix[j++] = reversedPostfix[i];
}
prefix[j] = '\0';
33
DSA Practical File Session 2020-24
int main() {
char infix[MAX_SIZE], postfix[MAX_SIZE], prefix[MAX_SIZE];
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
infixToPrefix(infix, prefix);
printf("Prefix expression: %s\n", prefix);
return 0;
}
Output:
34
DSA Practical File Session 2020-24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Stack {
int top;
int arr[MAX_SIZE];
};
stack->arr[++stack->top] = value;
}
return stack->arr[stack->top--];
}
switch (postfix[i]) {
case '+':
push(&stack, operand1 + operand2);
break;
case '-':
push(&stack, operand1 - operand2);
break;
case '*':
push(&stack, operand1 * operand2);
break;
case '/':
if (operand2 != 0) {
push(&stack, operand1 / operand2);
} else {
printf("Error: Division by zero.\n");
exit(EXIT_FAILURE);
}
break;
default:
printf("Invalid postfix expression.\n");
exit(EXIT_FAILURE);
}
}
}
return pop(&stack);
}
int main() {
char postfix[MAX_SIZE];
return 0;
}
Output:
36
DSA Practical File Session 2020-24
Program 11: Write a C Program for insertion and deletion in Linear Queue.
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
struct Queue {
int front, rear;
int arr[MAX_SIZE];
};
if (isEmpty(queue)) {
queue->front = 0;
queue->rear = 0;
} else {
queue->rear = (queue->rear + 1) % MAX_SIZE;
}
queue->arr[queue->rear] = value;
printf("Enqueued %d to the queue.\n", value);
}
if (queue->front == queue->rear) {
// Last element in the queue
initializeQueue(queue);
} else {
queue->front = (queue->front + 1) % MAX_SIZE;
37
DSA Practical File Session 2020-24
printf("Queue: ");
int i = queue->front;
while (i != queue->rear) {
printf("%d ", queue->arr[i]);
i = (i + 1) % MAX_SIZE;
}
printf("%d\n", queue->arr[queue->rear]);
}
int main() {
struct Queue queue;
initializeQueue(&queue);
enqueue(&queue, 10);
enqueue(&queue, 20);
enqueue(&queue, 30);
displayQueue(&queue);
dequeue(&queue);
dequeue(&queue);
dequeue(&queue);
dequeue(&queue); // Trying to dequeue from an empty queue
return 0;
}
Output:
38
DSA Practical File Session 2020-24
Program 12: Write a C program for insertion and deletion elements in Circular Queue.
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
struct CircularQueue {
int front, rear;
int arr[MAX_SIZE];
};
if (isEmpty(queue)) {
queue->front = 0;
queue->rear = 0;
} else {
queue->rear = (queue->rear + 1) % MAX_SIZE;
}
queue->arr[queue->rear] = value;
printf("Enqueued %d to the circular queue.\n", value);
}
if (queue->front == queue->rear) {
// Last element in the queue
initializeCircularQueue(queue);
} else {
queue->front = (queue->front + 1) % MAX_SIZE;
39
DSA Practical File Session 2020-24
int main() {
struct CircularQueue cQueue;
initializeCircularQueue(&cQueue);
enqueue(&cQueue, 10);
enqueue(&cQueue, 20);
enqueue(&cQueue, 30);
displayCircularQueue(&cQueue);
dequeue(&cQueue);
dequeue(&cQueue);
dequeue(&cQueue);
dequeue(&cQueue); // Trying to dequeue from an empty circular queue
return 0;
}
Output:
40
DSA Practical File Session 2020-24
Program 13: Write a program in C to implement Linear Queue using Linked List.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node* front;
struct Node* rear;
};
newNode->data = value;
newNode->next = NULL;
if (isEmpty(queue)) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
if (queue->front == queue->rear) {
41
DSA Practical File Session 2020-24
free(frontNode);
printf("Dequeued %d from the queue.\n", dequeuedValue);
return dequeuedValue;
}
printf("Queue: ");
struct Node* current = queue->front;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
struct Queue queue;
initializeQueue(&queue);
enqueue(&queue, 10);
enqueue(&queue, 20);
enqueue(&queue, 30);
displayQueue(&queue);
dequeue(&queue);
dequeue(&queue);
dequeue(&queue);
dequeue(&queue); // Trying to dequeue from an empty queue
return 0;
}
Output:
42
DSA Practical File Session 2020-24
Program 14: Write a C Program to insertion and deletion operation in single linked List.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
// Function to delete a node with a given value from the linked list
void deleteNode(struct Node** head, int value) {
if (*head == NULL) {
printf("Linked list is empty. Cannot delete.\n");
return;
}
if (current == NULL) {
43
DSA Practical File Session 2020-24
if (prev == NULL) {
// Deleting the first node
*head = current->next;
} else {
prev->next = current->next;
}
free(current);
printf("Deleted node with value %d from the linked list.\n", value);
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
displayLinkedList(head);
deleteNode(&head, 20);
displayLinkedList(head);
return 0;
}
Output:
44
DSA Practical File Session 2020-24
Program 15: Write a C Program to insertion and deletion operation in double linked List.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
// Function to delete a node with a given value from the doubly linked list
void deleteNode(struct Node** head, int value) {
if (*head == NULL) {
printf("Doubly linked list is empty. Cannot delete.\n");
return;
}
45
DSA Practical File Session 2020-24
if (current == NULL) {
printf("Value %d not found in the doubly linked list. Cannot delete.\n", value);
return;
}
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
// Deleting the first node
*head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
printf("Deleted node with value %d from the doubly linked list.\n", value);
}
// Function to display the doubly linked list in both forward and reverse order
void displayDoublyLinkedList(struct Node* head) {
printf("Doubly Linked List (Forward): ");
struct Node* current = head;
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->next;
}
printf("NULL\n");
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
displayDoublyLinkedList(head);
deleteNode(&head, 20);
displayDoublyLinkedList(head);
return 0;
}
Output:
47
DSA Practical File Session 2020-24
Program 16: Write a C program for creating, insertion, deletion and searching in
Binary search tree
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
return root;
}
return root;
}
int main() {
struct Node* root = NULL;
insert(root, 80);
return 0;
}
Output:
50
DSA Practical File Session 2020-24
#include <stdio.h>
#include <stdlib.h>
return graph;
}
int main() {
// Create a sample graph
struct Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 4);
addEdge(graph, 3, 5);
addEdge(graph, 4, 5);
return 0;
}
Output:
53
DSA Practical File Session 2020-24
#include <stdio.h>
#include <stdlib.h>
return graph;
}
int main() {
// Create a sample graph
struct Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 4);
addEdge(graph, 3, 5);
addEdge(graph, 4, 5);
return 0;
}
55
DSA Practical File Session 2020-24
Output:
56
DSA Practical File Session 2020-24
#include <stdio.h>
#include <stdlib.h>
// Function to find the set of an element using the path compression technique
int findSet(struct Subset* subsets, int i) {
if (subsets[i].parent != i) {
subsets[i].parent = findSet(subsets, subsets[i].parent); // Path compression
}
return subsets[i].parent;
}
int main() {
// Example graph represented by its edges
int numVertices = 6;
int numEdges = 9;
struct Edge edges[] = {
{0, 1, 4},
{0, 2, 4},
{1, 2, 2},
{1, 0, 4},
{2, 0, 4},
{2, 1, 2},
{2, 3, 3},
{2, 5, 2},
{3, 2, 3},
};
58
DSA Practical File Session 2020-24
return 0;
}
Output:
59
DSA Practical File Session 2020-24
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
return min_index;
}
60
DSA Practical File Session 2020-24
int main() {
// Example graph represented by an adjacency matrix
int graph[V][V] = {
{0, 2, 0, 6, 0, 0},
{2, 0, 3, 8, 5, 0},
{0, 3, 0, 0, 7, 9},
{6, 8, 0, 0, 0, 4},
{0, 5, 7, 0, 0, 1},
{0, 0, 9, 4, 1, 0}
};
return 0;
}
Output:
61