0% found this document useful (0 votes)
27 views15 pages

Ilovepdf Merged

Uploaded by

Dipanshu Xi
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)
27 views15 pages

Ilovepdf Merged

Uploaded by

Dipanshu Xi
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/ 15

Roll no:2323019

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:

//source code of insertion sort

#include <math.h>
#include <stdio.h>

void insertion_Sort(int arr[], int N) {

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


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

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


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = { 12, 11, 13, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);

printf("Unsorted array: ");


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

insertionSort(arr, N);

printf("Sorted array: ");


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

return 0;
}

//source code of selection sort


Roll no:2323019

#include<stdio.h>

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


for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}

int temp = arr[i];


arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}

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


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

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


printArray(arr, n);

selectionSort(arr, n);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}

//source code of bubble sort

#include <stdio.h>
Roll no:2323019

void swap(int* arr, int i, int j) {


int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

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])
swap(arr, j, j + 1);
}
}
}

int main() {
int arr[] = { 12, 11, 13, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);

printf("Unsorted array befor bubble sort: ");


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

bubbleSort(arr, N);

printf("Sorted array after bubble sort: ");


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

return 0;
}

OUTPUT:

//output of insertion sort


Roll no:2323019

//output of selection sort

//output of bubble sort


Roll no:2323019

PRACTICAL-6

AIM: Write a program to implement Stack and its operation.

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;

void initialize(Stack *stack) {


stack->top = -1;
}

bool isEmpty(Stack *stack) {


return stack->top == -1;
}

bool isFull(Stack *stack) {


return stack->top == MAX_SIZE - 1;
}

//to push elements

void push(Stack *stack, int value) {


if (isFull(stack)) {
printf("Stack Overflow\n");
return;
}
stack->arr[++stack->top] = value;
printf("Pushed %d onto the stack\n", value);
}

//to pop elements

int pop(Stack *stack) {


if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
Roll no:2323019

int popped = stack->arr[stack->top];


stack->top--;
printf("Popped %d from the stack\n", popped);
// return the popped element
return popped;

//to peek elements

int peek(Stack *stack) {


if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->arr[stack->top];
}

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

AIM: Write a program for quick sort.

THEORY: Quicksort is a divide-and-conquer algorithm. It works by selecting a ‘pivot’ element


from the array and partitioning the other elements into two sub-arrays, according to whether they
are less than or greater than the pivot.

SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>

int compare(const void* a, const void* b) {


return (*(int*)a - *(int*)b);
}

int main() {
int arr[] = { 4, 2, 5, 3, 1 };
int n = sizeof(arr) / sizeof(arr[0]);

printf("Unsorted array: ");


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

qsort(arr, n, sizeof(int), compare);

printf("Sorted array: ");


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

printf("\n");

return 0;
}

OUTPUT:
Roll no:2323019

PRACTICAL-8

AIM: Write a program for merge sort.

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>

void merge(int arr[], int left, int mid, int right) {


int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int leftArr[n1], rightArr[n2];

for (i = 0; i < n1; i++)


leftArr[i] = arr[left + i];
for (j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
}
else {
arr[k] = rightArr[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}
while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
}

void mergeSort(int arr[], int left, int right) {


Roll no:2323019
if (left < right) {

int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

int main() {
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);

printf("before merge sort: ");


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

mergeSort(arr, 0, n - 1);

printf("after merge sort: ");


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

printf("\n");

return 0;
}

OUTPUT:
Roll no:2323019

PRACTICAL-9

AIM: Write a program for BFS.

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

void bfs(int adj[MAX][MAX], int V, int s) {


int q[MAX], front = 0, rear = 0;
bool visited[MAX] = { false };
visited[s] = true;
q[rear++] = s;

while (front < rear) {


int curr = q[front++];
printf("%d ", curr);

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


if (adj[curr][i] == 1 && !visited[i]) {
visited[i] = true;
q[rear++] = i;
}
}
}
}

void addEdge(int adj[MAX][MAX], int u, int v) {


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

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);

printf("BFS starting from 0:\n");


bfs(adj, V, 0);
Roll no:2323019

return 0;
}

OUTPUT:
Roll no:2323019

PRACTICAL-10

AIM: Write a program for DFS.

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;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

struct Graph* createGraph(int vertices) {


struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->vertices = vertices;
graph->array = (struct List*)malloc(vertices * sizeof(struct List));

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


graph->array[i].head = NULL;
}

return graph;
}

void addEdge(struct Graph* graph, int src, int dest) {


struct Node* newNode = createNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
Roll no:2323019
}

void DFS(struct Graph* graph, int vertex, bool visited[]) {


visited[vertex] = true;
printf("%d ", vertex);
struct Node* currentNode = graph->array[vertex].head;
while (currentNode) {
int adjacentVertex = currentNode->data;
if (!visited[adjacentVertex]) {
DFS(graph, adjacentVertex, visited);
}
currentNode = currentNode->next;
}
}

void DFSTraversal(struct Graph* graph, int* order, int orderSize) {


bool* visited = (bool*)malloc(graph->vertices * sizeof(bool));
for (int i = 0; i < graph->vertices; i++) {
visited[i] = false;
}

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


if (!visited[order[i]]) {
DFS(graph, order[i], visited);
}
}
free(visited);
}

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);

int order[] = {2, 0, 1, 3};


int orderSize = sizeof(order) / sizeof(order[0]);

printf("Following is Depth First Traversal (starting from vertex 2):\n");


DFSTraversal(graph, order, orderSize);

return 0;
}

OUTPUT:
Roll no:2323019

You might also like