From c5c2c3ecf4868a4551b176e42ff78d82665bca2b Mon Sep 17 00:00:00 2001 From: ergz Date: Sat, 1 Jul 2023 21:07:33 -0700 Subject: [PATCH] more algos --- c/a.out | Bin 16976 -> 16856 bytes c/arraylist.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ c/mazer-solver.c | 0 c/qs.c | 49 +++++++++++++++++++++++++++++++++++++ c/stack.c | 42 +++++++++++++------------------- 5 files changed, 127 insertions(+), 25 deletions(-) create mode 100644 c/arraylist.c create mode 100644 c/mazer-solver.c create mode 100644 c/qs.c diff --git a/c/a.out b/c/a.out index 301e664dd0e64acd4b04a1bb9fdeae882432c449..11139801b2b6cfe7331f13634dd617ca92efca1c 100755 GIT binary patch delta 2154 zcmah~drVtZ7(eH>1l8Cz*_?G|&dom*4Kc-F>>Rq`Fd>F{{m#AZ9&Cvxx#xG! z_xoP=JKsI$;(k2UkB8Rcyy2qb>Y)@QjiKQm&FO7QdC{gM+1Qjmj^*Vzjy#S{*r+qe z>*d%ei%}zamHAeedt9IT+Nmd->lPj~?G2r1IUH*+k{(4X_7I1XB^qVkNVSs8DsaprOh30kV^ngn ztU*7dCdXXvntG1?pXXjFB>h-$y(ln4JI-9sdf*>7FZ{~)&l&jJ$}JzX?yXo0hZ6B+_VZ7Q7$IKEbad&7l(}mn4@h2yF|Mt0!61bjov zBN@q*Ga=yG zO^dDkx(q4FpelYGh;wJM3Y-Yh`Z>1MhwZ0|5OQpF3Ew;1F5z!SqeED7^Wow#5O1k3 zx&ie~i{q=2KXutFNMA~9#Gq_$^ZPseMzr04R<|0^4un@JDjOhG=y;R?ZMYw>9&ils zDZnoP2Lbm0cF&9AG~l7XL~#jFbzKzmp(f`6s{pGOz#fwehC+QFn3zEYIa8(I>>+ry z!qZ38#)t7Ju^8Fjp{t@;LUj@&A8-(!VX)QHQgiodUSc+8rYJNxBQ*_Co~CsW(%1)h zlV{o~E%E0;JOljcJNSol2u^<;Tw$a`V@C4Rx;6M+f~~CnV@b%ONjr^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Xg#vt zlq7hqpiZDX&|^UDK)G9zv=!(&p!@D>Wuw^iF$#H&pUI|#4ntH&SPV7UsJcGg_?o6JEy0(Xm;~Yy zn5SSmz+M3~X5Tq)9|!d&u)i2@*T?`gej4USU}v?d_#N}{sTj4P&%wdH0ALIApa3gj zty<_!alC!&JU|oJQ^7t6G*+PYbxb|hg31f}{bX}No&FQxPK1vaOXjDrKng7RxFqbbL@+KP1=bv`0fExWVGom1po1H%hZvUC zj9ZAQl*1En&k5I-9>&Q-0EMC3ZxoUrA6bS!B)E*r8)ZrkjjRSHZd@Q%uwG%BETn!D zRfp@uRmNE_!5TCOj%)nW1Q#9XGeKFBBl|1gU6f5Wlygg#DOPQkY-PO!b_M96*enei zBQGCdiGKwPgm}Ffc|jU?F~(hkJYu7noG^)$nSRVAa`|3m%H0t z0T=3W?`dW3%HPU8&OSe>w0VlF68g+@sP8&x}0{m*Bx*x zurxuJuiYJ2(Dh;`nvT-xn>!?iLRV7P{FnOtZ>3=f@8c+ZL diff --git a/c/arraylist.c b/c/arraylist.c new file mode 100644 index 0000000..a11dd56 --- /dev/null +++ b/c/arraylist.c @@ -0,0 +1,61 @@ +#include +#include + +typedef struct ArrayList { + int capacity; + int index; + int data[]; +} ArrayList; + +ArrayList* new_arraylist(int cap) { + ArrayList* arr = malloc(sizeof(ArrayList) + cap * sizeof(int)); + arr->capacity = cap; + arr->index = 0; + for (int i = 0; i < cap; i++) { + arr->data[i] = 0; + } + + return arr; +} + +void push_to_array(ArrayList* s, int v) { + if (s->index == s->capacity) { + printf("you attempted to insert %d, but array is at capacity cannot add mode values\n", v); + } else { + s->data[s->index] = v; + s->index++; + } +} + +void pop_from_array(ArrayList* s) { + if (s->index == 0) { + printf("there is nothing to remove!\n"); + } else { + s->index--; + } +} + +void grow_array_list(ArrayList* s, int amount) { +} + +void print_array_list(ArrayList* arr) { + printf("["); + for (int i = 0; i < arr->index; i++) { + printf(" %d ", arr->data[i]); + } + printf("]\n"); +} + +int main() { + ArrayList* a = new_arraylist(5); + push_to_array(a, 10); + push_to_array(a, 11); + push_to_array(a, 12); + push_to_array(a, 12); + push_to_array(a, 12); + push_to_array(a, 12); + push_to_array(a, 12); + pop_from_array(a); + push_to_array(a, 155); + print_array_list(a); +} \ No newline at end of file diff --git a/c/mazer-solver.c b/c/mazer-solver.c new file mode 100644 index 0000000..e69de29 diff --git a/c/qs.c b/c/qs.c new file mode 100644 index 0000000..3ed2c64 --- /dev/null +++ b/c/qs.c @@ -0,0 +1,49 @@ +#include + +// partition takes an array and a low and hi values and partitions the array +// by rearrating and placing everything smaller then the pivot point to the left +// of it and everything greater than it on the right +int partition(int arr[], int lo, int hi) { + int pivot_value = arr[hi]; + int idx = -1; // this will keep track of current location of values less then the pivot + + for (int i = 0; i < hi; i++) { + if (arr[i] <= pivot_value) { + idx++; + + // if less than the pivot then swap the ith value with the current idx + int tmp = arr[i]; + arr[i] = arr[idx]; + arr[idx] = tmp; + } + } + + // after the iteration we still need to reposition our pivot_value to the last + // position that idx was after the for loop ends, this will mean that everything + // to left of idx will be less than the pivot point + idx++; + arr[hi] = arr[idx]; + arr[idx] = pivot_value; +} + +void quick_sort(int arr[], int lo, int hi) { + if (lo >= hi) { + printf("error!"); + } + + int current_pivot = partition(arr, lo, hi); + quick_sort(arr, lo, current_pivot - 1); + quick_sort(arr, current_pivot + 1, hi); +} + +int main() { + int array_len = 5; + int arr[5] = {5, 4, 3, 2, 1}; + quick_sort(arr, 0, array_len); + + printf("[ "); + for (int i = 0; i < array_len; i++) { + printf("%d ", arr[i]); + } + printf("]\n"); +} \ No newline at end of file diff --git a/c/stack.c b/c/stack.c index 2ec2106..35619e3 100644 --- a/c/stack.c +++ b/c/stack.c @@ -39,11 +39,12 @@ Stack* new_stack() { return (s); } -bool pop(Stack* s) { +int pop(Stack* s) { if (s->length == 0) { return false; } else { Node* n = s->head; + int val = n->value; if (s->length == 1) { s->head = NULL; } else { @@ -51,7 +52,7 @@ bool pop(Stack* s) { } free(n); s->length--; - return true; + return val; } } @@ -103,32 +104,23 @@ int main() { printf("the value at the top of the stack is: %d\n", peek(s)); printf("the length of the stack is: %d\n", s->length); - // bool res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); - // res = pop(s); - // printf("the value at the top of the stack is: %d\n", peek(s)); - // printf("the length of the stack is: %d\n", s->length); - - delete_stack(s); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); + printf("the value at the top of the stack is: %d\n", pop(s)); + printf("the length of the stack is: %d\n", s->length); return (0); } \ No newline at end of file