0% found this document useful (0 votes)
3 views10 pages

ada exps

The document contains multiple C programming implementations of various algorithms, including selection sort, topological sort, merge sort, quick sort, knapsack problem, Prim's algorithm, Dijkstra's algorithm, and Kruskal's algorithm. Each section provides code for the algorithm, along with user input prompts and output results, demonstrating the functionality of sorting and graph algorithms. The implementations focus on efficiency and include time measurement for sorting algorithms.

Uploaded by

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

ada exps

The document contains multiple C programming implementations of various algorithms, including selection sort, topological sort, merge sort, quick sort, knapsack problem, Prim's algorithm, Dijkstra's algorithm, and Kruskal's algorithm. Each section provides code for the algorithm, along with user input prompts and output results, demonstrating the functionality of sorting and graph algorithms. The implementations focus on efficiency and include time measurement for sorting algorithms.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>
#include <time.h>

void selectionSort(int array[], int size) {


int i, j, min, temp;
for (i = 0; i < size - 1; i++) {
min = i;
for (j = i + 1; j < size; j++) {
if (array[j] < array[min])
min = j;
}
temp = array[min];
array[min] = array[i];
array[i] = temp;
}
}

int main() {
int n, a[20000], k;
clock_t st, et;
double ts;

printf("\nEnter how many numbers: ");


scanf("%d", &n);

printf("The random numbers are:\n");


for (k = 0; k < n; k++) {
a[k] = rand();
printf("%d ", a[k]);
}

st = clock();
selectionSort(a, n);
et = clock();

ts = (double)(et - st) / CLOCKS_PER_SEC;


printf("\nSorted elements:\n");
for (k = 0; k < n; k++)
printf("%d ", a[k]);

printf("\nTime taken to sort %d elements = %f seconds\n", n, ts);

return 0;
}

2.Topological Sort

#include <stdio.h>

int a[10][10], n, indegree[10];


void find_indegree() {
int i, j, sum;
for (j = 0; j < n; j++) {
sum = 0;
for (i = 0; i < n; i++) {
sum += a[i][j];
}
indegree[j] = sum;
}
}
void topology() {
int i, u, v, t[10], s[10], top = -1, k = 0;

find_indegree();
for (i = 0; i < n; i++) {
if (indegree[i] == 0)
s[++top] = i;
}

while (top != -1) {


u = s[top--]; // Pop vertex from stack
t[k++] = u; // Add to result

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


if (a[u][v] == 1) {
indegree[v]--; // Remove edge u → v
if (indegree[v] == 0) {
s[++top] = v; // Push if v has no more incoming edges
}
}
}
}

printf("The Topological Sequence is:\n");


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

int main() {
int i, j;

printf("Enter number of jobs: ");


scanf("%d", &n);

printf("Enter the adjacency matrix:\n");


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

topology(); // Perform topological sorting


return 0;
}

3. Merge sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void Merge(int a[], int low, int mid, int high) {


int i = low, j = mid + 1, k = low;
int b[20000]; // Temporary array to store merged result

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


if (a[i] <= a[j])
b[k++] = a[i++];
else
b[k++] = a[j++];
}

while (i <= mid)


b[k++] = a[i++];

while (j <= high)


b[k++] = a[j++];

for (i = low; i <= high; i++)


a[i] = b[i];
}

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


if (low >= high)
return;

int mid = (low + high) / 2;


MergeSort(a, low, mid);
MergeSort(a, mid + 1, high);
Merge(a, low, mid, high);
}

int main() {
int n, a[20000];
clock_t st, et;
double ts;

printf("Enter how many numbers: ");


scanf("%d", &n);

if (n > 20000) {
printf("Maximum limit is 20000. Exiting.\n");
return 1;
}

printf("The random numbers are:\n");


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

st = clock();
MergeSort(a, 0, n - 1);
et = clock();
ts = (double)(et - st) / CLOCKS_PER_SEC;

printf("\n\nThe sorted numbers are:\n");


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

printf("\n\nThe time taken is: %f seconds\n", ts);

return 0;
}

4. Quick Sort

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

// Swap two integers


void Exch(int *p, int *q) {
int temp = *p;
*p = *q;
*q = temp;
}

// QuickSort function
void QuickSort(int a[], int low, int high) {
int i, j, key;

if (low >= high)


return;

key = low;
i = low + 1;
j = high;

while (i <= j) {
while (i <= high && a[i] <= a[key])
i++;
while (j >= low && a[j] > a[key])
j--;

if (i < j)
Exch(&a[i], &a[j]);
}

Exch(&a[j], &a[key]);

QuickSort(a, low, j - 1); // Sort left partition


QuickSort(a, j + 1, high); // Sort right partition
}

