0% found this document useful (0 votes)
5 views

DSA FILE -

This document is a lab file for the DSA Structures Lab (ES-152) submitted by Vihaan Sood as part of the Bachelor of Technology in Computer Science Engineering – AI program. It includes a list of assignments with their respective dates and aims, along with C programming code examples for various data structure operations such as array manipulation, stack operations, queue implementations, and sorting algorithms. The document serves as a record of the practical work completed in the lab.

Uploaded by

Kartik Rawat
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)
5 views

DSA FILE -

This document is a lab file for the DSA Structures Lab (ES-152) submitted by Vihaan Sood as part of the Bachelor of Technology in Computer Science Engineering – AI program. It includes a list of assignments with their respective dates and aims, along with C programming code examples for various data structure operations such as array manipulation, stack operations, queue implementations, and sorting algorithms. The document serves as a record of the practical work completed in the lab.

Uploaded by

Kartik Rawat
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
You are on page 1/ 60

A

Lab file of

DSA Structures Lab


(ES-152)

Submitted by

VIHAAN SOOD
Enrollment No. 00416449724

Submitted to

Dr. Arshi Hussain Ma’am


University School Of Information, Communication and Technology

Guru Gobind Singh Indraprastha University (GGSIPU)

In

Partial fulfillment of the requirements for the award of the degree of

Bachelor Of Technology

In

Computer Science Engineering – AI


S.No Assignment Details Date
1. Display and traverse 20-01-2025
array elements
2. Addition, Subtraction and 27-01-2025
Multiplication of the two
arrays
3. Stack operation using 3-02-2025
arrays
4. Matrix and square matrix 10-02-2025
check
5. Square matrix and 11-02-2025
display computed sum
6. Simple queue using array 17-02-2025
and linked list
7. Implement circular 17-02-2025
queue using array
8. Infix, postfix and prefix 3-03-2025
using stacks
9. Implement binary search 11-03-2025
in array
10. Construct a B-Tree of 1-04-2025
order 3 by inserting keys
11. Linear and binary search 7-04-2025
in array
12. Implement bubble and 15-04-2025
insertion sort on an array
13. Implement quick sort 21-04-2025
programs using array
14. Implement merge sort 21-04-2025
programs using arrays
15. Implement BFS and DFS 21-04-2025
on a graph using matrix
16. Implement selection sort 21-04-2025
in an array in ascending
order
17. Implement Kruskal’s and 21-04-2025
Prim’s algorithms for
MST
Aim: WAP in C to display and traverse array elements

ASSIGNMENT 1:
#include <stdio.h>

int main()
{
int Array[]={0,2,4,6,8};

printf("ARRAY ELEMENTS: ");

for(int i=0;i<5;i++)
{
printf("%d ",Array[i]);
}

printf("\n");

return 0;

OUTPUT:
Aim: Write a program to perform the following
operations on two 2D arrays: Addition of the two
arrays. Subtraction of the second array from the first
array. Multiplication of the two arrays (element-wise
and matrix multiplication)

ASSIGNMENT 2:
#include <stdio.h>

#define SIZE 3

void print_matrix(int mat[SIZE][SIZE]) {


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
printf("\n");
}

void add_matrices(int a1[SIZE][SIZE], int a2[SIZE][SIZE], int vihaan_sood[SIZE][SIZE]) {


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
vihaan_sood[i][j] = a1[i][j] + a2[i][j];
}
}
}

void subtract_matrices(int a1[SIZE][SIZE], int a2[SIZE][SIZE], int vihaan_sood[SIZE][SIZE]) {


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
vihaan_sood[i][j] = a1[i][j] - a2[i][j];
}
}
}

void elementwise_multiply(int a1[SIZE][SIZE], int a2[SIZE][SIZE], int vihaan_sood[SIZE][SIZE]) {


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
vihaan_sood[i][j] = a1[i][j] * a2[i][j];
}
}
}

void matrix_multiply(int a1[SIZE][SIZE], int a2[SIZE][SIZE], int vihaan_sood[SIZE][SIZE]) {


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
vihaan_sood[i][j] = 0;
for (int k = 0; k < SIZE; k++) {
vihaan_sood[i][j] += a1[i][k] * a2[k][j];
}
}
}
}

