more algos
This commit is contained in:
parent
722aecd6dc
commit
c5c2c3ecf4
|
@ -0,0 +1,61 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
}
|
42
c/stack.c
42
c/stack.c
|
@ -39,11 +39,12 @@ Stack* new_stack() {
|
||||||
return (s);
|
return (s);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pop(Stack* s) {
|
int pop(Stack* s) {
|
||||||
if (s->length == 0) {
|
if (s->length == 0) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
Node* n = s->head;
|
Node* n = s->head;
|
||||||
|
int val = n->value;
|
||||||
if (s->length == 1) {
|
if (s->length == 1) {
|
||||||
s->head = NULL;
|
s->head = NULL;
|
||||||
} else {
|
} else {
|
||||||
|
@ -51,7 +52,7 @@ bool pop(Stack* s) {
|
||||||
}
|
}
|
||||||
free(n);
|
free(n);
|
||||||
s->length--;
|
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 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 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", 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 length of the stack is: %d\n", s->length);
|
|
||||||
|
|
||||||
// res = pop(s);
|
printf("the value at the top of the stack is: %d\n", 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 length of the stack is: %d\n", s->length);
|
|
||||||
|
|
||||||
// res = pop(s);
|
printf("the value at the top of the stack is: %d\n", 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 length of the stack is: %d\n", s->length);
|
|
||||||
|
|
||||||
// res = pop(s);
|
printf("the value at the top of the stack is: %d\n", 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 length of the stack is: %d\n", s->length);
|
printf("the value at the top of the stack is: %d\n", pop(s));
|
||||||
// res = 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", peek(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 length of the stack is: %d\n", s->length);
|
||||||
// res = pop(s);
|
printf("the value at the top of the stack is: %d\n", 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 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);
|
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
Loading…
Reference in New Issue