From 8fc59db9baff94a026fa6e38cfd842f3a2556f37 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Wed, 4 Jan 2023 21:09:59 +0700 Subject: [PATCH] First commit with some working examples already --- GrokkingAlgorithms.org | 5 +++ TheAlgorithmDesignManual.org | 5 +++ binary_search.org | 15 ++++++++ selection_sort.org | 51 ++++++++++++++++++++++++++++ stack.org | 66 ++++++++++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 GrokkingAlgorithms.org create mode 100644 TheAlgorithmDesignManual.org create mode 100644 binary_search.org create mode 100644 selection_sort.org create mode 100644 stack.org diff --git a/GrokkingAlgorithms.org b/GrokkingAlgorithms.org new file mode 100644 index 0000000..826f457 --- /dev/null +++ b/GrokkingAlgorithms.org @@ -0,0 +1,5 @@ +#+TITLE: Notes & Exercises: Grokking Algorithms +#+AUTHOR: Joseph Ferano + +** Notes +** Exercises diff --git a/TheAlgorithmDesignManual.org b/TheAlgorithmDesignManual.org new file mode 100644 index 0000000..cd9e0e0 --- /dev/null +++ b/TheAlgorithmDesignManual.org @@ -0,0 +1,5 @@ +#+TITLE: Notes & Exercises: The Algorith Design Manual +#+AUTHOR: Joseph Ferano + +** Notes +** Exercises diff --git a/binary_search.org b/binary_search.org new file mode 100644 index 0000000..2996f1f --- /dev/null +++ b/binary_search.org @@ -0,0 +1,15 @@ + +** OCaml + +#+begin_src ocaml +let binary_search items target = + let rec f low high = + match (high - low) / 2 + low with + | mid when target = items.(mid) -> Some items.(mid) + | mid when target < items.(mid) -> f low mid + | mid when target > items.(mid) -> f mid high + | _ -> None + in f 0 (Array.length items);; + +binary_search [|1;2;3;4;5|] 3;; +#+end_src diff --git a/selection_sort.org b/selection_sort.org new file mode 100644 index 0000000..05d76fa --- /dev/null +++ b/selection_sort.org @@ -0,0 +1,51 @@ +#+OPTIONS: ^:{} + +** Selection Sort + +Runtime O(n^{2}) + +*** Python + +#+begin_src python +def selection_sort(arr): + sorted_list = [] + for i in range(len(arr)): + max = arr[0] + for count, value in enumerate(arr): + if value > max: + max = value + sorted_list.append(max) + arr.remove(max) + return sorted_list + +selection_sort([2,1,5,3,4]) +#+end_src + +*** OCaml + +Really reinventing the wheel on this one... + +#+begin_src ocaml +let max_element = function + | [] -> invalid_arg "empty list" + | x::xs -> + let rec f acc = function + | [] -> acc + | x::xs -> f (if x > acc then x else acc) xs + in f x xs + +let remove item list = + let rec f acc item = function + | [] -> List.rev acc + | x::xs -> if item = x then (List.rev acc) @ xs else f (x::acc) item xs + in f [] item list + +let selection_sort list = + let rec f acc = function + | [] -> acc + | xs -> + let m = max xs + in f (m::acc) (remove m xs) + in f [] list +#+end_src + diff --git a/stack.org b/stack.org new file mode 100644 index 0000000..d39b1f4 --- /dev/null +++ b/stack.org @@ -0,0 +1,66 @@ +** C + +#+begin_src C :includes stdlib.h stdio.h stdbool.h +typedef struct Node { + struct Node* next; + /* void *data; */ + int data; +} Node; + +typedef struct Stack { + Node* top; + int length; +} Stack; + +/* void stack_create(void* data) { */ +Stack *stack_create(int data) { + Stack *stack = malloc(sizeof(Stack)); + Node *node = malloc(sizeof(Node)); + node->data = data; + node->next = NULL; + stack->top = node; + stack->length = 1; + return stack; +} + +int stack_pop(Stack *stack) { + Node *top = stack->top; + Node *next = top->next; + if (stack->length > 0 && next != NULL) { + stack->top = next; + stack->length--; + int value = top->data; + free(top); + return value; + } else { + // A better API design would be to return a bool and the int as a pointer + // Although once we switch away from int and use void pointers, might not be needed + return -1; + } +} + +/* void stack_push(Stack *stack, void *data) { */ +void stack_push(Stack *stack, int data) { + Node *node = malloc(sizeof(Node)); + Node *top = stack->top; + node->data = data; + node->next = top; + stack->top = node; + stack->length++; +} + +/* void* stack_peak(Stack *stack) { */ +int stack_peak(Stack *stack) { + return stack->top->data; +} + +void stack_print(Stack *stack) { + Node *current = stack->top; + int i = 0; + while (current != NULL) { + printf("Stack at %d: %d\n", ++i, current->data); + current = current->next; + } + printf("------------------\n"); +} +#+end_src