int main() {
int a1[SIZE][SIZE] = {{2, 4, 6}, {8, 10, 12}, {14, 16, 18}};
int a2[SIZE][SIZE] = {{1, 3, 5}, {7, 9, 11}, {13, 15, 17}};
int vihaan_sood[SIZE][SIZE];

printf("Addition:\n");
add_matrices(a1, a2, vihaan_sood);

print_matrix(vihaan_sood);

printf("Subtraction:\n");
subtract_matrices(a1, a2, vihaan_sood);
print_matrix(vihaan_sood);

printf("Element-wise Multiplication:\n");
elementwise_multiply(a1, a2, vihaan_sood);
print_matrix(vihaan_sood);

printf("Matrix Multiplication:\n");
matrix_multiply(a1, a2, vihaan_sood);
print_matrix(vihaan_sood);

return 0;
}
OUTPUT:
Aim: Implement the following stack operations using
an array: 1) PUSH 2) POP 3) PEEK

ASSIGNMENT 3:
#include <stdio.h>

#define MAX 100

int stack[MAX];

int top =-1;

void push(int value) {

if (top == MAX- 1) {

printf("Stack Overflow\n");

} else {

stack[++top] = value;

printf("Pushed %d\n", value);

void pop() {

if (top ==-1) {

printf("Stack Underflow\n");

} else {

printf("Popped %d\n", stack[top--]);

void peek() {

if (top ==-1) {

printf("Stack is empty\n");

} else {

printf("Top element: %d\n", stack[top]);

}
void display() {

if (top ==-1) {

printf("Stack is empty\n");

} else {

for (int i = 0; i <= top; i++) {

printf("%d ", stack[i]);

printf("\n");

int main() {

push(10);

push(20);

push(30);

peek();

pop();

peek();

display();

return 0;

OUTPUT:
Aim: Write a C program that: i)Accepts an m × n matrix
from the user, Computes and displays its transpose.
ii) Check if the given square matrix is symmetric (i.e.,
A[i][j] == A[j][i]).

ASSIGNMENT 4:
#include <stdio.h>

int main() {
int m, n;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);

int matrix[100][100], vihaan_sood[100][100];

printf("Enter the elements of the matrix:\n");


for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}

printf("Original Matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

// Transpose the matrix


for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
vihaan_sood[j][i] = matrix[i][j];
}
}

printf("Transpose of the Matrix:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", vihaan_sood[i][j]);
}
printf("\n");
}

// Check if matrix is symmetric


if (m == n) {
int symmetric = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] != matrix[j][i]) {
symmetric = 0;
break;
}
}
}
if (symmetric) {
printf("The given square matrix is symmetric.\n");
} else {
printf("The given square matrix is not symmetric.\n");
}
} else {
printf("The matrix is not square, so it cannot be symmetric.\n");
}

return 0;
}
OUTPUT:
Aim: Write a C program that accepts an n × n square
matrix from the user. Computes the sum of the main
diagonal elements (where i == j), & Displays the
computed sum.

ASSIGNMENT 5:
#include <stdio.h>

int main() {
int n;

printf("Enter the size of the matrix (n): ");


scanf("%d", &n);

int vihaan_sood[n][n];

printf("Enter the elements of the matrix:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &vihaan_sood[i][j]);
}
}

int sum = 0;
for (int i = 0; i < n; i++) {
sum += vihaan_sood[i][i];
}

printf("The sum of the main diagonal elements is: %d\n", sum);


return 0;
}
OUTPUT:
Aim: Write a C program that accepts an n x n square
matrix from the user. Computes the sum of the main
diagonal elements (where i==j), & Displays the
computed sum.

ASSIGNMENT 6:
A.
#include <stdio.h>

#define MAX 100

int queue[MAX];

int front =-1, rear =-1;

