Ilovepdf Merged
Ilovepdf Merged
PRACTICAL-5
AIM: Write a program for insertion sort, selection sort and bubble sort.
THEORY: Selection sort: repeatedly pick the smallest element to append to the result. Insertion
sort: repeatedly add new element to the sorted result. Bubble sort: repeatedly compare neighbor
pairs and swap if necessary.
SOURCE CODE:
#include <math.h>
#include <stdio.h>
insertionSort(arr, N);
return 0;
}
#include<stdio.h>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
return 0;
}
#include <stdio.h>
Roll no:2323019
int main() {
int arr[] = { 12, 11, 13, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
return 0;
}
OUTPUT:
PRACTICAL-6
THEORY: A stack is a linear data structure in which the insertion of a new element and removal
of an existing element takes place at the same end represented as the top of the stack.
SOURCE CODE:
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 100
typedef struct {
int arr[MAX_SIZE];
int top;
} Stack;
int main() {
Stack stack;
initialize(&stack);
push(&stack, 3);
printf("Top element: %d\n", peek(&stack));
push(&stack, 5);
printf("Top element: %d\n", peek(&stack));
push(&stack, 2);
printf("Top element: %d\n", peek(&stack));
push(&stack, 8);
printf("Top element: %d\n", peek(&stack));
while (!isEmpty(&stack)) {
printf("Top element: %d\n", peek(&stack));
printf("Popped element: %d\n", pop(&stack));
}
return 0;
}
OUTPUT:
Roll no:2323019
Roll no:2323019
PRACTICAL-7
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = { 4, 2, 5, 3, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("\n");
return 0;
}
OUTPUT:
Roll no:2323019
PRACTICAL-8
THEORY: Merge sort is a divide-and-conquer algorithm. This means that it breaks down a
problem into smaller sub problems, solves the sub problems recursively, and then combines the
solutions to the sub problems to solve the original problem.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
printf("\n");
return 0;
}
OUTPUT:
Roll no:2323019
PRACTICAL-9
THEORY: BFS, or Breadth-First Search, is a node method for obtaining the graph's shortest path.
It makes use of a queue data structure with FIFO (first in, first out) ordering.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100
int main() {
int V = 5;
int adj[MAX][MAX] = {0};
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 4);
return 0;
}
OUTPUT:
Roll no:2323019
PRACTICAL-10
THEORY: Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data
structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in
the case of a graph) and explores as far as possible along each branch before backtracking.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node {
int data;
struct Node* next;
};
struct List {
struct Node* head;
};
struct Graph {
int vertices;
struct List* array;
};
return graph;
}
int main() {
int vertices = 4;
struct Graph* graph = createGraph(vertices);
addEdge(graph, 2, 0);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 0, 1);
addEdge(graph, 3, 3);
addEdge(graph, 1, 3);
return 0;
}
OUTPUT:
Roll no:2323019