int main() {
int n, a[10000], k;
clock_t st, et;
double ts;

printf("Enter how many numbers: ");


scanf("%d", &n);

if (n > 10000) {
printf("Maximum supported limit is 10000\n");
return 1;
}

printf("The Random Numbers are:\n");


for (k = 0; k < n; k++) {
a[k] = rand();
printf("%d ", a[k]);
}

st = clock();
QuickSort(a, 0, n - 1);
et = clock();

ts = (double)(et - st) / CLOCKS_PER_SEC;

printf("\n\nThe Sorted Numbers are:\n");


for (k = 0; k < n; k++) {
printf("%d ", a[k]);
}

printf("\n\nThe time taken is %f seconds\n", ts);


return 0;
}

5. Knapsack

#include <stdio.h>

int w[10], p[10], v[10], knap[10][10], x[10], n; // Weights, Profits, used flags,
solution table, solution items
int max(int i, int j) {
return (i > j) ? i : j;
}

int knapsack(int i, int j) {


int value;

if (v[i] != 0)
return knap[i][j];

if (i == 0 || j == 0)
value = 0;
else if (j < w[i])
value = knapsack(i - 1, j);
else
value = max(knapsack(i - 1, j), p[i] + knapsack(i - 1, j - w[i]));

knap[i][j] = value;
v[i] = 1;

return knap[i][j];
}

int main() {
int profit = 0, count = 0, i, j, cap;

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


scanf("%d", &n);

printf("Enter the profit and weights of the elements:\n");


for (i = 1; i <= n; i++) {
printf("For item no %d:\n", i);
scanf("%d %d", &p[i], &w[i]);
}

printf("Enter the capacity:\n");


scanf("%d", &cap);

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


for (j = 0; j <= cap; j++)
knap[i][j] = 0;

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


v[i] = 0;

profit = knapsack(n, cap);


i = n;
j = cap;

while (j != 0 && i != 0) {
if (v[i] && (knap[i][j] != knap[i - 1][j])) {
x[i] = 1;
j = j - w[i];
count++;
} else {
x[i] = 0;
}
i--;
}

printf("Items included are:\n");


printf("Sl.no\tWeight\tProfit\n");

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


if (x[i] == 1)
printf("%d\t%d\t%d\n", i, w[i], p[i]);
}

printf("Total profit = %d\n", profit);


return 0;
}

7. Prims
#include <stdio.h>

int a, b, u, v, n, i, j, ne = 1;
int visited[10] = {0}, min, mincost = 0, cost[10][10];

void main() {
printf("\nEnter the number of nodes: ");
scanf("%d", &n);

printf("\nEnter the adjacency matrix:\n");


for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999; // Representing no edge as a large number
}
}

visited[1] = 1; // Start from node 1


printf("\n");

while (ne < n) {


min = 999;

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


for (j = 1; j <= n; j++) {
if (cost[i][j] < min) {
if (visited[i] != 0) {
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}
}

if (visited[u] == 0 || visited[v] == 0) {
printf("Edge %d: (%d %d) cost: %d\n", ne++, a, b, min);
mincost += min;
visited[b] = 1;
}

cost[a][b] = cost[b][a] = 999; // Mark edge as used


}

printf("\nMinimum cost = %d\n", mincost);


}

9. Dijkstra’s Algorithm:

#include <stdio.h>
#define infinity 999

void dijk(int n, int v, int cost[10][10], int dist[100]) {


int i, u, count, w, flag[10], min;

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


flag[i] = 0;
dist[i] = cost[v][i];
}

count = 2;

while (count <= n) {


min = 99;
for (w = 1; w <= n; w++) {
if (dist[w] < min && !flag[w]) {
min = dist[w];
u = w;
}
}

flag[u] = 1;
count++;

for (w = 1; w <= n; w++) {


if ((dist[u] + cost[u][w] < dist[w]) && !flag[w]) {
dist[w] = dist[u] + cost[u][w];
}
}
}
}

int main() {
int n, v, i, j, cost[10][10], dist[10];

printf("\n Enter the number of nodes: ");


scanf("%d", &n);

printf("\n Enter the cost matrix:\n");


for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = infinity;
}
}

printf("\n Enter the source node: ");


scanf("%d", &v);

dijk(n, v, cost, dist);

printf("\nShortest paths from node %d:\n", v);


for (i = 1; i <= n; i++) {
if (i != v)
printf("%d -> %d = %d\n", v, i, dist[i]);
}

return 0;
}

8. Kruskal’s

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

int i, j, k, a, b, u, v, n, ne = 1;
int min, mincost = 0, cost[9][9], parent[9];

int find(int i) {
while (parent[i])
i = parent[i];
return i;
}

int uni(int i, int j) {


if (i != j) {
parent[j] = i;
return 1;
}
return 0;
}

void main() {
printf("\nEnter the number of vertices: ");
scanf("%d", &n);

printf("\nEnter the cost adjacency matrix:\n");


for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}

printf("\nThe edges of Minimum Cost Spanning Tree are:\n");


while (ne < n) {
min = 999;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (cost[i][j] < min) {
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}

u = find(u);
v = find(v);

if (uni(u, v)) {
printf("%d edge (%d,%d) = %d\n", ne++, a, b, min);
mincost += min;
}

cost[a][b] = cost[b][a] = 999;


}

printf("\nMinimum cost = %d\n", mincost);


}

You might also like