Experiment (1)

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

Experiment No: 1

Aim: write a program of Linear search by using recursion .

Program:

#include <stdio.h>

int linearSearch(int arr[], int size, int key, int index) {

if (index >= size) {

return -1; // Base case: key not found

if (arr[index] == key) {

return index; // Base case: key found

return linearSearch(arr, size, key, index + 1);

int main() {

int n, key;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements of the array: \n", n);

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

scanf("%d", &arr[i]);

printf("Enter the element to search for: ");


scanf("%d", &key)

int result = linearSearch(arr, n, key, 0);

if (result != -1) {

printf("Element %d found at index %d.\n", key, result);

} else {

printf("Element %d not found in the array.\n", key);

return 0;

Output:

Enter the number of elements in the array: 5

Enter 5 elements of the array:

10 20 30 40 50

Enter the element to search for: 30

Element 30 found at index 2.

Experiment No: 2
Aim: Write a Program to perform Binary Search for a given set of integer values recursively

Program:

#include <stdio.h>

int binarySearch(int arr[], int low, int high, int key) {

if (low > high) {

return -1;

}
int mid = low + (high - low) / 2;

if (arr[mid] == key) {

return mid; // Element found

} else if (arr[mid] > key) {

return binarySearch(arr, low, mid - 1, key);

} else {

return binarySearch(arr, mid + 1, high, key);

int main() {

int n, key;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d sorted elements: \n", n);

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

scanf("%d", &arr[i]);

printf("Enter the element to search: ");


scanf("%d", &key);

int result = binarySearch(arr, 0, n - 1, key);

if (result != -1) {

printf("Element %d found at index %d.\n", key, result);

} else {

printf("Element %d not found in the array.\n", key);

return 0;

OUTPUT:

Enter the number of elements in the array: 5

Enter 5 sorted elements:

13579

Enter the element to search: 7

Element 7 found at index 3.

Experiment No: 3
Aim: Write a program to perform Quick Sort for the given list of integer values

Program:

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;


*a = *b;

*b = temp;

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

int pivot = arr[high];

int i = low - 1;

for (int j = low; j < high; j++) {

if (arr[j] <= pivot) {

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[high]);

return (i + 1);

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

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

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

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

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

printf("\n");

int main() {

int arr[] = {10, 7, 8, 9, 1, 5};

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

printf("Original array: \n");

printArray(arr, n);

quickSort(arr, 0, n - 1);

printf("\nSorted array: \n");

printArray(arr, n);

return 0;

OUTPUT:

If the input array is {10, 7, 8, 9, 1, 5}, the program prints:

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>

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

int n1 = mid - left + 1;

int n2 = right - mid;

int L[n1], R[n2];

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

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

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

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

int i = 0, j = 0, k = left;

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

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

arr[k] = L[i];

i++;
} else {

arr[k] = R[j];

j++;

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];

j++;

k++;

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

if (left < right) {

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

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);


}

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

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

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

printf("\n");

int main() {

int arr[] = {38, 27, 43, 3, 9, 82, 10};

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

printf("Original array: \n");

printArray(arr, size);

mergeSort(arr, 0, size - 1);

printf("\nSorted array: \n");

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>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

void heapify(int arr[], int n, int i) {

int largest = i;

int left = 2 * i + 1;

int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest]) {

largest = left;

if (right < n && arr[right] > arr[largest]) {

largest = right;

}
if (largest != i) {

swap(&arr[i], &arr[largest]);

heapify(arr, n, largest);

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

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

heapify(arr, n, i);

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

swap(&arr[0], &arr[i]);

heapify(arr, i, 0);

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

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

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

printf("\n");

int main() {

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

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


printf("Original array: \n");

printArray(arr, n);

heapSort(arr, n);

printf("\nSorted array: \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;

};

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

struct Item *item1 = (struct Item *)a;

struct Item *item2 = (struct Item *)b;

if (item1->ratio > item2->ratio) return -1;

if (item1->ratio < item2->ratio) return 1;

return 0;

double knapsack(struct Item items[], int n, int capacity) {

qsort(items, n, sizeof(struct Item), compare);

double totalValue = 0.0;

int currentWeight = 0;

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

if (currentWeight + items[i].weight <= capacity) {

currentWeight += items[i].weight;

totalValue += items[i].value;

} else {

int remainingCapacity = capacity - currentWeight;

totalValue += items[i].value * ((double)remainingCapacity / items[i].weight);

break;
}

return totalValue;

int main() {

int n = 4;

struct Item items[] = {

{10, 60, 0.0}, // {weight, value, ratio}

{20, 100, 0.0},

{30, 120, 0.0},

{5, 50, 0.0}

};

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

items[i].ratio = (double)items[i].value / items[i].weight;

int capacity = 50;

double maxValue = knapsack(items, n, capacity);

printf("Maximum value in knapsack = %.2f\n", maxValue);


return 0;

OUTPUT:

Input:

 Items: {weight = 10, value = 60}, {weight = 20, value = 100}, {weight = 30,
value = 120}, {weight = 5, value = 50}
 Knapsack capacity: 50

Output:

 Maximum value in knapsack: 240.00

Experiment No: 7
Aim: Write a program to find minimum cost spanning tree using Prim’s Algorithm..

Program:

#include <stdio.h>

#include <limits.h>

#define MAX 100

int findMinKeyVertex(int key[], int included[], int vertices) {

int min = INT_MAX, minIndex;

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

if (!included[v] && key[v] < min) {

min = key[v];

minIndex = v;
}

return minIndex;

void printMST(int parent[], int graph[MAX][MAX], int vertices) {

printf("Edge \tWeight\n");

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

printf("%d - %d \t%d\n", parent[i], i, graph[i][parent[i]]);

void primMST(int graph[MAX][MAX], int vertices) {

int parent[MAX];

int key[MAX];

int included[MAX];

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

key[i] = INT_MAX;

included[i] = 0;

}
key[0] = 0;

parent[0] = -1;

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

int u = findMinKeyVertex(key, included, vertices);

included[u] = 1;

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

if (graph[u][v] && !included[v] && graph[u][v] < key[v]) {

parent[v] = u;

key[v] = graph[u][v];

printMST(parent, graph, vertices);

int main() {

int vertices;

int graph[MAX][MAX];

printf("Enter the number of vertices: ");

scanf("%d", &vertices);
printf("Enter the adjacency matrix (use 0 for no edge):\n");

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

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

scanf("%d", &graph[i][j]);

primMST(graph, vertices);

return 0;

OUTPUT:

Enter the number of vertices: 4

Enter the adjacency matrix (use 0 for no edge):

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>

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

int min = INT_MAX, min_index;

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

if (sptSet[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]; // The shortest distance from source to each vertex

int sptSet[V]; // sptSet[i] will be true if vertex i is included in the shortest path tree

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

dist[i] = INT_MAX;

sptSet[i] = 0;

dist[src] = 0;
for (int count = 0; count < V - 1; count++) {

int u = minDistance(dist, sptSet);

sptSet[u] = 1;

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

if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {

dist[v] = dist[u] + graph[u][v];

printf("Vertex \t Distance from Source\n");

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

printf("%d \t %d\n", i, dist[i]);

int main() {

int graph[V][V] = {

{0, 4, 0, 0, 0, 0, 0, 8, 0},

{4, 0, 8, 0, 0, 0, 0, 11, 0},

{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},

{0, 0, 0, 9, 0, 10, 0, 0, 0},

{0, 0, 4, 14, 10, 0, 2, 0, 0},

{0, 0, 0, 0, 0, 2, 0, 1, 6},

{8, 11, 0, 0, 0, 0, 1, 0, 7},

{0, 0, 2, 0, 0, 0, 6, 7, 0}

};

int source = 0;

dijkstra(graph, source);

return 0;

OUTPUT:

Vertex Distance from Source

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>

#define INF 99999

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

int dist[V][V], i, j, k;

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

for (j = 0; j < V; j++) {

if (i == j) {

dist[i][j] = 0;

} else if (graph[i][j] != 0) {

dist[i][j] = graph[i][j];

} else {

dist[i][j] = INF;

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

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

for (j = 0; j < V; j++) {

if (dist[i][j] > dist[i][k] + dist[k][j]) {

dist[i][j] = dist[i][k] + dist[k][j];

}
printf("The shortest distance matrix is:\n");

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

for (j = 0; j < V; j++) {

if (dist[i][j] == INF) {

printf("INF ");

} else {

printf("%d ", dist[i][j]);

printf("\n");

int main() {

int graph[4][4] = {

{0, 3, INF, INF},

{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:

The shortest distance matrix is:

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

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

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

printf("%d ", board[i][j]);

printf("\n");

bool isSafe(int n, int row, int col) {

int i, j;

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

if (board[i][col] == 1) {

return false;

for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {

if (board[i][j] == 1) {

return false;

for (i = row, j = col; i >= 0 && j < n; i--, j++) {


if (board[i][j] == 1) {

return false;

return true;

bool solveNQueens(int n, int row) {

if (row == n) {

return true;

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

if (isSafe(n, row, col)) {

board[row][col] = 1;

if (solveNQueens(n, row + 1)) {

return true;

board[row][col] = 0;

return false;

}
int main() {

int n;

printf("Enter the value of N: ");

scanf("%d", &n);

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

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

board[i][j] = 0;

if (solveNQueens(n, 0)) {

printf("Solution to %d-Queens problem is:\n", n);

printSolution(n);

} else {

printf("No solution exists for %d-Queens problem.\n", n);

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.

You might also like