implements selection sort
This commit is contained in:
parent
5564d94684
commit
09e18f9ac9
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -32,7 +34,8 @@ void do_append(i32_ArrayList *s, int v) {
|
|||
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");
|
||||
|
@ -66,8 +69,9 @@ i32_ArrayList *new_arraylist_from_array(int cap, int *arr) {
|
|||
}
|
||||
|
||||
// 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) {
|
||||
do_append(arr, value);
|
||||
|
@ -75,8 +79,11 @@ void array_insert_at(i32_ArrayList *arr, int at_index, int32_t 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--) {
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -17,6 +17,7 @@ typedef struct FixedOrderedArray {
|
|||
int data[];
|
||||
} FixedOrderedArray;
|
||||
|
||||
|
||||
FixedOrderedArray *new_fixed_ordered_array(int capacity) {
|
||||
FixedOrderedArray *arr = malloc(sizeof(FixedOrderedArray) + (sizeof(int) * capacity));
|
||||
arr->capacity = capacity;
|
||||
|
@ -85,6 +86,7 @@ void print_array_list(FixedOrderedArray *arr) {
|
|||
for (int i = 0; i < arr->index; i++) {
|
||||
printf(" %d ", arr->data[i]);
|
||||
}
|
||||
|
||||
printf("]\t<capacity: %d; index: %d>\n", arr->capacity, arr->index);
|
||||
}
|
||||
|
||||
|
|
|
@ -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