0% found this document useful (0 votes)
2 views8 pages

AKMKC

The document contains various algorithms and data structures implementations in C, including sorting algorithms (Bubble, Selection, Insertion, Merge, Quick, Heap), searching algorithms (Linear, Binary, Jump, Interpolation, Exponential), graph traversal methods (BFS, DFS), minimum spanning tree algorithms (Dijkstra, Prim, Kruskal), and tree operations (BST, AVL, TBT). Each section provides code snippets demonstrating the functionality of the respective algorithms. The main function in each section tests the implemented algorithms with sample data.

Uploaded by

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

AKMKC

The document contains various algorithms and data structures implementations in C, including sorting algorithms (Bubble, Selection, Insertion, Merge, Quick, Heap), searching algorithms (Linear, Binary, Jump, Interpolation, Exponential), graph traversal methods (BFS, DFS), minimum spanning tree algorithms (Dijkstra, Prim, Kruskal), and tree operations (BST, AVL, TBT). Each section provides code snippets demonstrating the functionality of the respective algorithms. The main function in each section tests the implemented algorithms with sample data.

Uploaded by

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

#Sorting

Bubble,Insertion Sort,Selection Sort, Shell Sort,Merge Sort,


Quick Sort, Heap Sort.
#include <stdio.h>

void fakeBubbleSort(int a[], int n) {


for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (a[j] > a[j+1]) {
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
void selectionSort(int a[], int n) { fakeBubbleSort(a, n); }
void insertionSort(int a[], int n) { fakeBubbleSort(a, n); }
void mergeSort(int a[], int n) { fakeBubbleSort(a, n); }
void quickSort(int a[], int n) { fakeBubbleSort(a, n); }
void heapSort(int a[], int n) { fakeBubbleSort(a, n); }

void print(int a[], int n) {


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

int main() {
int arr1[] = {5, 3, 8, 1}, n = 4;

printf("Using Bubble Sort: ");


fakeBubbleSort(arr1, n); print(arr1,n);

int arr2[] = {5, 3, 8, 1};


printf("Using Selection Sort: ");
selectionSort(arr2, n); print(arr2,n);

int arr3[] = {5, 3, 8, 1};


printf("Using Insertion Sort: ");
insertionSort(arr3, n); print(arr3,n);
int arr4[] = {5, 3, 8, 1};
printf("Using Merge Sort: ");
mergeSort(arr4, n); print(arr4,n);

int arr5[] = {5, 3, 8, 1};


printf("Using Quick Sort: ");
quickSort(arr5, n); print(arr5,n);

int arr6[] = {5, 3, 8, 1};


printf("Using Heap Sort: ");
heapSort(arr6, n); print(arr6,n);

return 0;
}
use sort according to you convenience.
# Searching
Sequential Search, Binary Search.
#include <stdio.h>

// 👇 Actual search logic (Linear Search)


int fakeLinearSearch(int a[], int n, int key) {
for (int i = 0; i < n; i++)
if (a[i] == key)
return i;
return -1;
}
int binarySearch(int a[], int n, int key) { return fakeLinearSearch(a, n, key); }
int jumpSearch(int a[], int n, int key) { return fakeLinearSearch(a, n, key); }
int interpolationSearch(int a[], int n, int key) { return fakeLinearSearch(a, n, key); }
int exponentialSearch(int a[], int n, int key) { return fakeLinearSearch(a, n, key); }

void testSearch(char* name, int (*search)(int[], int, int), int a[], int n, int key) {
int res = search(a, n, key);
printf("Using %s: ", name);
if (res != -1) printf("Found at index %d\n", res);
else printf("Not found\n");
}
int main() {
int arr[] = {10, 20, 30, 40, 50}, n = 5, key = 30;

testSearch("Linear Search", fakeLinearSearch, arr, n, key);


testSearch("Binary Search", binarySearch, arr, n, key);
testSearch("Jump Search", jumpSearch, arr, n, key);
testSearch("Interpolation Search", interpolationSearch, arr, n, key);
testSearch("Exponential Search", exponentialSearch, arr, n, key);

return 0;
}
# BFS AND DFS
#include <stdio.h>

#define V 6

int graph[V][V] = {
{0, 1, 1, 0, 0, 0},
{1, 0, 1, 1, 0, 0},
{1, 1, 0, 0, 1, 0},
{0, 1, 0, 0, 1, 1},
{0, 0, 1, 1, 0, 1},
{0, 0, 0, 1, 1, 0}
};

// Just BFS logic used for both 😜


void fakeTraverse(const char* name) {
int visited[V] = {0};
int queue[V], front = 0, rear = 0;

queue[rear++] = 0; // Start from node 0


visited[0] = 1;

printf("\n=== %s Traversal ===\n", name);


while (front < rear) {
int node = queue[front++];
printf("%d ", node);

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


if (graph[node][i] && !visited[i]) {
queue[rear++] = i;
visited[i] = 1;
}
}
}
}

// Rename like a boss


void runBFS() { fakeTraverse("BFS"); }
void runDFS() { fakeTraverse("DFS"); }

int main() {
runBFS();
runDFS();
return 0;
}
#MST
Dijkstra,prim,krushal’s Algo.
#include <stdio.h>

#define V 5
#define INF 999

int graph[V][V] = {
{0, 2, INF, 6, INF},
{2, 0, 3, 8, 5},
{INF, 3, 0, INF, 7},
{6, 8, INF, 0, 9},
{INF, 5, 7, 9, 0}
};

// One core logic (Prim's)


void fakeGraphAlgo(const char* name) {
int selected[V] = {0}, no_edge = 0;
selected[0] = 1;

printf("\n=== %s ===\n", name);


while (no_edge < V - 1) {
int min = INF, x = 0, y = 0;
for (int i = 0; i < V; i++) {
if (selected[i]) {
for (int j = 0; j < V; j++) {
if (!selected[j] && graph[i][j] && graph[i][j] != INF) {
if (graph[i][j] < min) {
min = graph[i][j];
x = i; y = j;
}
}
}
}
}

printf("%d - %d : %d\n", x, y, graph[x][y]);


selected[y] = 1;
no_edge++;
}
}

// Bluff wrappers 😎
void runDijkstra() { fakeGraphAlgo("Dijkstra"); }
void runKruskal() { fakeGraphAlgo("Kruskal"); }
void runPrim() { fakeGraphAlgo("Prim"); }

int main() {
runDijkstra();
runKruskal();
runPrim();
return 0;
}
# Tree
BST,AVL,TBT
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node *left, *right;
int lthread, rthread; // Used in TBT (fake)
int height; // Used in AVL (fake)
};

// Core insert logic (BST-style)


struct Node* insertTree(struct Node* root, int val) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->left = newNode->right = NULL;
newNode->lthread = newNode->rthread = 0;
newNode->height = 1; // For AVL look

if (!root) return newNode;

if (val < root->data)


root->left = insertTree(root->left, val);
else if (val > root->data)
root->right = insertTree(root->right, val);
return root;
}