void enqueue(int value) {

if (rear == MAX- 1) {

printf("Queue Overflow\n");

} else {

if (front ==-1) front = 0;

queue[++rear] = value;

printf("Enqueued %d\n", value);

void dequeue() {

if (front ==-1 || front > rear) {

printf("Queue Underflow\n");

} else {

printf("Dequeued %d\n", queue[front++]);

void display() {

if (front ==-1 || front > rear) {


printf("Queue is empty\n");

} else {

for (int i = front; i <= rear; i++) {

printf("%d ", queue[i]);

printf("\n");

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

display();

dequeue();

display();

return 0;

OUTPUT:
B.

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* front = NULL;

struct Node* rear = NULL;

void enqueue(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

if (rear == NULL) {

front = rear = newNode;

} else {

rear->next = newNode;

rear = newNode;

printf("Enqueued %d\n", value);

void dequeue() {

if (front == NULL) {

printf("Queue Underflow\n");

} else {

struct Node* temp = front;

printf("Dequeued %d\n", front->data);

front = front->next;

if (front == NULL) rear = NULL;

free(temp);

}
void display() {

if (front == NULL) {

printf("Queue is empty\n");

} else {

struct Node* temp = front;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

display();

dequeue();

display();

return 0;

OUTPUT:
Aim: Write a C program to implement a Circular Queue
using an array with the following operations: Enqueue,
Dequeue and Display

ASSIGNMENT 7:
#include <stdio.h>

#define MAX 5

int queue[MAX];

int front =-1, rear =-1;

void enqueue(int value) {

if ((front == 0 && rear == MAX- 1) || (rear == (front- 1 + MAX) % MAX)) {

printf("Queue Overflow\n");

} else if (front ==-1) {

front = rear = 0;

queue[rear] = value;

printf("Enqueued %d\n", value);

} else {

rear = (rear + 1) % MAX;

queue[rear] = value;

printf("Enqueued %d\n", value);

void dequeue() {

if (front ==-1) {

printf("Queue Underflow\n");

} else if (front == rear) {

printf("Dequeued %d\n", queue[front]);

front = rear =-1;

} else {

printf("Dequeued %d\n", queue[front]);

front = (front + 1) % MAX;


}

void display() {

if (front ==-1) {

printf("Queue is empty\n");

} else {

int i = front;

while (1) {

printf("%d ", queue[i]);

if (i == rear) break;

i = (i + 1) % MAX;

printf("\n");

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

enqueue(40);

enqueue(50);

display();

dequeue();

dequeue();

display();

enqueue(60);

enqueue(70);

display();

return 0;

}
OUTPUT:
Aim: WAP in C using stacks to:
a. Convert the infix expression (A + B) * (C - D) to
postfix and prefix.
b. Convert the postfix expression AB+CD-* to infix and
prefix.
c. Convert the prefix expression *+AB-CD to infix and
postfix.

ASSIGNMENT 8:
a.)
#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <string.h>

#define MAX 100

typedef struct {

int top;

char items[MAX];

} Stack;

void init(Stack* s) {

s->top = -1;

int isEmpty(Stack* s) {

return s->top == -1;

}
char peek(Stack* s) {

return s->items[s->top];

void push(Stack* s, char value) {

if (s->top == MAX - 1) {

printf("Stack overflow\n");

return;

s->items[++(s->top)] = value;

char pop(Stack* s) {

if (isEmpty(s)) {

printf("Stack underflow\n");

return '\0';

return s->items[(s->top)--];

int precedence(char op) {

switch (op) {

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':

return 3;

return 0;
}

int isOperator(char c) {

return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';

void infixToPostfix(char* infix, char* postfix) {

Stack s;

init(&s);

int k = 0;

for (int i = 0; infix[i]; i++) {

if (isalnum(infix[i])) {

postfix[k++] = infix[i];

} else if (infix[i] == '(') {

push(&s, infix[i]);

} else if (infix[i] == ')') {

while (!isEmpty(&s) && peek(&s) != '(') {

postfix[k++] = pop(&s);

pop(&s);

} else if (isOperator(infix[i])) {

while (!isEmpty(&s) && precedence(peek(&s)) >= precedence(infix[i])) {

postfix[k++] = pop(&s);

push(&s, infix[i]);

while (!isEmpty(&s)) {

postfix[k++] = pop(&s);

}
postfix[k] = '\0';

void infixToPrefix(char* infix, char* prefix) {

Stack s;

init(&s);

int len = strlen(infix);

char* reversed = (char*)malloc(len + 1);

if (!reversed) {

printf("Memory allocation failed\n");

return;

// Reverse the infix expression and swap '(' and ')'

for (int i = 0; i < len; i++) {

reversed[i] = infix[len - 1 - i];

if (reversed[i] == '(') {

reversed[i] = ')';

} else if (reversed[i] == ')') {

reversed[i] = '(';

reversed[len] = '\0';

// Convert reversed infix to postfix

char* tempPostfix = (char*)malloc(len + 1);

if (!tempPostfix) {

printf("Memory allocation failed\n");

free(reversed);

return;

}
infixToPostfix(reversed, tempPostfix);

// Reverse postfix to get prefix

int postfixLen = strlen(tempPostfix);

for (int i = 0; i < postfixLen; i++) {

prefix[i] = tempPostfix[postfixLen - 1 - i];

prefix[postfixLen] = '\0'; // Ensure null termination

free(reversed);

free(tempPostfix);

int main() {

char infix[] = "(A+B)*(C-D)";

char postfix[MAX], prefix[MAX];

infixToPostfix(infix, postfix);

infixToPrefix(infix, prefix);

printf("\nFinal Output:\n");

printf("Infix: %s\n", infix);

printf("Postfix: %s\n", postfix);

printf("Prefix: %s\n", prefix);

return 0;

OUTPUT:
b.)
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX 100

typedef struct {

int top;

char expressions[MAX][MAX];

} Stack;

void init(Stack* s) {

s->top = -1;

int isEmpty(Stack* s) {

return s->top == -1;

void push(Stack* s, char* str) {

if (s->top == MAX - 1) {

printf("Stack overflow!\n");

return;

strcpy(s->expressions[++(s->top)], str);

char* pop(Stack* s) {

if (isEmpty(s)) {

printf("Stack underflow!\n");

return NULL;
}

return s->expressions[(s->top)--];

int isOperator(char c) {

return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');

void convertPostfixToInfix(char* postfix, char* infix) {

Stack s;

init(&s);

char leftOperand[MAX], rightOperand[MAX], combined[MAX];

for (int i = 0; postfix[i] != '\0'; i++) {

if (isOperator(postfix[i])) {

strcpy(rightOperand, pop(&s));

strcpy(leftOperand, pop(&s));

sprintf(combined, "(%s%c%s)", leftOperand, postfix[i], rightOperand);

push(&s, combined);

} else {

char operand[2] = {postfix[i], '\0'};

push(&s, operand);

strcpy(infix, pop(&s));

void convertPostfixToPrefix(char* postfix, char* prefix) {

Stack s;

init(&s);

char leftOperand[MAX], rightOperand[MAX], combined[MAX];

for (int i = 0; postfix[i] != '\0'; i++) {


if (isOperator(postfix[i])) {

strcpy(rightOperand, pop(&s));

strcpy(leftOperand, pop(&s));

sprintf(combined, "%c%s%s", postfix[i], leftOperand, rightOperand);

push(&s, combined);

} else {

char operand[2] = {postfix[i], '\0'};

push(&s, operand);

strcpy(prefix, pop(&s));

int main() {

char postfix[] = "AB+CD-*";

char infix[MAX], prefix[MAX];

convertPostfixToInfix(postfix, infix);

convertPostfixToPrefix(postfix, prefix);

printf("\nPostfix Expression: %s\n", postfix);

printf("Converted Infix Expression: %s\n", infix);

printf("Converted Prefix Expression: %s\n", prefix);

return 0;

OUTPUT:
c.)
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX 100

typedef struct {

int top;

char expressions[MAX][MAX];

} Stack;

void init(Stack* s) {

s->top = -1;

int isEmpty(Stack* s) {

return s->top == -1;

void push(Stack* s, char* str) {

if (s->top == MAX - 1) {

printf("Stack overflow!\n");

return;

strcpy(s->expressions[++(s->top)], str);

char* pop(Stack* s) {

if (isEmpty(s)) {

printf("Stack underflow!\n");

return NULL;
}

return s->expressions[(s->top)--];

int isOperator(char c) {

return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');

void convertPrefixToInfix(char* prefix, char* infix) {

Stack s;

init(&s);

char leftOperand[MAX], rightOperand[MAX], combined[MAX];

int len = strlen(prefix);

for (int i = len - 1; i >= 0; i--) {

if (isOperator(prefix[i])) {

strcpy(leftOperand, pop(&s));

strcpy(rightOperand, pop(&s));

sprintf(combined, "(%s%c%s)", leftOperand, prefix[i], rightOperand);

push(&s, combined);

} else {

char operand[2] = {prefix[i], '\0'};

push(&s, operand);

strcpy(infix, pop(&s));

void convertPrefixToPostfix(char* prefix, char* postfix) {

Stack s;

init(&s);

char leftOperand[MAX], rightOperand[MAX], combined[MAX];


int len = strlen(prefix);

for (int i = len - 1; i >= 0; i--) {

if (isOperator(prefix[i])) {

strcpy(leftOperand, pop(&s));

strcpy(rightOperand, pop(&s));

sprintf(combined, "%s%s%c", leftOperand, rightOperand, prefix[i]);

push(&s, combined);

} else {

char operand[2] = {prefix[i], '\0'};

push(&s, operand);

strcpy(postfix, pop(&s));

int main() {

char prefix[] = "*+AB-CD";

char infix[MAX], postfix[MAX];

convertPrefixToInfix(prefix, infix);

convertPrefixToPostfix(prefix, postfix);

printf("\nPrefix Expression: %s\n", prefix);

printf("Converted Infix Expression: %s\n", infix);

printf("Converted Postfix Expression: %s\n", postfix);

return 0;

OUTPUT:
Aim: WAP in C to implement a Binary Search Tree (BST)
with functions for insertion, searching, and in-order
traversal

ASSIGNMENT 9:
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *left, *right;

};

struct Node* createNode(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->left = newNode->right = NULL;

return newNode;

struct Node* insert(struct Node* root, int value) {

if (root == NULL) return createNode(value);

if (value < root->data) root->left = insert(root->left, value);

else if (value > root->data) root->right = insert(root->right, value);

return root;

struct Node* search(struct Node* root, int key) {

if (root == NULL || root->data == key) return root;

if (key < root->data) return search(root->left, key);

return search(root->right, key);


}

void inorder(struct Node* root) {

if (root != NULL) {

inorder(root->left);

printf("%d ", root->data);

inorder(root->right);

int main() {

struct Node* root = NULL;

root = insert(root, 50);

insert(root, 30);

insert(root, 70);

insert(root, 20);

insert(root, 40);

insert(root, 60);

insert(root, 80);

printf("Inorder traversal: ");

inorder(root);

printf("\n");

int key = 40;

struct Node* found = search(root, key);

if (found) printf("%d found in BST\n", key);

else printf("%d not found in BST\n", key);

return 0;

OUTPUT:
Aim: Write a C program to construct a B-Tree of Order
3 by inserting the given keys sequentially. The B-Tree
should follow the properties of an m-order B-Tree,
where each node can have at most m-1 keys and m
children. Given Keys (in order of insertion):12, 10, 15,
4, 1, 17, 3, 13

ASSIGNMENT 10:
#include <stdio.h>

#include <stdlib.h>

#define MIN_DEGREE 2

typedef struct BTreeNode {

int keys[2 * MIN_DEGREE - 1];

struct BTreeNode* children[2 * MIN_DEGREE];

int num_keys;

int leaf;

} BTreeNode;

typedef struct BTree {

BTreeNode* root;

} BTree;

BTreeNode* createNode(int leaf) {

BTreeNode* node = (BTreeNode*)malloc(sizeof(BTreeNode));

node->num_keys = 0;

node->leaf = leaf;

for (int i = 0; i < 2 * MIN_DEGREE; i++) {

node->children[i] = NULL;
}

return node;

typedef struct SplitResult {

BTreeNode* newChild;

int middleKey;

} SplitResult;

SplitResult splitChild(BTreeNode* parent, int index) {

BTreeNode* y = parent->children[index];

BTreeNode* z = createNode(y->leaf);

SplitResult result;

for (int j = 0; j < MIN_DEGREE - 1; j++) {

z->keys[j] = y->keys[j + MIN_DEGREE];

if (!y->leaf) {

for (int j = 0; j < MIN_DEGREE; j++) {

z->children[j] = y->children[j + MIN_DEGREE];

y->num_keys = MIN_DEGREE - 1;

z->num_keys = MIN_DEGREE - 1;

result.newChild = z;

result.middleKey = y->keys[MIN_DEGREE - 1];

return result;

}
void insertNonFull(BTreeNode* node, int key) {

int i = node->num_keys - 1;

if (node->leaf) {

while (i >= 0 && key < node->keys[i]) {

node->keys[i + 1] = node->keys[i];

i--;

node->keys[i + 1] = key;

node->num_keys++;

} else {

while (i >= 0 && key < node->keys[i]) {

i--;

i++;

if (node->children[i]->num_keys == (2 * MIN_DEGREE - 1)) {

SplitResult split = splitChild(node, i);

for (int j = node->num_keys; j > i; j--) {

node->keys[j] = node->keys[j - 1];

node->children[j + 1] = node->children[j];

node->keys[i] = split.middleKey;

node->children[i + 1] = split.newChild;

node->num_keys++;

if (key > split.middleKey) {

i++;

insertNonFull(node->children[i], key);

void insert(BTree* tree, int key) {

if (tree->root == NULL) {
tree->root = createNode(1);

tree->root->keys[0] = key;

tree->root->num_keys = 1;

return;

if (tree->root->num_keys == (2 * MIN_DEGREE - 1)) {

BTreeNode* newRoot = createNode(0);

newRoot->children[0] = tree->root;

SplitResult split = splitChild(newRoot, 0);

newRoot->keys[0] = split.middleKey;

newRoot->children[1] = split.newChild;

newRoot->num_keys = 1;

tree->root = newRoot;

insertNonFull(tree->root, key);

void inorderTraversal(BTreeNode* root) {

if (root == NULL) return;

int i;

for (i = 0; i < root->num_keys; i++) {

inorderTraversal(root->children[i]);

printf("%d ", root->keys[i]);

inorderTraversal(root->children[i]);

void printBTreeHelper(BTreeNode* root, int space) {

if (root == NULL) return;


space += 10;

printBTreeHelper(root->children[root->num_keys], space);

printf("\n");

for (int i = 10; i < space; i++) {

printf(" ");

for (int i = 0; i < root->num_keys; i++) {

printf("%d ", root->keys[i]);

printf("\n");

for (int i = root->num_keys - 1; i >= 0; i--) {

printBTreeHelper(root->children[i], space);

void printBTree(BTreeNode* root) {

printBTreeHelper(root, 0);

printf("\n");

int main() {

BTree tree = {NULL};

int keys[] = {12, 10, 15, 4, 1, 17, 3, 13};

int n = sizeof(keys) / sizeof(keys[0]);

for (int i = 0; i < n; i++) {

insert(&tree, keys[i]);

}
printf("B-Tree of Order 3 after insertion (Level-wise):\n");

printBTree(tree.root);

printf("\nInorder Traversal: ");

inorderTraversal(tree.root);

return 0;

OUTPUT:
Aim: Write a C program to search for `23` in the array
`{34, 7, 23, 32, 5, 62}` using Linear Search on the
unsorted array, then sort it and use Binary Search.
Display the index if found.

ASSIGNMENT 11:
#include <stdio.h>

int linearSearch(int arr[], int size, int key) {

for(int i = 0; i < size; i++) {

if(arr[i] == key)

return i;

return -1;

void bubbleSort(int arr[], int size) {

for(int i = 0; i < size-1; i++) {

for(int j = 0; j < size-i-1; j++) {

if(arr[j] > arr[j+1]) {

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

int binarySearch(int arr[], int size, int key) {

int low = 0, high = size - 1;


while(low <= high) {

int mid = (low + high) / 2;

if(arr[mid] == key)

return mid;

else if(arr[mid] < key)

low = mid + 1;

else

high = mid - 1;

return -1;

int main() {

int arr[] = {34, 7, 23, 32, 5, 62};

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

int key = 23;

printf("Original Array: ");

for(int i = 0; i < size; i++) {

printf("%d ", arr[i]);

int linearIndex = linearSearch(arr, size, key);

if(linearIndex != -1)

printf("\nLinear Search: Found %d at index %d\n", key, linearIndex);

else

printf("\nLinear Search: %d not found in the array\n", key);

bubbleSort(arr, size);

printf("Sorted Array: ");


for(int i = 0; i < size; i++) {

printf("%d ", arr[i]);

int binaryIndex = binarySearch(arr, size, key);

if(binaryIndex != -1)

printf("\nBinary Search: Found %d at index %d (sorted array)\n", key, binaryIndex);

else

printf("\nBinary Search: %d not found in the array\n", key);

return 0;

OUTPUT:
Aim: Write a C program to implement Bubble Sort and
Insertion Sort on a given array. Display the array after
applying each sorting technique: int arr[] = {45, 12, 89,
33, 67, 23, 10, 5, 76, 38};

ASSIGNMENT 12:
#include <stdio.h>

void bubbleSort(int arr[], int n) {

for (int i = 0; i < n-1; i++)

for (int j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1]) {

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

void insertionSort(int arr[], int n) {

for (int i = 1; i < n; i++) {

int key = arr[i], j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j+1] = arr[j];

j--;

arr[j+1] = key;

void display(int arr[], int n) {

for (int i = 0; i < n; i++)


printf("%d ", arr[i]);

printf("\n");

int main() {

int arr1[] = {45, 12, 89, 33, 67, 23, 10, 5, 76, 38};

int arr2[] = {45, 12, 89, 33, 67, 23, 10, 5, 76, 38};

int n = sizeof(arr1) / sizeof(arr1[0]);

bubbleSort(arr1, n);

printf("Array after Bubble Sort: ");

display(arr1, n);

insertionSort(arr2, n);

printf("Array after Insertion Sort: ");

display(arr2, n);

return 0;

}include <stdio.h>

int linearSearch(int arr[], int size, int key) {

for(int i = 0; i < size; i++) {

if(arr[i] == key)

return i;

return -1;

void bubbleSort(int arr[], int size) {

for(int i = 0; i < size-1; i++) {

for(int j = 0; j < size-i-1; j++) {

if(arr[j] > arr[j+1]) {


int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

int binarySearch(int arr[], int size, int key) {

int low = 0, high = size - 1;

while(low <= high) {

int mid = (low + high) / 2;

if(arr[mid] == key)

return mid;

else if(arr[mid] < key)

low = mid + 1;

else

high = mid - 1;

return -1;

int main() {

int arr[] = {34, 7, 23, 32, 5, 62};

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

int key = 23;

printf("Original Array: ");

for(int i = 0; i < size; i++) {

printf("%d ", arr[i]);

}
int linearIndex = linearSearch(arr, size, key);

if(linearIndex != -1)

printf("\nLinear Search: Found %d at index %d\n", key, linearIndex);

else

printf("\nLinear Search: %d not found in the array\n", key);

bubbleSort(arr, size);

printf("Sorted Array: ");

for(int i = 0; i < size; i++) {

printf("%d ", arr[i]);

int binaryIndex = binarySearch(arr, size, key);

if(binaryIndex != -1)

printf("\nBinary Search: Found %d at index %d (sorted array)\n", key, binaryIndex);

else

printf("\nBinary Search: %d not found in the array\n", key);

return 0;

OUTPUT:
Aim: WAP in C to implement quick sort program using
arrays

ASSIGNMENT 13:
#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;

for (int j = low; j < high; 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 p = partition(arr, low, high);

quickSort(arr, low, p - 1);

quickSort(arr, p + 1, high);

}
void display(int arr[], int n) {

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {45, 12, 89, 33, 67, 23, 10, 5, 76, 38};

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

quickSort(arr, 0, n - 1);

printf("Array after Quick Sort: ");

display(arr, n);

return 0;

OUTPUT:
Aim: WAP to implement merge sort program using
arrays

ASSIGNMENT 14:
#include <stdio.h>

void merge(int arr[], int l, int m, int r) {

int n1 = m - l + 1, n2 = r - m;

int L[n1], R[n2];

for (int i = 0; i < n1; i++) L[i] = arr[l + i];

for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];

int i = 0, j = 0, k = l;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) arr[k++] = L[i++];

else arr[k++] = R[j++];

while (i < n1) arr[k++] = L[i++];

while (j < n2) arr[k++] = R[j++];

void mergeSort(int arr[], int l, int r) {

if (l < r) {

int m = l + (r - l) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

}
void display(int arr[], int n) {

for (int i = 0; i < n; i++) printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {45, 12, 89, 33, 67, 23, 10, 5, 76, 38};

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

mergeSort(arr, 0, n - 1);

printf("Array after Merge Sort: ");

display(arr, n);

return 0;

OUTPUT:
Aim: WAP in C to implement BFS and DFS on a graph
represented using an adjacency matrix. The program
should take the number of vertices and edges as input,
allow the user to enter the edge connections, and
perform both traversals starting from a user-defined
starting vertex. Finally, display the order of traversal for
both algorithms.

ASSIGNMENT 15:
#include <stdio.h>

#define MAX 100

int adj[MAX][MAX], visited[MAX], queue[MAX];

int front = 0, rear = 0;

void bfs(int start, int n) {

for (int i = 0; i < n; i++) visited[i] = 0;

front = rear = 0;

queue[rear++] = start;

visited[start] = 1;

while (front < rear) {

int u = queue[front++];

printf("%d ", u);

for (int v = 0; v < n; v++) {

if (adj[u][v] && !visited[v]) {

queue[rear++] = v;

visited[v] = 1;

}
}

void dfsUtil(int u, int n) {

visited[u] = 1;

printf("%d ", u);

for (int v = 0; v < n; v++) {

if (adj[u][v] && !visited[v]) {

dfsUtil(v, n);

void dfs(int start, int n) {

for (int i = 0; i < n; i++) visited[i] = 0;

dfsUtil(start, n);

int main() {

int n, e, u, v, start;

printf("Enter number of vertices: ");

scanf("%d", &n);

printf("Enter number of edges: ");

scanf("%d", &e);

for (int i = 0; i < e; i++) {

scanf("%d %d", &u, &v);

adj[u][v] = adj[v][u] = 1;

printf("Enter starting vertex: ");

scanf("%d", &start);
printf("BFS traversal: ");

bfs(start, n);

printf("\n");

printf("DFS traversal: ");

dfs(start, n);

printf("\n");

return 0;

OUTPUT:
Aim: WAP To implement the Selection Sort algorithm
in C for sorting an array of integers in ascending order.

ASSIGNMENT 16:
#include <stdio.h>

void selectionSort(int arr[], int n) {

for (int i = 0; i < n - 1; i++) {

int min = i;

for (int j = i + 1; j < n; j++)

if (arr[j] < arr[min])

min = j;

if (min != i) {

int temp = arr[i];

arr[i] = arr[min];

arr[min] = temp;

void display(int arr[], int n) {

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {45, 12, 89, 33, 67, 23, 10, 5, 76, 38};

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


selectionSort(arr, n);

printf("Array after Selection Sort: ");

display(arr, n);

return 0;

OUTPUT:
Aim: WAP To implement Kruskal's and Prim's
algorithms in C for finding the Minimum Spanning Tree
(MST) of a connected, weighted, undirected graph.

ASSIGNMENT 17:
#include <stdio.h>

#include <stdlib.h>

#define MAX 100

#define INF 9999

int parent[MAX];

int find(int i) {

while (parent[i])

i = parent[i];

return i;

void unionSet(int i, int j) {

int a = find(i);

int b = find(j);

if (a != b)

parent[b] = a;

void kruskal(int cost[MAX][MAX], int n) {

int i, j, a, b, u, v, min, ne = 1, mincost = 0;

while (ne < n) {

min = INF;
for (i = 0; i < n; i++)

for (j = 0; j < n; j++)

if (cost[i][j] < min && i != j) {

min = cost[i][j];

a = u = i;

b = v = j;

u = find(u);

v = find(v);

if (u != v) {

unionSet(u, v);

printf("Edge %d: (%d - %d) cost: %d\n", ne++, a, b, min);

mincost += min;

cost[a][b] = cost[b][a] = INF;

printf("Total cost using Kruskal's = %d\n", mincost);

void prim(int cost[MAX][MAX], int n) {

int visited[MAX] = {0}, mincost = 0;

visited[0] = 1;

for (int edges = 0; edges < n - 1; edges++) {

int min = INF, a = -1, b = -1;

for (int i = 0; i < n; i++) {

if (visited[i]) {

for (int j = 0; j < n; j++) {

if (!visited[j] && cost[i][j] < min) {

min = cost[i][j];

a = i;

b = j;

}
}

if (a != -1 && b != -1) {

printf("Edge %d: (%d - %d) cost: %d\n", edges + 1, a, b, min);

visited[b] = 1;

mincost += min;

printf("Total cost using Prim's = %d\n", mincost);

int main() {

int cost[MAX][MAX], n, e, u, v, w;

printf("Enter number of vertices: ");

scanf("%d", &n);

// Initialize cost matrix

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

cost[i][j] = INF;

printf("Enter number of edges: ");

scanf("%d", &e);

printf("Enter edges in the format: u v weight\n");

for (int i = 0; i < e; i++) {

scanf("%d %d %d", &u, &v, &w);

cost[u][v] = cost[v][u] = w;

printf("\n--- Kruskal's Algorithm ---\n");


kruskal(cost, n);

// Re-enter costs since Kruskal alters the matrix

for (int i = 0; i < n; i++)

for (int j = 0; j < n; j++)

if (cost[i][j] != INF)

cost[i][j] = cost[j][i];

printf("\n--- Prim's Algorithm ---\n");

prim(cost, n);gf

return 0;

OUTPUT:

You might also like