Objective: LAB-1: Implementation of Quick Sort and Merge Sort 1.1 Quick Sort

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

LAB-1

Objective: Implementation of Quick Sort and Merge Sort

1.1 Quick Sort

#include <stdio.h>

void quickSort(int A[], int low, int high) {


if (low < high) {
int pivotIndex = partition(A, low, high);
quickSort(A, low, pivotIndex - 1);
quickSort(A, pivotIndex + 1, high);
}
}

int partition(int A[], int low, int high) {


int pivot = A[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (A[j] <= pivot) {
i++;
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
int temp = A[i + 1];
A[i + 1] = A[high];
A[high] = temp;
return i + 1;
}

void PrintArray(int *A, int n) {


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

}
int main() {
int A[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(A) / sizeof(A[0]);

printf("Original array: ");


PrintArray(A, n);

quickSort(A, 0, n - 1);

printf("Sorted array: ");


PrintArray(A, n);

return 0;
}

Output
1.2 Merge Sort

#include <stdio.h>

void PrintArray(int *A, int n) {


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

void merge(int A[], int low, int mid, int high) {


int i, j, k;
int B[100];
i = low;
j = mid + 1;
k = low;

while (i <= mid && j <= high) {


if (A[i] < A[j]) {
B[k] = A[i];
i++;
} else {
B[k] = A[j];
j++;
}
k++;
}

while (i <= mid) {


B[k] = A[i];
i++;
k++;
}

while (j <= high) {


B[k] = A[j];
j++;
k++;
}
for (i = low; i <= high; i++) {
A[i] = B[i];
}
}

void MergeSort(int A[], int low, int high) {


if (low < high) {
int mid = (low + high) / 2;
MergeSort(A, low, mid);
MergeSort(A, mid + 1, high);
merge(A, low, mid, high);
}
}

int main() {
int A[] = {9, 4, 5, 10, 12, 2};
int n = sizeof(A) / sizeof(A[0]);

printf("Original array: ");


PrintArray(A, n);

MergeSort(A, 0, n - 1);

printf("Sorted array: ");


PrintArray(A, n);

return 0;
}

Output
LAB-2
Objective: Implementation of Linear-Time Sorting Algorithm

1.1 Radix Sort

#include <stdio.h>

int getMax(int A[], int n) {


int max = A[0];
for (int i = 1; i < n; i++) {
if (A[i] > max) {
max = A[i];
}
}
return max;
}

void countSort(int A[], int n, int exp) {


int output[n];
int count[10] = {0};

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


count[(A[i] / exp) % 10]++;
}

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


count[i] += count[i - 1];
}

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


output[count[(A[i] / exp) % 10] - 1] = A[i];
count[(A[i] / exp) % 10]--;
}

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


A[i] = output[i];
}
}
void radixSort(int A[], int n) {
int max = getMax(A, n);

for (int exp = 1; max / exp > 0; exp *= 10)


{
countSort(A, n, exp);
}
}

void PrintArray(int A[], int n) {


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

int main() {
int A[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(A) / sizeof(A[0]);

printf("Original array: ");


PrintArray(A, n);

radixSort(A, n);

printf("Sorted array: ");


PrintArray(A, n);

return 0;
}

Output
1.2 Count Sort

#include <stdio.h>

void countSort(int A[], int n) {


int output[n];
int max = A[0];
for (int i = 1; i < n; i++) {
if (A[i] > max) {
max = A[i];
}
}

int count[max + 1];


for (int i = 0; i <= max; i++) {
count[i] = 0;
}

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


count[A[i]]++;
}

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


count[i] += count[i - 1];
}

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


output[count[A[i]] - 1] = A[i];
count[A[i]]--;
}

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


A[i] = output[i];
}
}

void PrintArray(int A[], int n) {


for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
}
int main() {
int A[] = {4, 2, 2, 8, 3, 3, 1};
int n = sizeof(A) / sizeof(A[0]);

printf("Original array: ");


PrintArray(A, n);

countSort(A, n);

printf("Sorted array: ");


PrintArray(A, n);

return 0;
}

Output
1.3. Bucket Sort

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

#define BUCKET_COUNT 10

