Compare commits

...

9 Commits

Author SHA1 Message Date
71b78d17d6 exp 14: correct wrong algorithm name
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2023-01-03 14:22:22 +05:30
5f936b76bb exp 14: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2023-01-03 14:08:29 +05:30
158a5eeadb exp 13: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-29 18:15:25 +05:30
6e31a2402f exp 10: call bubble_sort
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-28 05:58:17 +05:30
869d3051f2 exp 9: add delete
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-28 05:26:10 +05:30
1b9c98247f exp 4: cleanup
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-28 04:28:47 +05:30
6b6012ab05 exp 12: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-27 22:12:08 +05:30
5f95c60e19 exp 10, 11: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-27 21:41:56 +05:30
4ee6a026bf exp 9: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-27 20:25:35 +05:30
14 changed files with 564 additions and 11 deletions

181
10.c Normal file
View File

@@ -0,0 +1,181 @@
#include <math.h>
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void selection_sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int mn = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[mn])
mn = j;
swap(&arr[i], &arr[mn]);
}
}
void insertion_sort(int arr[], int n) {
for (int i = 0; i < n; i++) {
int j = i - 1, x = arr[i];
while (j >= 0 && arr[j] > x) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = x;
}
}
void bubble_sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int swapped = 0;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
swapped = 1;
}
}
if (!swapped) {
break;
}
}
}
void merge_sort_helper(int arr[], int b, int m, int e) {
int i = 0, j = 0, k = b;
int l1 = m - b + 1, l2 = e - m;
int l[l1], r[l2];
for (int x = 0; x < l1; x++)
l[x] = arr[b + x];
for (int x = 0; x < l2; x++)
r[x] = arr[m + 1 + x];
while (i < l1 && j < l2) {
if (l[i] <= r[j])
arr[k++] = l[i++];
else
arr[k++] = r[j++];
}
while (i < l1)
arr[k++] = l[i++];
while (j < l2)
arr[k++] = r[j++];
}
void merge_sort(int arr[], int b, int e) {
if (b >= e)
return;
int m = b + (e - b) / 2;
merge_sort(arr, b, m);
merge_sort(arr, m + 1, e);
merge_sort_helper(arr, b, m, e);
}
int quick_sort_helper(int arr[], int b, int e) {
int p = arr[e];
int i = b - 1;
for (int j = b; j < e; j++) {
if (arr[j] < p) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[e]);
return i + 1;
}
void quick_sort(int arr[], int b, int e) {
if (b < e) {
int p = quick_sort_helper(arr, b, e);
quick_sort(arr, b, p - 1);
quick_sort(arr, p + 1, e);
}
}
void heap_sort_helper(int arr[], int n, int i) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int largest = i;
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]);
heap_sort_helper(arr, n, largest);
}
}
void heap_sort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heap_sort_helper(arr, n, i);
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heap_sort_helper(arr, i, 0);
}
}
void display(int arr[], int n) {
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
const int SIZE = 5;
int arr1[] = {65, 65, 65, 1, 1};
printf("Insertion Sort on: ");
display(arr1, SIZE);
insertion_sort(arr1, SIZE);
display(arr1, SIZE);
int arr2[] = {-4, 91, 300, 912, 8};
printf("Selection Sort on: ");
display(arr2, SIZE);
selection_sort(arr2, SIZE);
display(arr2, SIZE);
int arr3[] = {11, -2, 0, 0, -3};
printf("Merge Sort on: ");
display(arr3, SIZE);
merge_sort(arr3, 0, SIZE - 1);
display(arr3, SIZE);
int arr4[] = {99999, 9999, 999, 99, 9};
printf("Quick Sort on: ");
display(arr4, SIZE);
quick_sort(arr4, 0, SIZE - 1);
display(arr4, SIZE);
int arr5[] = {-9, -99, -999, -9999, -99999};
printf("Heap Sort on: ");
display(arr5, SIZE);
heap_sort(arr5, SIZE);
display(arr5, SIZE);
int arr6[] = {1, 10, -100000, 1, -2};
printf("Bubble Sort on: ");
display(arr6, SIZE);
bubble_sort(arr6, SIZE);
display(arr6, SIZE);
return 0;
}

BIN
10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

57
11.c Normal file
View File

