0% found this document useful (0 votes)
6 views9 pages

SQL

The document contains implementations of three algorithms: Merge Sort, 0/1 Knapsack Problem, and Kruskal's Algorithm. Each algorithm is presented with C code, demonstrating how to sort an array, maximize value in a knapsack, and find the minimum spanning tree of a graph, respectively. The outputs of the algorithms are also provided, showing the sorted array, maximum value in the knapsack, and edges in the minimum spanning tree.

Uploaded by

suriyaganesh128
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

SQL

The document contains implementations of three algorithms: Merge Sort, 0/1 Knapsack Problem, and Kruskal's Algorithm. Each algorithm is presented with C code, demonstrating how to sort an array, maximize value in a knapsack, and find the minimum spanning tree of a graph, respectively. The outputs of the algorithms are also provided, showing the sorted array, maximum value in the knapsack, and edges in the minimum spanning tree.

Uploaded by

suriyaganesh128
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MERGE SORT PGM

#include <stdio.h>

// Function to merge two subarrays

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

int i, j, k;

int n1 = mid - left + 1;

int n2 = right - mid;

// Create temp arrays

int L[n1], R[n2];

// Copy data to temp arrays

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

L[i] = arr[left + i];

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

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

// Merge the temp arrays back into arr

i = 0; // Initial index of first subarray

j = 0; // Initial index of second subarray

k = left; // Initial index of merged array

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

if (L[i] <= R[j]) {


arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++;

k++;

// Copy remaining elements of L[], if any

while (i < n1) {

arr[k] = L[i];

i++;

k++;

// Copy remaining elements of R[], if any

while (j < n2) {

arr[k] = R[j];

j++;

k++;

// Function to perform merge sort


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

if (left < right) {

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

// Sort first and second halves

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);

// Function to print an array

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

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

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

printf("\n");

// Main function

int main() {

int arr[] = {12, 11, 13, 5, 6, 7};

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

printf("Original array:\n");
printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("Sorted array:\n");

printArray(arr, arr_size);

return 0;

0/1 Knapsack Problem using Dynamic programming


#include <stdio.h>

// Function to get the maximum of two integers

int max(int a, int b) {

return (a > b) ? a : b;

// Function to solve 0/1 Knapsack problem

int knapsack(int W, int wt[], int val[], int n) {

int i, w;

int dp[n + 1][W + 1];


// Build table dp[][] in bottom-up manner

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

for (w = 0; w <= W; w++) {

if (i == 0 || w == 0)

dp[i][w] = 0;

else if (wt[i - 1] <= w)

dp[i][w] = max(val[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]);

else

dp[i][w] = dp[i - 1][w];

return dp[n][W];

int main() {

int val[] = {60, 100, 120}; // Values (profits)

int wt[] = {10, 20, 30}; // Weights

int W = 50; // Maximum weight of knapsack

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

printf("Maximum value in Knapsack = %d\n", knapsack(W, wt, val, n));

return 0;
}

Output:

Maximum value in Knapsack = 220

KRUSHKALS ALGORITHM

#include <stdio.h>

// Structure to represent an edge

struct Edge {

int src, dest, weight;

};

int parent[4];

// Function to find the parent (for cycle detection)

int find(int i) {

while (parent[i] != i)

i = parent[i];

return i;
}

// Function to do union of two subsets

void union_set(int i, int j) {

int a = find(i);

int b = find(j);

parent[a] = b;

int main() {

// 4 vertices: A=0, B=1, C=2, D=3

struct Edge edges[] = {

{0, 1, 1}, // A-B

{1, 3, 2}, // B-D

{0, 3, 3}, // A-D

{0, 2, 4}, // A-C

{2, 3, 5} // C-D

};

int n = 5; // Number of edges

int i, j;

// Initialize each vertex as its own parent

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

parent[i] = i;
// Sort edges based on weight (simple bubble sort for small size)

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

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

if (edges[j].weight > edges[j+1].weight) {

struct Edge temp = edges[j];

edges[j] = edges[j+1];

edges[j+1] = temp;

int totalWeight = 0;

printf("Edges in Minimum Spanning Tree:\n");

// Pick edges one by one

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

int u = edges[i].src;

int v = edges[i].dest;

int pu = find(u);

int pv = find(v);

if (pu != pv) { // No cycle

printf("%c -- %c == %d\n", u + 'A', v + 'A', edges[i].weight);

totalWeight += edges[i].weight;
union_set(pu, pv);

printf("Total weight of MST = %d\n", totalWeight);

return 0;

Output:

Edges in Minimum Spanning Tree:

A -- B == 1

B -- D == 2

A -- D == 3

Total weight of MST = 6

You might also like