void bucketSort(float A[], int n) {


float buckets[BUCKET_COUNT][n];
int bucketSizes[BUCKET_COUNT] = {0};

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


int bucketIndex = A[i] * BUCKET_COUNT;
buckets[bucketIndex][bucketSizes[bucketIndex]++] = A[i];
}

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


for (int j = 1; j < bucketSizes[i]; j++) {
float key = buckets[i][j];
int k = j - 1;
while (k >= 0 && buckets[i][k] > key) {
buckets[i][k + 1] = buckets[i][k];
k--;
}
buckets[i][k + 1] = key;
}
}

int index = 0;
for (int i = 0; i < BUCKET_COUNT; i++) {
for (int j = 0; j < bucketSizes[i]; j++) {
A[index++] = buckets[i][j];
}
}
}

void PrintArray(float A[], int n) {


for (int i = 0; i < n; i++) {
printf("%.2f ", A[i]);
}
printf("\n");
}
int main() {
float A[] = {0.42, 0.32, 0.23, 0.52, 0.25,
0.47, 0.51};
int n = sizeof(A) / sizeof(A[0]);

printf("Original array: ");


PrintArray(A, n);

bucketSort(A, n);

printf("Sorted array: ");


PrintArray(A, n);

return 0;
}

Output
LAB - 3

Objective: Implementation of Red-Black Tree Operations

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

typedef enum { RED, BLACK } Color;

typedef struct Node {


int data;
Color color;
struct Node *left, *right, *parent;
} Node;

Node *root = NULL;

Node *createNode(int data) {


Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->color = RED;
newNode->left = newNode->right = newNode->parent = NULL;
return newNode;
}

void rotateLeft(Node **root, Node *pt) {


Node *pt_y = pt->right;
pt->right = pt_y->left;
if (pt_y->left) pt_y->left->parent = pt;
pt_y->parent = pt->parent;
if (!pt->parent) *root = pt_y;
else if (pt == pt->parent->left) pt->parent->left = pt_y;
else pt->parent->right = pt_y;
pt_y->left = pt;
pt->parent = pt_y;
}

void rotateRight(Node **root, Node *pt) {


Node *pt_y = pt->left;
pt->left = pt_y->right;
if (pt_y->right) pt_y->right->parent = pt;
pt_y->parent = pt->parent;
if (!pt->parent) *root = pt_y;
else if (pt == pt->parent->left) pt->parent->left = pt_y;
else pt->parent->right = pt_y;
pt_y->right = pt;
pt->parent = pt_y;
}

void fixViolation(Node **root, Node *pt) {


Node *pt_parent;
while (pt != *root && pt->color == RED && pt->parent->color == RED) {
pt_parent = pt->parent;
Node *pt_grandparent = pt_parent->parent;
if (pt_parent == pt_grandparent->left) {
Node *pt_uncle = pt_grandparent->right;
if (pt_uncle && pt_uncle->color == RED) {
pt_grandparent->color = RED;
pt_parent->color = BLACK;
pt_uncle->color = BLACK;
pt = pt_grandparent;
} else {
if (pt == pt_parent->right) {
rotateLeft(root, pt_parent);
pt = pt_parent;
pt_parent = pt->parent;
}
rotateRight(root, pt_grandparent);
pt_parent->color = BLACK;
pt_grandparent->color = RED;
pt = pt_parent;
}
} else {
Node *pt_uncle = pt_grandparent->left;
if (pt_uncle && pt_uncle->color == RED) {
pt_grandparent->color = RED;
pt_parent->color = BLACK;
pt_uncle->color = BLACK;
pt = pt_grandparent;
} else {
if (pt == pt_parent->left) {
rotateRight(root, pt_parent);
pt = pt_parent;
pt_parent = pt->parent;
}
rotateLeft(root, pt_grandparent);
pt_parent->color = BLACK;
pt_grandparent->color = RED;
pt = pt_parent;
}
}
}
(*root)->color = BLACK;
}

void insert(int data) {


Node *pt = createNode(data);
Node *y = NULL;
Node *x = root;
while (x) {
y = x;
if (pt->data < x->data) x = x->left;
else x = x->right;
}
pt->parent = y;
if (!y) root = pt;
else if (pt->data < y->data) y->left = pt;
else y->right = pt;
fixViolation(&root, pt);
}

