Compare commits
3 Commits
84653df183
...
09e18f9ac9
Author | SHA1 | Date |
---|---|---|
|
09e18f9ac9 | |
|
5564d94684 | |
|
68ced98e24 |
|
@ -12,7 +12,9 @@ i32_ArrayList *new_arraylist(int cap) {
|
|||
i32_ArrayList *arr = malloc(sizeof(i32_ArrayList) + cap * sizeof(int));
|
||||
|
||||
if (arr == NULL) {
|
||||
printf("ERROR: there was an error attemping to allocate memory for i32_ArrayList\n");
|
||||
printf(
|
||||
"ERROR: there was an error attemping to allocate memory for "
|
||||
"i32_ArrayList\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -23,20 +25,17 @@ i32_ArrayList *new_arraylist(int cap) {
|
|||
}
|
||||
|
||||
// add to end of the array
|
||||
void array_append(i32_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 {
|
||||
void do_append(i32_ArrayList *s, int v) {
|
||||
s->data[s->index] = v;
|
||||
s->index++;
|
||||
}
|
||||
}
|
||||
|
||||
// takes the address of a pointer
|
||||
i32_ArrayList *resize_arraylist(i32_ArrayList *arr) {
|
||||
int new_cap = arr->capacity * 2;
|
||||
|
||||
i32_ArrayList *new_arr = (i32_ArrayList *)realloc(arr, (sizeof(int) * new_cap) + sizeof(i32_ArrayList));
|
||||
i32_ArrayList *new_arr = (i32_ArrayList *)realloc(
|
||||
arr, (sizeof(int) * new_cap) + sizeof(i32_ArrayList));
|
||||
|
||||
if (new_arr == NULL) {
|
||||
fprintf(stderr, "ERROR: unable to resize array\n");
|
||||
|
@ -47,15 +46,15 @@ i32_ArrayList *resize_arraylist(i32_ArrayList *arr) {
|
|||
return (new_arr);
|
||||
}
|
||||
|
||||
void array_append2(i32_ArrayList **arr_ptr, int v) {
|
||||
void array_append(i32_ArrayList **arr_ptr, int v) {
|
||||
i32_ArrayList *arr = *arr_ptr;
|
||||
if (arr->index == arr->capacity) {
|
||||
i32_ArrayList *new_arr = resize_arraylist(arr);
|
||||
*arr_ptr = new_arr;
|
||||
array_append(*arr_ptr, v);
|
||||
do_append(*arr_ptr, v);
|
||||
|
||||
} else {
|
||||
array_append(arr, v);
|
||||
do_append(arr, v);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,23 +62,28 @@ void array_append2(i32_ArrayList **arr_ptr, int v) {
|
|||
i32_ArrayList *new_arraylist_from_array(int cap, int *arr) {
|
||||
i32_ArrayList *out = new_arraylist(cap);
|
||||
for (int i = 0; i < cap; i++) {
|
||||
array_append(out, arr[i]);
|
||||
do_append(out, arr[i]);
|
||||
}
|
||||
|
||||
return (out);
|
||||
}
|
||||
|
||||
// insert value at index
|
||||
// the strategy here is to start from the last element in the array and shift it to the right
|
||||
// gotta be careful and check that the index + 1 <= capacity otherwise we are in trouble
|
||||
// the strategy here is to start from the last element in the array and shift it
|
||||
// to the right gotta be careful and check that the index + 1 <= capacity
|
||||
// otherwise we are in trouble
|
||||
void array_insert_at(i32_ArrayList *arr, int at_index, int32_t value) {
|
||||
if (at_index == arr->index) {
|
||||
array_append(arr, value);
|
||||
do_append(arr, value);
|
||||
}
|
||||
|
||||
// TODO: eh this should be much better
|
||||
if (at_index + 1 > arr->capacity) {
|
||||
printf("ERROR: this insert is not possible since the shift required would be over the capacity of the array\n");
|
||||
printf("You requested insert at %d but array capacity is set to %d\n", at_index, arr->capacity);
|
||||
printf(
|
||||
"ERROR: this insert is not possible since the shift required "
|
||||
"would be over the capacity of the array\n");
|
||||
printf("You requested insert at %d but array capacity is set to %d\n",
|
||||
at_index, arr->capacity);
|
||||
}
|
||||
|
||||
for (int i = arr->index; i >= at_index; i--) {
|
||||
|
@ -119,24 +123,24 @@ int main() {
|
|||
print_array_list(b);
|
||||
|
||||
// these should all work just fine
|
||||
array_append(a, 10);
|
||||
array_append(&a, 10);
|
||||
print_array_list(a);
|
||||
array_append(a, 11);
|
||||
array_append(&a, 11);
|
||||
print_array_list(a);
|
||||
array_append(a, 12);
|
||||
array_append(&a, 12);
|
||||
print_array_list(a);
|
||||
array_append(a, 13);
|
||||
array_append(&a, 13);
|
||||
print_array_list(a);
|
||||
array_append(a, 14);
|
||||
array_append(&a, 14);
|
||||
print_array_list(a);
|
||||
|
||||
// this one will error
|
||||
array_append(a, 100);
|
||||
array_append(&a, 100);
|
||||
|
||||
// so we remove one and then add
|
||||
pop_from_array(a);
|
||||
print_array_list(a);
|
||||
array_append(a, 100);
|
||||
array_append(&a, 100);
|
||||
print_array_list(a);
|
||||
|
||||
// now we test inserting different index
|
||||
|
@ -154,13 +158,19 @@ int main() {
|
|||
// this will shift the current 3 to 4, but this causes the 100 to be removed
|
||||
array_insert_at(a, 3, 123);
|
||||
print_array_list(a);
|
||||
array_append2(&a, 5000);
|
||||
array_append(&a, 5000);
|
||||
print_array_list(a);
|
||||
|
||||
printf("--------------------------------\n");
|
||||
print_array_list(b);
|
||||
array_append2(&b, 100);
|
||||
array_append(&b, 100);
|
||||
print_array_list(b);
|
||||
|
||||
int vals[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
i32_ArrayList *c = new_arraylist_from_array(10, vals);
|
||||
|
||||
array_append(&c, 10);
|
||||
print_array_list(c);
|
||||
// array_append(a, 14);
|
||||
// print_array_list(a);
|
||||
// pop_from_array(a);
|
||||
|
|
|
@ -32,7 +32,7 @@ bool binary_search(int *arr, int len, int val) {
|
|||
}
|
||||
|
||||
int main() {
|
||||
int arr[8] = {1, 3, 4, 5, 6, 7, 8, 11};
|
||||
int arr[8] = {1, 3, 4, 5, 6, 7, 8, 10};
|
||||
bool result = binary_search(arr, 8, 11);
|
||||
printf("the result is %s\n", result ? "true" : "false");
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
in bubble sort we iterate through the array each time we encounter adjacent elements
|
||||
out of order, we swap them. We continue this iteration until we have reached
|
||||
*/
|
||||
|
||||
void swap(int *a, int *b) {
|
||||
int temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
void bubble_sort(int arr[], int len) {
|
||||
int swap_count = 1;
|
||||
int temp;
|
||||
int end_index = len - 1;
|
||||
|
||||
while (swap_count > 0) {
|
||||
swap_count = 0;
|
||||
for (int i = 0; i < end_index; i++) {
|
||||
if (arr[i] > arr[i + 1]) {
|
||||
temp = arr[i];
|
||||
arr[i] = arr[i + 1];
|
||||
arr[i + 1] = temp;
|
||||
swap_count++;
|
||||
}
|
||||
}
|
||||
end_index--;
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(int arr[], int len) {
|
||||
printf("[ ");
|
||||
for (int i = 0; i < len; i++) {
|
||||
printf(" %d", arr[i]);
|
||||
}
|
||||
printf(" ]\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
int input[5] = {5, 4, 3, 2, 1};
|
||||
bubble_sort(input, 5);
|
||||
print_array(input, 5);
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
an ordered array is just a regular array except all elements must be in order,
|
||||
so there a few things to note:
|
||||
|
||||
1. inserting must respect order
|
||||
2. removing must respect order
|
||||
3. serching can be made faster with binary search
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct FixedOrderedArray {
|
||||
int capacity;
|
||||
int index; // index next to insert at, arr[index] should be empty
|
||||
int data[];
|
||||
} FixedOrderedArray;
|
||||
|
||||
|
||||
FixedOrderedArray *new_fixed_ordered_array(int capacity) {
|
||||
FixedOrderedArray *arr = malloc(sizeof(FixedOrderedArray) + (sizeof(int) * capacity));
|
||||
arr->capacity = capacity;
|
||||
arr->index = 0;
|
||||
|
||||
return (arr);
|
||||
}
|
||||
|
||||
void fixed_ordered_array_insert(FixedOrderedArray *arr, int val) {
|
||||
if (arr->capacity == arr->index) {
|
||||
fprintf(stderr, "ERROR: array is at capacity, cannot append value\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (arr->index == 0) {
|
||||
arr->data[0] = val;
|
||||
arr->index++;
|
||||
} else if (val >= arr->data[arr->index - 1]) {
|
||||
arr->data[arr->index] = val;
|
||||
arr->index++;
|
||||
} else if (val <= arr->data[0]) { // its smallest to add to start
|
||||
for (int i = arr->index; i > 0; i--) { // shift everything to the right
|
||||
arr->data[i] = arr->data[i - 1];
|
||||
}
|
||||
arr->data[0] = val;
|
||||
arr->index++;
|
||||
} else { // look for where it belongs
|
||||
for (int i = 0; i < arr->index; i++) {
|
||||
if (arr->data[i] > val) { // we must shift everything to the right up to this index
|
||||
for (int j = arr->index; j > i; j--) {
|
||||
arr->data[j] = arr->data[j - 1];
|
||||
}
|
||||
arr->data[i] = val;
|
||||
arr->index++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool search_fixed_ordered_array(FixedOrderedArray *arr, int val) {
|
||||
if (arr->index == 0) {
|
||||
return (false);
|
||||
}
|
||||
|
||||
if (val < arr->data[0]) {
|
||||
return (false);
|
||||
}
|
||||
|
||||
if (val > arr->data[arr->index - 1]) {
|
||||
return (false);
|
||||
}
|
||||
|
||||
for (int i = 0; i < arr->index; i++) {
|
||||
if (val == arr->data[i]) {
|
||||
return (true);
|
||||
} else if (arr->data[i] > val) {
|
||||
return (false);
|
||||
}
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
void print_array_list(FixedOrderedArray *arr) {
|
||||
printf("[");
|
||||
for (int i = 0; i < arr->index; i++) {
|
||||
printf(" %d ", arr->data[i]);
|
||||
}
|
||||
|
||||
printf("]\t<capacity: %d; index: %d>\n", arr->capacity, arr->index);
|
||||
}
|
||||
|
||||
int main() {
|
||||
FixedOrderedArray *arr = new_fixed_ordered_array(5);
|
||||
|
||||
fixed_ordered_array_insert(arr, 10);
|
||||
print_array_list(arr);
|
||||
fixed_ordered_array_insert(arr, 9);
|
||||
print_array_list(arr);
|
||||
fixed_ordered_array_insert(arr, 13);
|
||||
print_array_list(arr);
|
||||
fixed_ordered_array_insert(arr, 11);
|
||||
print_array_list(arr);
|
||||
fixed_ordered_array_insert(arr, 9);
|
||||
print_array_list(arr);
|
||||
|
||||
bool result = search_fixed_ordered_array(arr, 11);
|
||||
printf("the result is %d\n", result);
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void swap(int *a, int *b) {
|
||||
int temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
void selection_sort(int arr[], int len) {
|
||||
int curr_low;
|
||||
int low_index = 0;
|
||||
int pass_index = 0;
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
low_index = i;
|
||||
for (int j = i + 1; j < len; j++) {
|
||||
if (arr[i] > arr[j]) { // if we encounter a new low
|
||||
low_index = j;
|
||||
}
|
||||
}
|
||||
swap(&arr[i], &arr[low_index]);
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(int arr[], int len) {
|
||||
printf("[ ");
|
||||
for (int i = 0; i < len; i++) {
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
int arr[6] = {5, 3, 4, 2, 1, 7};
|
||||
selection_sort(arr, 6);
|
||||
print_array(arr, 6);
|
||||
}
|
Loading…
Reference in New Issue