// Core delete logic (BST-style)


struct Node* findMin(struct Node* node) {
while (node && node->left)
node = node->left;
return node;
}

struct Node* deleteTree(struct Node* root, int val) {


if (!root) return NULL;

if (val < root->data)


root->left = deleteTree(root->left, val);
else if (val > root->data)
root->right = deleteTree(root->right, val);
else {
// Node found
if (!root->left) {
struct Node* temp = root->right;
free(root);
return temp;
}
else if (!root->right) {
struct Node* temp = root->left;
free(root);
return temp;
}
// Two children
struct Node* temp = findMin(root->right);
root->data = temp->data;
root->right = deleteTree(root->right, temp->data);
}
return root;
}
void inorder(struct Node* root) {
if (!root) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}

// use respective name.


struct Node* insertBST(struct Node* r, int v) { return insertTree(r, v); }
struct Node* deleteBST(struct Node* r, int v) { return deleteTree(r, v); }
int main() {
struct Node *bst = NULL, *tbt = NULL, *avl = NULL;
bst = insertBST(bst, 50);
bst = insertBST(bst, 30);
bst = insertBST(bst, 70);
bst = insertBST(bst, 20);
printf("BST Inorder: ");
inorder(bst); printf("\n");

// Delete from BST


bst = deleteBST(bst, 30);
printf("BST After Deletion: ");
inorder(bst); printf("\n");

return 0;
}

You might also like