void inorder(Node *root) {


if (root) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

int main() {
insert(10);
insert(20);
insert(30);
insert(15);
printf("Inorder Traversal: ");
inorder(root);
return 0;
}
Output:
LAB-4
Objective-Implementation of Binomial Heap Operations
#include <stdio.h>
#include <stdlib.h>

struct Node {
int key, degree;
struct Node *parent, *child, *sibling;
};

struct Node *createNode(int key) {


struct Node *node = (struct Node *)malloc(sizeof(struct Node));
node->key = key;
node->degree = 0;
node->parent = node->child = node->sibling = NULL;
return node;
}

struct Node *mergeBinomialTrees(struct Node *tree1, struct Node *tree2) {


if (tree1->key > tree2->key) {
struct Node *temp = tree1;
tree1 = tree2;
tree2 = temp;
}
tree2->parent = tree1;
tree2->sibling = tree1->child;
tree1->child = tree2;
tree1->degree++;
return tree1;
}

struct Node *unionBinomialHeap(struct Node *heap1, struct Node *heap2) {


if (!heap1) return heap2;
if (!heap2) return heap1;

struct Node *newHeap = NULL;


struct Node **position = &newHeap;

while (heap1 && heap2) {


if (heap1->degree <= heap2->degree) {
*position = heap1;
heap1 = heap1->sibling;
}
else {
*position = heap2;
heap2 = heap2->sibling;
}
position = &((*position)->sibling);
}

if (heap1) *position = heap1;


else *position = heap2;

return newHeap;
}

struct Node *adjustBinomialHeap(struct Node *heap) {


if (!heap || !heap->sibling) return heap;

struct Node *prev = NULL;


struct Node *curr = heap;
struct Node *next = curr->sibling;

while (next) {
if ((curr->degree != next->degree) || (next->sibling && next->sibling->degree == curr-
>degree)) {
prev = curr;
curr = next;
} else if (curr->key <= next->key) {
curr->sibling = next->sibling;
curr = mergeBinomialTrees(curr, next);
} else {
if (prev) prev->sibling = next;
else heap = next;
next = mergeBinomialTrees(next, curr);
curr = next;
}
next = curr->sibling;
if (curr->key < next->key) {
linkTrees(next, curr);
curr = next;
} else {
linkTrees(curr, next);
}
}
next = curr->sibling;
}
return h;
}
void insert(BinomialHeap* heap, int key) {
BinomialHeap* temp = createHeap();
temp->head = createNode(key);
heap = unionHeaps(heap, temp);
}

Node* getMinNode(Node* head) {


Node* minNode = head;
Node* next = head->sibling;
while (next) {
if (next->key < minNode->key) minNode = next;
next = next->sibling;
}
return minNode;
}

int extractMin(BinomialHeap* heap) {


Node* minNode = getMinNode(heap->head);
if (!minNode) return -1;

Node* prev = NULL;


}
return heap;
}

struct Node *insert(struct Node *heap, int key) {


struct Node *newNode = createNode(key);
return adjustBinomialHeap(unionBinomialHeap(heap, newNode));
}

void display(struct Node *heap) {


while (heap) {
printf("Node with key: %d, Degree: %d\n", heap->key, heap->degree);
struct Node *child = heap->child;
while (child) {
printf(" -> Child Node: %d\n", child->key);
child = child->sibling;
}
heap = heap->sibling;
}
}
int main() {
struct Node *heap = NULL;
heap = insert(heap, 10);
heap = insert(heap, 20);
heap = insert(heap, 30);
heap = insert(heap, 15);
display(heap);
return 0;
}

Output
LAB-5

Objective: Implementation of an application of Dynamic Programming

#include <stdio.h>

int fibonacci(int n) {
int fib[n + 1];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}

int main() {
int n = 10;
printf("Fibonacci of %d is %d\n", n, fibonacci(n));
return 0;
}

Output
LAB-6

Objective: Implementation of an application of Greedy Algorthim

#include <stdio.h>

struct Item {
int weight;
int value;
};

void fractionalKnapsack(struct Item items[], int n, int capacity) {


float ratio[n];
for (int i = 0; i < n; i++)
ratio[i] = (float)items[i].value / items[i].weight;

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


for (int j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
float tempRatio = ratio[i];
ratio[i] = ratio[j];
ratio[j] = tempRatio;

struct Item tempItem = items[i];


items[i] = items[j];
items[j] = tempItem;
}
}
}

float totalValue = 0.0;


for (int i = 0; i < n; i++) {
if (capacity >= items[i].weight) {
capacity -= items[i].weight;
totalValue += items[i].value;
} else {
totalValue += items[i].value * ((float)capacity / items[i].weight);
break;
}
}
printf("Maximum value in knapsack = %.2f\n", totalValue);
}

int main() {
struct Item items[] = {{10, 60}, {20, 100}, {30, 120}};
int capacity = 50;
int n = sizeof(items) / sizeof(items[0]);

fractionalKnapsack(items, n, capacity);

return 0;
}

Output
LAB-7

Objective: Implementation of Minimum Spanning Tree Algorithm

#include <stdio.h>
#include <limits.h>

#define V 5

int minKey(int key[], int mstSet[]) {


int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (mstSet[v] == 0 && key[v] < min) {
min = key[v];
min_index = v;
}
return min_index;
}

void printMST(int parent[], int graph[V][V]) {


printf("Edge \tWeight\n");
for (int i = 1; i < V; i++)
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}

void primMST(int graph[V][V]) {


int parent[V];
int key[V];
int mstSet[V];

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


key[i] = INT_MAX;
mstSet[i] = 0;
}

key[0] = 0;
parent[0] = -1;

for (int count = 0; count < V - 1; count++) {


int u = minKey(key, mstSet);
mstSet[u] = 1;
for (int v = 0; v < V; v++)
if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}

printMST(parent, graph);
}

int main() {
int graph[V][V] = {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}};

primMST(graph);

return 0;
}

Output
LAB-8

Objective: Implementation of Single-Pair Shortest Path Algorthim

#include <stdio.h>
#include <limits.h>

#define V 5

int minDistance(int dist[], int visited[]) {


int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (visited[v] == 0 && dist[v] <= min) {
min = dist[v];
min_index = v;
}
return min_index;
}

void dijkstra(int graph[V][V], int src) {


int dist[V];
int visited[V];

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


dist[i] = INT_MAX;
visited[i] = 0;
}

dist[src] = 0;

for (int count = 0; count < V - 1; count++) {


int u = minDistance(dist, visited);
visited[u] = 1;

for (int v = 0; v < V; v++)


if (!visited[v] && graph[u][v] && dist[u] !=
INT_MAX && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}

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


printf("Distance from source to vertex %d: %d\n", i,
dist[i]);
}
int main() {
int graph[V][V] = {{0, 10, 0, 0,
5},
{0, 0, 1, 0, 2},
{0, 0, 0, 4, 0},
{7, 0, 6, 0, 0},
{0, 3, 9, 2, 0}};

dijkstra(graph, 0);

return 0;
}

Output
LAB-9

Objective: Implementation of All Pair Shortest Path Algorithm

#include <stdio.h>

#define V 4
#define INF 99999

void floydWarshall(int graph[V][V]) {


int dist[V][V];

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


for (int j = 0; j < V; j++)
dist[i][j] = graph[i][j];

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


for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}

printf("Shortest distances between every pair of vertices:\n");


for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
printf("%7s", "INF");
else
printf("%7d", dist[i][j]);
}
printf("\n");
}
}

int main() {
int graph[V][V] = {{0, 3, INF, 7},
{8, 0, 2, INF},
{5, INF, 0, 1},
{2, INF, INF, 0}};
floydWarshall(graph);

return 0;
}

Output
LAB-10

Objective: Implementation of String Matching Algorithm

#include <stdio.h>
#include <string.h>

void naiveStringMatching(char text[], char pattern[]) {


int textLength = strlen(text);
int patternLength = strlen(pattern);

for (int i = 0; i <= textLength - patternLength; i++) {


int j;
for (j = 0; j < patternLength; j++)
if (text[i + j] != pattern[j])
break;

if (j == patternLength)
printf("Pattern found at index %d\n", i);
}
}

int main() {
char text[] = "ABABDABACDABABCABAB";
char pattern[] = "ABABCABAB";

naiveStringMatching(text, pattern);

return 0;
}

Output

You might also like