Experiment (1)
Experiment (1)
Experiment (1)
Program:
#include <stdio.h>
if (arr[index] == key) {
int main() {
int n, key;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
if (result != -1) {
} else {
return 0;
Output:
10 20 30 40 50
Experiment No: 2
Aim: Write a Program to perform Binary Search for a given set of integer values recursively
Program:
#include <stdio.h>
return -1;
}
int mid = low + (high - low) / 2;
if (arr[mid] == key) {
} else {
int main() {
int n, key;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
if (result != -1) {
} else {
return 0;
OUTPUT:
13579
Experiment No: 3
Aim: Write a program to perform Quick Sort for the given list of integer values
Program:
#include <stdio.h>
*b = temp;
int i = low - 1;
i++;
swap(&arr[i], &arr[j]);
return (i + 1);
quickSort(arr, pi + 1, high);
}
void printArray(int arr[], int size) {
printf("\n");
int main() {
printArray(arr, n);
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
OUTPUT:
Original array:
10 7 8 9 1 5
Sorted array:
1 5 7 8 9 10
Experiment No: 4
Aim: Write a program to perform Merge sort for any given list of numbers.
Program:
#include <stdio.h>
int i = 0, j = 0, k = left;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
printf("\n");
int main() {
printArray(arr, size);
printArray(arr, size);
return 0;
OUTPUT:
Input:
Original array:
38 27 43 3 9 82 10
Output:
Sorted array:
3 9 10 27 38 43 82
Experiment No: 5
Aim: Write a program to perform Heap sort for any given list of numbers.
Program:
#include <stdio.h>
*a = *b;
*b = temp;
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
largest = right;
}
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
heapify(arr, n, i);
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
printf("\n");
int main() {
printArray(arr, n);
heapSort(arr, n);
printArray(arr, n);
return 0;
OUTPUT:
Original array:
12 11 13 5 6 7
Sorted array:
5 6 7 11 12 13
Experiment No: 6
Aim: Write a program to find solution for knapsack problem using greedy method
Program:
#include <stdio.h>
struct Item {
int weight;
int value;
double ratio;
};
return 0;
int currentWeight = 0;
currentWeight += items[i].weight;
totalValue += items[i].value;
} else {
break;
}
return totalValue;
int main() {
int n = 4;
};
OUTPUT:
Input:
Items: {weight = 10, value = 60}, {weight = 20, value = 100}, {weight = 30,
value = 120}, {weight = 5, value = 50}
Knapsack capacity: 50
Output:
Experiment No: 7
Aim: Write a program to find minimum cost spanning tree using Prim’s Algorithm..
Program:
#include <stdio.h>
#include <limits.h>
min = key[v];
minIndex = v;
}
return minIndex;
printf("Edge \tWeight\n");
int parent[MAX];
int key[MAX];
int included[MAX];
key[i] = INT_MAX;
included[i] = 0;
}
key[0] = 0;
parent[0] = -1;
included[u] = 1;
parent[v] = u;
key[v] = graph[u][v];
int main() {
int vertices;
int graph[MAX][MAX];
scanf("%d", &vertices);
printf("Enter the adjacency matrix (use 0 for no edge):\n");
scanf("%d", &graph[i][j]);
primMST(graph, vertices);
return 0;
OUTPUT:
0206
2038
0300
6800
OUTPUT:
Edge Weight
0-1 2
1-2 3
0-3 6
Experiment No: 8
Aim:. Write a C program implementing Dijkstra's Algorithm
Program:
#include <stdio.h>
#include <limits.h>
min = dist[v];
min_index = v;
return min_index;
int sptSet[V]; // sptSet[i] will be true if vertex i is included in the shortest path tree
dist[i] = INT_MAX;
sptSet[i] = 0;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
sptSet[u] = 1;
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
int main() {
int graph[V][V] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
int source = 0;
dijkstra(graph, source);
return 0;
OUTPUT:
0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
Experiment No: 9
Aim: Write a program for Floyd-Warshall Algorithm in C
Program:
#include <stdio.h>
int dist[V][V], i, j, k;
if (i == j) {
dist[i][j] = 0;
} else if (graph[i][j] != 0) {
dist[i][j] = graph[i][j];
} else {
dist[i][j] = INF;
}
printf("The shortest distance matrix is:\n");
if (dist[i][j] == INF) {
printf("INF ");
} else {
printf("\n");
int main() {
int graph[4][4] = {
{3, 0, 1, INF},
{INF, 1, 0, 7},
{INF, INF, 7, 0}
};
int V = 4;
floydWarshall(graph, V);
return 0;
OUTPUT:
Input:
0---3---1
| |
INF 7
| |
3---INF---0
Output:
0 3 4 10
3018
4107
10 8 7 0
Experiment No: 10
Aim: Write a program to solve N-QUEENS problem.
Program:
#include <stdio.h>
#include <stdbool.h>
#define MAX 20
int board[MAX][MAX];
void printSolution(int n) {
printf("\n");
int i, j;
if (board[i][col] == 1) {
return false;
if (board[i][j] == 1) {
return false;
return false;
return true;
if (row == n) {
return true;
board[row][col] = 1;
return true;
board[row][col] = 0;
return false;
}
int main() {
int n;
scanf("%d", &n);
board[i][j] = 0;
if (solveNQueens(n, 0)) {
printSolution(n);
} else {
return 0;
OUTPUT:
Input:
The user enters the value of N, which is the size of the board (NxN).
Output:
If a solution exists, the program prints the board with the queens' positions. If no solution exists,
it prints a message saying no solution exists.