@@ -0,0 +1,57 @@
#include <stdio.h>
int linear(int target, int arr[], int n) {
for (int i = 0; i < n; i++)
if (arr[i] == target)
return i;
return -1;
}
int binary(int target, int arr[], int b, int e) {
if (b > e)
return -1;
int m = b + (e - b) / 2;
if (arr[m] == target)
return m;
if (arr[m] > target)
return binary(target, arr, b, m - 1);
else
return binary(target, arr, m + 1, e);
return -1;
}
int main() {
int const SIZE = 5;
int arr[5], target, index;
printf("------\nLinear:\n------\n");
printf("Enter %d integers for the array:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
printf("Element %d: ", i);
scanf("%d", arr + i);
}
printf("Enter element to find: ");
scanf("%d", &target);
index = linear(target, arr, SIZE);
if (index == -1)
printf("Element not found\n");
else
printf("Element %d found at index %d\n", target, index);
printf("------\nBinary:\n------\n");
printf("Enter %d SORTED integers for the array:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
printf("Element %d: ", i);
scanf("%d", arr + i);
}
printf("Enter element to find: ");
scanf("%d", &target);
index = binary(target, arr, 0, SIZE - 1);
if (index == -1)
printf("Element not found\n");
else
printf("Element %d found at index %d\n", target, index);
}

BIN
11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

48
12.c Normal file
View File

@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Item item;
struct Item {
unsigned int key;
int value;
};
unsigned int hash(char *x, unsigned int n) {
const unsigned int offset = 2166136261;
unsigned int hash = 0;
for (unsigned int i = 0; i < n; x++, i++) {
hash *= offset;
hash ^= (*x);
}
return hash;
}
int search(item items[], int n, char *x, int xn) {
unsigned int key = hash(x, xn);
for (int i = 0; i < n; i++) {
if (items[i].key == key)
return items[i].value;
}
printf("Not found");
exit(1);
}
int main() {
int const SIZE = 4;
item items[SIZE];
items[0].key = hash("nita", 4);
items[0].value = 99;
items[1].key = hash("amaang", 5);
items[1].value = -129;
items[2].key = hash("unreal", 6);
items[2].value = 4;
items[3].key = hash("alpaviraam", 10);
items[3].value = 10000;
char x[] = "unreal";
printf("Value for string %s is: %d", x, search(items, SIZE, x, 6));
}

BIN
12.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

136
13.c Normal file
View File

@@ -0,0 +1,136 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node node;
struct Node {
int vertex;
struct Node *next;
};
typedef struct List list;
struct List {
node *head;
node *tail;
};
typedef struct Graph graph;
struct Graph {
int nvertices;
int *visited;
list *vertices;
};
graph *init_graph(int n) {
graph *graph = malloc(sizeof(struct Graph));
graph->nvertices = n;
graph->vertices = malloc(n * sizeof(struct List));
graph->visited = malloc(n * sizeof(int));
memset(graph->visited, 0, sizeof(int) * n);
return graph;
}
void insert(list *list, int vertex) {
node *new = malloc(sizeof(struct Node));
new->next = NULL;
new->vertex = vertex;
if (!list->head)
list->head = list->tail = new;
else
list->tail->next = new, list->tail = new;
}
void pop(list *list) {
if (!list->head)
return;
node *next = list->head->next;
free(list->head);
if (!next)
list->tail = next;
list->head = next;
}
void add_edge(graph *graph, int src, int dst) {
insert(&graph->vertices[src], dst);
insert(&graph->vertices[dst], src);
}
void dfs(struct Graph *graph, int vertex) {
list list = graph->vertices[vertex];
printf("%d ", vertex);
graph->visited[vertex] = 1;
node *cur = list.head;
while (cur) {
if (!graph->visited[cur->vertex])
dfs(graph, cur->vertex);
cur = cur->next;
}
}
void bfs(struct Graph *graph, int _vertex) {
list next;
next.head = next.tail = NULL;
graph->visited[_vertex] = 1;
insert(&next, _vertex);
while (next.head) {
int vertex = next.head->vertex;
list list = graph->vertices[vertex];
node *cur = list.head;
printf("%d ", vertex);
pop(&next);
while (cur) {
if (!graph->visited[cur->vertex]) {
graph->visited[cur->vertex] = 1;
insert(&next, cur->vertex);
}
cur = cur->next;
}
}
}
int main() {
const int ROOT = 0;
const int SIZE = 5;
struct Graph *graph = init_graph(SIZE);
add_edge(graph, 0, 1);
add_edge(graph, 0, 3);
add_edge(graph, 0, 4);
add_edge(graph, 2, 3);
add_edge(graph, 1, 3);
printf("DFS: ");
dfs(graph, ROOT);
printf("\n");
memset(graph->visited, 0, sizeof(int) * graph->nvertices);
printf("BFS: ");
bfs(graph, ROOT);
printf("\n");
// free
for (int i = 0; i < graph->nvertices; i++)
while(graph->vertices[i].head)
pop(&graph->vertices[i]);
free(graph->visited);
free(graph->vertices);
free(graph);
return 0;
}

