All Codes
All Codes
All Codes
#include <stdio.h>
// Main function
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("Sorted array: \n");
printArray(arr, n);
return 0;
}
Selection sort
#include <stdio.h>
// Main function
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Insertion sort
#include <stdio.h>
// Move elements of arr[0..i-1], that are greater than key, to one position ahead of their
current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// Function to print the 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};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
insertionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Heap Sort
#include <stdio.h>
// Main function
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("Sorted array: \n");
printArray(arr, n);
return 0;
}
Radix Sort
#include <stdio.h>
// Change count[i] so that count[i] now contains the actual position of this digit in output[]
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
// Perform counting sort for every digit, starting from the least significant digit
for (int exp = 1; max / exp > 0; exp *= 10) {
countSort(arr, n, exp);
}
}
// Main function
int main() {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
radixSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Bubble Sort
#include <stdio.h>
// Main function
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Knapsack
#include<stdio.h>
return K[n][W];
}
// Main function
int main() {
int val[] = {60, 100, 120};
int wt[] = {10, 20, 30};
int W = 50;
int n = sizeof(val) / sizeof(val[0]);
printf("Maximum value that can be obtained is: %d\n", knapSack(W, wt, val, n));
return 0;
}
N queen problem
#include<stdio.h>
return 1;
}
// Consider this column and try placing this queen in all rows one by one
for (int i = 0; i < N; i++) {
// Check if the queen can be placed on board[i][col]
if (isSafe(board, i, col)) {
// Place this queen in board[i][col]
board[i][col] = 1;
// If placing queen in board[i][col] doesn't lead to a solution, then remove queen from
board[i][col]
board[i][col] = 0; // Backtrack
}
}
// If the queen cannot be placed in any row in this column, then return false
return 0;
}
if (solveNQUtil(board, 0) == 0) {
printf("Solution does not exist");
return;
}
printSolution(board);
}
// Main function
int main() {
solveNQ();
return 0;
}
Tsp
#include <stdio.h>
#include <limits.h>
// Main function
int main() {
int graph[V][V] = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
// Mask to keep track of visited vertices, start from 1st vertex (0th index)
int mask = 1;
int pos = 0; // Starting position
return 0;
}
Huffman
#include <stdio.h>
#include <stdlib.h>
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
// Function to create a min heap of capacity equal to size and inserts all character of data[]
struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size) {
struct MinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
// Function to build Huffman Tree and print codes by traversing the built Huffman Tree
void HuffmanCodes(char data[], int freq[], int size) {
struct MinHeapNode* root = buildHuffmanTree(data, freq, size);
int arr[MAX_CHAR], top = 0;
printCodes(root, arr, top);
}
// Main function
int main() {
char data[] = {'a', 'b', 'c', 'd', 'e', 'f'};
int freq[] = {5, 9, 12, 13, 16, 45};
int size = sizeof(data) / sizeof(data[0]);
HuffmanCodes(data, freq, size);
return 0;
}
Floyd
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
printSolution(dist);
}
// Main function
int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = { {0, 5, INF, 10},
{INF, 0, 3, INF},
{INF, INF, 0, 1},
{INF, INF, INF, 0} };
floydWarshall(graph);
return 0;
}
Dijakstras
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
// Function to find the vertex with minimum distance value, from the set of vertices not yet
included in shortest path tree
int minDistance(int dist[], bool sptSet[]) {
int min = INT_MAX, min_index;
// Function to implement Dijkstra's algorithm for a graph represented using adjacency matrix
void dijkstra(int graph[V][V], int src) {
int dist[V]; // The output array. dist[i] will hold the shortest distance from src to i
bool sptSet[V]; // sptSet[i] will be true if vertex i is included in shortest path tree or shortest
distance from src to i is finalized
dijkstra(graph, 0);
return 0;
}
Ford fulk
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
// Function to implement Breadth-First Search (BFS) to find if there is a path from source to sink
bool bfs(int residualGraph[V][V], int source, int sink, int parent[]) {
bool visited[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// Create a residual graph and fill the residual graph with given capacities in the original graph
int residualGraph[V][V];
for (u = 0; u < V; u++)
for (v = 0; v < V; v++)
residualGraph[u][v] = graph[u][v];
// Update residual capacities of the edges and reverse edges along the path
for (v = sink; v != source; v = parent[v]) {
u = parent[v];
residualGraph[u][v] -= pathFlow;
residualGraph[v][u] += pathFlow;
}
return maxFlow;
}
// Main function
int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = { {0, 16, 13, 0, 0, 0},
{0, 0, 10, 12, 0, 0},
{0, 4, 0, 0, 14, 0},
{0, 0, 9, 0, 0, 20},
{0, 0, 0, 7, 0, 4},
{0, 0, 0, 0, 0, 0} };
Subset
#include <stdio.h>
#include <stdbool.h>
// Main function
int main() {
int set[] = {3, 34, 4, 12, 5, 2};
int sum = 9;
int n = sizeof(set) / sizeof(set[0]);
if (isSubsetSum(set, n, sum))
printf("Subset with sum %d exists\n", sum);
else
printf("No subset with sum %d exists\n", sum);
return 0;
}
Strassen
#include<stdio.h>
int newSize = n / 2;
int A11[newSize][newSize], A12[newSize][newSize], A21[newSize][newSize],
A22[newSize][newSize];
int B11[newSize][newSize], B12[newSize][newSize], B21[newSize][newSize],
B22[newSize][newSize];
int C11[newSize][newSize], C12[newSize][newSize], C21[newSize][newSize],
C22[newSize][newSize];
int P1[newSize][newSize], P2[newSize][newSize], P3[newSize][newSize],
P4[newSize][newSize], P5[newSize][newSize], P6[newSize][newSize], P7[newSize][newSize];
int temp1[newSize][newSize], temp2[newSize][newSize];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + newSize];
B21[i][j] = B[i + newSize][j];
B22[i][j] = B[i + newSize][j + newSize];
}
// Calculating P1 to P7 recursively
subtractMatrix(newSize, B12, B22, temp1);
strassen(newSize, A11, temp1, P1); // P1 = A11 * (B12 - B22)
// Main function
int main() {
int n = 4; // Size of the matrices (n x n)
int A[4][4] = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16} };
int B[4][4] = { {17, 18, 19, 20},
{21, 22, 23, 24},
{25, 26, 27, 28},
{29, 30, 31, 32} };
int C[4][4]; // Resultant matrix
printf("Matrix A:\n");
printMatrix(n, A);
printf("\n");
printf("Matrix B:\n");
printMatrix(n, B);
printf("\n");
return 0;
}
Bellman
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
// Main function
int main() {
// Example graph represented using edge list
struct Edge edges[] = {
// src, dest, weight
{0, 1, -1},
{0, 2, 4},
{1, 2, 3},
{1, 3, 2},
{1, 4, 2},
{3, 2, 5},
{3, 1, 1},
{4, 3, -3}
};
// Source vertex
int source = 0;
return 0;
}
Puzzle
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
return state;
}
// Function to calculate the heuristic value (total Manhattan distance) for a state
int calculateHeuristic(State state) {
int h = 0;
int value = 1;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
if (state.puzzle[i][j] != value && state.puzzle[i][j] != EMPTY_TILE) {
int targetRow = (state.puzzle[i][j] - 1) / N;
int targetCol = (state.puzzle[i][j] - 1) % N;
h += manhattanDistance(i, j, targetRow, targetCol);
}
value++;
}
return h;
}
// Priority queue or any suitable data structure to store states based on their cost + heuristic
value
// Implementing a basic linear search for simplicity
State queue[10000];
int front = -1, rear = -1;
queue[++rear] = initial_state;
if (isGoalState(current)) {
printf("Goal State Reached!\n");
printPuzzle(current);
return;
}
// Main function
int main() {
int puzzle[N][N] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 0} }; // Example puzzle configuration
solvePuzzle(puzzle);
return 0;
}
Graph color
#include <stdio.h>
#include <stdbool.h>
return false;
}
// Main function
int main() {
bool graph[V][V] = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}
};
int m = 3; // Number of colors
graphColoring(graph, m);
return 0;
}
Merge sort
#include <stdio.h>
// Function to merge two subarrays arr[l..m] and arr[m+1..r] into one sorted array
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1; // Size of the left subarray
int n2 = r - m; // Size of the right subarray
// Main function
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);
printf("Given array is \n");
printArray(arr, arr_size);
return 0;
}
Linear search
#include <stdio.h>
// Main function
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 10;
if (index != -1) {
printf("Element %d found at index %d\n", target, index);
} else {
printf("Element %d not found in the array\n", target);
}
return 0;
}
B