BIN
13.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

30
14.c Normal file
View File

@@ -0,0 +1,30 @@
#include <stdio.h>
void shell_sort(int arr[], int n) {
for (int g = n / 2; g > 0; g /= 2) {
for (int i = g; i < n; i++) {
int x = arr[i], j;
for (j = i; j >= g && arr[j - g] > x; j -= g)
arr[j] = arr[j - g];
arr[j] = x;
}
}
}
void display(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
const int SIZE = 5;
int arr[] = {93, 110, 12398, -5, 2};
printf("Shell Sort on: ");
display(arr, SIZE);
shell_sort(arr, SIZE);
display(arr, SIZE);
return 0;
}

BIN
14.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

16
4.c
View File

@@ -11,12 +11,10 @@ void insert(node **last, int collegeid) {
node *dst = malloc(sizeof(node)); node *dst = malloc(sizeof(node));
dst->collegeid = collegeid; dst->collegeid = collegeid;
if (*last == NULL) if (*last == NULL) {
dst->next = dst;
*last = dst; *last = dst;
} else {
if ((*last)->next == NULL)
(*last)->next = dst, dst->next = *last;
else {
dst->next = (*last)->next; dst->next = (*last)->next;
(*last)->next = dst; (*last)->next = dst;
} }
@@ -25,18 +23,14 @@ void insert(node **last, int collegeid) {
void delete (node **last) { void delete (node **last) {
node *prev = NULL; node *prev = NULL;
if ((*last)->next == NULL) if ((*last)->next == (*last))
goto cleanup; goto cleanup;
prev = *last; prev = *last;
while (prev->next != *last) while (prev->next != *last)
prev = prev->next; prev = prev->next;
if (prev->next->next == prev) prev->next = (*last)->next;
prev->next = NULL;
else
prev->next = (*last)->next;
cleanup: cleanup:
free(*last); free(*last);

BIN
8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

107
9.c Normal file
View File

@@ -0,0 +1,107 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Car car;
typedef struct Node node;
struct Node {
char type[30];
char company[50];
uint yom;
struct Node *left;
struct Node *right;
};
node *new (char *type, char *company, uint yom) {
node *n = malloc(sizeof(node));
n->left = n->right = NULL;
strcpy(n->type, type);
strcpy(n->company, company);
n->yom = yom;
return n;
}
void insert(node **root, char *type, char *company, uint yom) {
if (!(*root)) {
*root = new (type, company, yom);
return;
}
if (yom < (*root)->yom)
insert(&((*root)->left), type, company, yom);
else
insert(&((*root)->right), type, company, yom);
}
void free_tree(node *n) {
if (!n)
return;
free_tree(n->left);
free_tree(n->right);
free(n);
}
void delete (node **root, uint yom) {
if ((*root)->yom == yom) {
free_tree(*root);
*root = NULL;
return;
}
if (yom < (*root)->yom)
return delete (&((*root)->left), yom);
return delete (&((*root)->right), yom);
}
void preorder(node *n) {
if (!n)
return;
printf("Type: %s\nCompany: %s\nYear: %d\n", n->type, n->company, n->yom);
preorder(n->left);
preorder(n->right);
}
void postorder(node *n) {
if (!n)
return;
postorder(n->left);
postorder(n->right);
printf("Type: %s\nCompany: %s\nYear: %d\n", n->type, n->company, n->yom);
}
void inorder(node *n) {
if (!n)
return;
inorder(n->left);
printf("Type: %s\nCompany: %s\nYear: %d\n", n->type, n->company, n->yom);
inorder(n->right);
}
int main() {
node *root = NULL;
insert(&root, "A", "Amaang", 4004);
insert(&root, "Sahi", "Vivek", 2003);
insert(&root, "How", "Alphonso", 9999);
insert(&root, "real", "hmm", 4);
delete (&root, 9999);
printf("---------\n");
printf("Preorder:\n");
printf("---------\n");
preorder(root);
printf("---------\n");
printf("Postorder:\n");
printf("---------\n");
postorder(root);
printf("---------\n");
printf("Inorder:\n");
printf("---------\n");
inorder(root);
free_tree(root);
}

BIN
9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB