This commit is contained in:
MarcUs7i 2025-05-14 09:01:03 +02:00
parent a04a66c509
commit add23e123e
23 changed files with 1041 additions and 107 deletions

View file

@ -1,9 +1,9 @@
/*----------------------------------------------------------
* HTBLA-Leonding / Class: <your class>
* HTBLA-Leonding / Class: 2IHIF
* ---------------------------------------------------------
* Exercise Number: S07
* Title: Array backed List implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Implementation of an array backed list.
@ -50,6 +50,9 @@
/** The implementation of list data: payload-buffer, capacity, size */
struct IntListData {
int* buffer;
int capacity;
int size;
};
/* ===================================================================== */
@ -60,6 +63,360 @@ struct IntListData {
* Hint: memcpy may be used to copy all bytes(!) from the existing to the new buffer
*/
static void increase_buffer(IntList list, unsigned int additional_capacity) {
if (list->buffer == 0) {
list->buffer = alloc_mem(sizeof(int) * additional_capacity);
if (list->buffer != 0) {
list->capacity = additional_capacity;
}
} else {
int* new_buffer = alloc_mem(sizeof(int) * (list->capacity + additional_capacity));
if (new_buffer != 0) {
for (int i = 0; i < list->size; i++) {
new_buffer[i] = list->buffer[i];
}
free_mem(list->buffer);
list->buffer = new_buffer;
list->capacity += additional_capacity;
}
}
}
/* ===================================================================== */
/**
* Obtains ('creates') and provides a 'new' list instance.
* Any list obtained via this function MUST be released using
* function `release_list()`.
*
* Note: This function does not make any assumptions
* about how list components, esp. nodes, are allocated.
*
* @return The list instance or 0, if no list could by instantiated.
*/
IntList list_obtain() {
IntList list = alloc_mem(sizeof(struct IntListData));
if (list != 0) {
list->buffer = 0;
list->capacity = 0;
list->size = 0;
}
return list;
}
/**
* Releases a list that was obtained earlier via function `obtain_list`.
* Released lists MUST NOT be used anymore.
*
* Note: The implementation of this function does not make any assumptions
* about the allocation method of list elements, but MUST match the implementation
* of function `obtain_list` as its inverse function.
*
* @param p_list The pointer to the list to release. The value of the pointer
* is set to 0, if the list was successfully released, otherwise it is left untouched.
*/
void list_release(IntList* p_list) {
if (p_list != 0 && *p_list != 0) {
IntList list = *p_list;
if (list->buffer != 0) {
free_mem(list->buffer);
}
free_mem(list);
*p_list = 0;
}
}
/**
* Determines whether or not the given list is valid.
*
* @param list The list to evaluate.
* @return `True` if the list is valid, false otherwise.
*/
bool list_is_valid(IntList list) {
return list != 0;
}
/**
* Determines whether or not the list contains at least one item.
*
* @param list The list to evaluate.
* @return `False` if the list contains one or more items, `true` otherwise.
*/
bool list_is_empty(IntList list) {
return !list_is_valid(list) || list->size == 0;
}
/**
* Provides the number of values stored in the list.
*
* @param list The list to evaluate.
* @return The number of values the list contains.
*/
int list_get_size(IntList list) {
return list_is_valid(list) ? list->size : 0;
}
/**
* Determines whether or not the list given list contains the queried value
* at least once.
*
* @param list The list to query.
* @param value The value.
* @return `True` if the list contains at least one instance of the value,
* `false ` otherwise.
*/
bool list_contains(IntList list, int value) {
if (list_is_valid(list)) {
for (int i = 0; i < list->size; i++) {
if (list->buffer[i] == value) {
return true;
}
}
}
return false;
}
/**
* Provides the value stored in the list at the given position.
*
* @param list The list from which the value shall be retrieved.
* @param index The zero-based position index of the value to retrieve.
* @return The value stored at the given position or 0, if the position
* is not available.
*/
int list_get_at(IntList list, unsigned int index) {
if (list_is_valid(list) && index < list->size) {
return list->buffer[index];
}
return 0;
}
/**
* Inserts the given value at the end of the given list.
*
* @param list The list to which the value shall be appended.
* @param value The value to append to the list.
*/
void list_insert(IntList list, int value) {
if (list_is_valid(list)) {
if (list->size >= list->capacity) {
increase_buffer(list, CAPACITY_INCREMENT);
}
list->buffer[list->size] = value;
list->size++;
}
}
/**
* Inserts the given value at the indexed position in a way the
* the inserted value is on that position. The index is
* - similar to arrays - zero-based. If the the list is shorter
* than the indexed position, the value is inserted at the end
* of the list.
*
* @param list The list into which the value shall be appended.
* @param index The position index of the value to insert.
* @param value The value to insert.
*/
void list_insert_at(IntList list, unsigned int index, int value) {
if (list_is_valid(list)) {
if (index >= list->size) {
list_insert(list, value);
} else {
if (list->size >= list->capacity) {
increase_buffer(list, CAPACITY_INCREMENT);
}
for (int i = list->size; i > index; i--) {
list->buffer[i] = list->buffer[i - 1];
}
list->buffer[index] = value;
list->size++;
}
}
}
/**
* Appends the `list_to_append` at the end of the given `list`.
* The appended list is empty afterwards, because all nodes of that list
* have been transferred to `list`.
*
* @param list The list that receives the other list.
* @param list_to_append The list that is appended to `list`.
*/
/* This function is not required in this assignment. */
/* void list_append(IntList list, IntList list_to_append); */
/**
* Removes the first occurrance of `value` from the given list.
* If the list does not contain that value, the list shall not
* be modified.
*
* @param list The list from which the given value shall be removed.
* @param value The value to remove from the list.
*/
void list_remove(IntList list, int value) {
if (list_is_valid(list)) {
for (int i = 0; i < list->size; i++) {
if (list->buffer[i] == value) {
for (int j = i; j < list->size - 1; j++) {
list->buffer[j] = list->buffer[j + 1];
}
list->size--;
return;
}
}
}
}
/**
* Removes all occurrances of `value` from the list.
* If the list does not contain that value, the list shall not
* be modified.
*
* @param list The list from which all occurrances of `value` shall be removed.
* @param value The `value` to remove throughout the list.
*/
void list_remove_all(IntList list, int value) {
if (list_is_valid(list)) {
for (int i = 0; i < list->size; i++) {
if (list->buffer[i] == value) {
for (int j = i; j < list->size - 1; j++) {
list->buffer[j] = list->buffer[j + 1];
}
list->size--;
i--;
}
}
}
}
/**
* Removes the value at the indexed position from the given list
* and provides that value. If the list does not have a value
* at that position, the list remains unmodified.
*
* @param list The list from which the value at the given index shall be returned.
* @param index The zero-based index of the value to return.
* @return The removed value or 0 in case of errors.
*/
int list_remove_at(IntList list, unsigned int index) {
if (list_is_valid(list) && index < list->size) {
int value = list->buffer[index];
for (int i = index; i < list->size - 1; i++) {
list->buffer[i] = list->buffer[i + 1];
}
list->size--;
return value;
}
return 0;
}
/**
* Swaps the values at the given indexes, so that value at fst_idx becomes
* the value at snd_idx and vice versa. The invocation is ignored, if the
* list is invalid or at least one of the given indexes is out of range.
*
* @param list The list to manipulate
* @param fst_idx The index of the first item to swap.
* @param snd_idx The index of the second item to swap.
*/
void list_swap(IntList list, unsigned int fst_idx, unsigned int snd_idx) {
if (list_is_valid(list) && fst_idx < list->size && snd_idx < list->size) {
int temp = list->buffer[fst_idx];
list->buffer[fst_idx] = list->buffer[snd_idx];
list->buffer[snd_idx] = temp;
}
}
/**
* Clears the given list by removing all values from the list.
*
* @param list The list to clear.
*/
void list_clear(IntList list) {
if (list_is_valid(list)) {
list->size = 0;
}
}
/**
* Appends the `list_to_append` at the end of the given `list`.
* The appended list is empty afterwards, because all nodes of that list
* have been transferred to `list`.
*
* @param list The list that receives the other list.
* @param list_to_append The list that is appended to `list`.
*/
void list_append(IntList list, IntList list_to_append) {
if (list_is_valid(list) && list_is_valid(list_to_append)) {
for (int i = 0; i < list_to_append->size; i++) {
list_insert(list, list_to_append->buffer[i]);
}
list_to_append->size = 0;
}
}
/**
* Splits the given `list` into a `left` and `right` part at the given `split_idx`.
* The `left` list contains the items of `list` from head to `split_idx - 1` rendering
* the size of the left part equal to `split_idx`. The `right` list contains
* the items of `list` from originally `split_idx` to tail.
*
* If `split_idx` is larger than the size of `list`, all items of `list`
* are moved to `left`, leaving the `right` list empty.
*
* If a target list (`left`, `right`) is not empty, the nodes that are received from
* `list` are appended.
*
* If any of the given lists is invalid, the invocation is ignored.
*
* @param list The list to split.
* @param left The left part of the original list.
* @param right The right part of the original list.
* @param split_idx The index of the node of `list` that becomes the head of `right` list.
*/
void list_split(IntList list, IntList left, IntList right, unsigned int split_idx) {
if (list_is_valid(list) && list_is_valid(left) && list_is_valid(right)) {
if (split_idx >= list->size) {
split_idx = list->size;
}
for (int i = 0; i < split_idx; i++) {
list_insert(left, list->buffer[i]);
}
for (int i = split_idx; i < list->size; i++) {
list_insert(right, list->buffer[i]);
}
list_clear(list);
}
}
/**
* Transfers the first node (the 'head') of the given `list` as last node ('tail')
* of the target list.
*
* If any of the given lists is invalid or the source list is empty, the invocation is ignored.
*
* @param list The list from which the `head` is transferred to `target_list`.
* @param target_list The list that receives the head of `list`.
*/
void list_transfer_head(IntList list, IntList target_list) {
if (list_is_valid(list) && list_is_valid(target_list) && !list_is_empty(list)) {
int value = list->buffer[0];
list_remove_at(list, 0);
list_insert(target_list, value);
}
}

View file

@ -1,5 +1,5 @@
/*----------------------------------------------------------
* HTBLA-Leonding / Class: <your class>
* HTBLA-Leonding / Class: 2IHIF
* ---------------------------------------------------------
* Exercise Number: S07
* Title: Array backed List

View file

@ -3,7 +3,7 @@
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Title: Binary search implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implements the binary search algorithm
@ -11,3 +11,91 @@
*/
#include "binary_search.h"
/**
* Searches the given needle within the given haystack using binary search approach.
*
* Implementation hint: delegate the invocation to function `binary_search_list_limited()`.
*
* @param haystack The sorted list in which the `needle` is searched.
* @param criterion The pointer to the function that implements the sorting criterion of the list.
* That function accepts two integer parameters and returns a boolean value.
* @param needle The value to find within the haystack.
* @return The index of the `needle` within the list if it was found or the position where the needle need to
* be inserted into the sorted list if it is not included in the list. The insertion position is
* expressed as index with negative sign, so that the value can be inserted at index (-1 * result).
*/
int binary_search_list(IntList haystack, criterion_fn criterion, int needle) {
if (!list_is_valid(haystack)) {
return -1;
}
return binary_search_list_limited(haystack, list_get_size(haystack), criterion, needle);
}
/**
* Searches the given needle within the given haystack up the given length using binary search approach.
*
* @param haystack The sorted list in which the `needle` is searched.
* @param length The number of items to consider starting from the beginning of the haystack.
* If the haystack contains more items, the remaining items are omitted from the search. If it contains
* less items, the number of items are clipped to the size of the haystack.
* @param criterion The pointer to the function that implements the sorting criterion of the list.
* That function accepts two integer parameters and returns a boolean value.
* @param needle The value to find within the haystack.
* @return The index of the `needle` within the list if it was found or the position where the needle need to
* be inserted into the sorted list if it is not included in the list. The insertion position is
* expressed as index with negative sign, so that the value can be inserted at index (-1 * result).
*/
int binary_search_list_limited(IntList haystack, int length, criterion_fn criterion, int needle) {
if (!list_is_valid(haystack) || criterion == 0) {
return -1;
}
int list_size = list_get_size(haystack);
if (length > list_size) {
length = list_size;
}
if (length <= 0) {
return -1;
}
int left = 0;
int right = length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int mid_value = list_get_at(haystack, mid);
if (mid_value == needle) {
return mid;
}
if (criterion(mid_value, needle)) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -left;
}
/* ARRAY VARIANT */
/**
* Searches the given needle within the given haystack using binary search approach.
*
* @param haystack The sorted list in which the `needle` is searched.
* @param length The length of the haystack array.
* @param criterion The pointer to the function that implements the sorting criterion of the list.
* That function accepts two integer parameters and returns a boolean value.
* @param needle The value to find within the haystack.
* @return The index of the `needle` within the list if it was found or the position where the needle need to
* be inserted into the sorted list if it is not included in the list. The insertion position is
* expressed as index with negative sign, so that the value can be inserted at index (-1 * result).
*/
int binary_search_array(int haystack[], int length, criterion_fn criterion, int needle) {
return -1;
}

View file

@ -3,7 +3,7 @@
* ---------------------------------------------------------
* Exercise Number: S07
* Title: Binary search implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Implements the binary search algorithm
@ -30,7 +30,7 @@
* be inserted into the sorted list if it is not included in the list. The insertion position is
* expressed as index with negative sign, so that the value can be inserted at index (-1 * result).
*/
int binary_search_list(<params>);
int binary_search_list(IntList haystack, criterion_fn criterion, int needle);
/**
* Searches the given needle within the given haystack up the given length using binary search approach.
@ -46,7 +46,7 @@ int binary_search_list(<params>);
* be inserted into the sorted list if it is not included in the list. The insertion position is
* expressed as index with negative sign, so that the value can be inserted at index (-1 * result).
*/
<type> binary_search_list_limited(<params>);
int binary_search_list_limited(IntList haystack, int length, criterion_fn criterion, int needle);
/* ARRAY VARIANT */
@ -62,6 +62,6 @@ int binary_search_list(<params>);
* be inserted into the sorted list if it is not included in the list. The insertion position is
* expressed as index with negative sign, so that the value can be inserted at index (-1 * result).
*/
<type> binary_search_array(<params>);
int binary_search_array(int haystack[], int length, criterion_fn criterion, int needle);
#endif

View file

@ -1,14 +1,61 @@
/*-----------------------------------------------------------------------------
* HTBLA-Leonding
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S05
* Title: Bubble sort implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implements the bubble sort algorithm
* Implements the bubble sort strategy
*-----------------------------------------------------------------------------
*/
#include "bubble_sort.h"
/**
* Sorts the given list according to the bubble sort strategy.
*
* @param list The list to be sorted.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void bubble_sort_list(IntList list, criterion_fn criterion) {
if (!list_is_valid(list) || criterion == 0) {
return;
}
int size = list_get_size(list);
if (size <= 1) {
return;
}
for (int i = 0; i < size - 1; i++) {
int swapped = 0;
for (int j = 0; j < size - i - 1; j++) {
int current = list_get_at(list, j);
int next = list_get_at(list, j + 1);
if (!criterion(current, next)) {
list_swap(list, j, j + 1);
swapped = 1;
}
}
if (!swapped) {
break;
}
}
}
/**
* Sorts the given array according to the bubble sort strategy.
*
* @param array The array to be sorted.
* @param length The length of the array.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void bubble_sort_array(int array[], int length, criterion_fn criterion) {
}

View file

@ -1,12 +1,12 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S05
* Title: Bubble sort
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Implements the bubble sort algorithm
* Implements the bubble sort strategy
* ----------------------------------------------------------
*/
#ifndef ___BUBBLE_SORT_H
@ -18,22 +18,22 @@
/* NOTE: Either list or array variant is required! */
/**
* Sorts the given list according to the bubble sort algorithm.
* Sorts the given list according to the bubble sort strategy.
*
* @param list The list to be sorted.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void bubble_sort_list(<params>);
void bubble_sort_list(IntList list, criterion_fn criterion);
/**
* Sorts the given array according to the bubble sort algorithm.
* Sorts the given array according to the bubble sort strategy.
*
* @param array The array to be sorted.
* @param length The length of the array.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void bubble_sort_array(<params>);
void bubble_sort_array(int array[], int length, criterion_fn criterion);
#endif

View file

@ -1,13 +1,53 @@
/*-----------------------------------------------------------------------------
* HTBLA-Leonding
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S05
* Title: Insertion sort implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implements the insertion sort algorithm
* Implements the insertion sort strategy
*-----------------------------------------------------------------------------
*/
#include "insertion_sort.h"
/**
* Sorts the given list according to the insertion sort strategy.
*
* @param list The list to be sorted.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void insertion_sort_list(IntList list, criterion_fn criterion) {
if (!list_is_valid(list) || criterion == 0) {
return;
}
int size = list_get_size(list);
if (size <= 1) {
return;
}
for (int i = 1; i < size; i++) {
int current = list_get_at(list, i);
int j = i - 1;
while (j >= 0 && !criterion(list_get_at(list, j), current)) {
list_swap(list, j, j + 1);
j--;
}
}
}
/**
* Sorts the given array according to the insertion sort strategy.
*
* @param array The array to be sorted.
* @param length The length of the array.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void insertion_sort_array(int array[], int length, criterion_fn criterion) {
}

View file

@ -1,12 +1,12 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S05
* Title: Insertion sort
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Implements the insertion sort algorithm
* Implements the insertion sort strategy
* ----------------------------------------------------------
*/
#ifndef ___INSERTION_SORT_H
@ -18,22 +18,22 @@
/* NOTE: Either list or array variant is required! */
/**
* Sorts the given list according to the insertion sort algorithm.
* Sorts the given list according to the insertion sort strategy.
*
* @param list The list to be sorted.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void insertion_sort_list(<params>);
void insertion_sort_list(IntList list, criterion_fn criterion);
/**
* Sorts the given array according to the insertion sort algorithm.
* Sorts the given array according to the insertion sort strategy.
*
* @param array The array to be sorted.
* @param length The length of the array.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void insertion_sort_array(<params>);
void insertion_sort_array(int array[], int length, criterion_fn criterion);
#endif

View file

@ -3,7 +3,7 @@
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Title: Insertion sort implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implements the insertion sort algorithm
@ -11,3 +11,56 @@
*/
#include "insertion_sort_binary_search.h"
#include "binary_search.h"
/**
* Sorts the given list according to the insertion sort algorithm using binary.
*
* @param list The list to be sorted.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void insertion_sort_binsearch_list(IntList list, criterion_fn criterion) {
if (!list_is_valid(list) || criterion == 0) {
return;
}
int size = list_get_size(list);
if (size <= 1) {
return;
}
for (int i = 1; i < size; i++) {
int current = list_get_at(list, i);
IntList temp = list_obtain();
for (int k = 0; k < i; k++) {
list_insert(temp, list_get_at(list, k));
}
int pos = binary_search_list_limited(temp, i, criterion, current);
if (pos < 0) {
pos = -pos;
}
if (pos < i) {
list_remove_at(list, i);
list_insert_at(list, pos, current);
}
list_release(&temp);
}
}
/**
* Sorts the given array according to the insertion sort algorithm using binary search.
*
* @param array The array to be sorted.
* @param length The length of the array.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void insertion_sort_binsearch_array(int array[], int length, criterion_fn criterion) {
}

View file

@ -3,7 +3,7 @@
* ---------------------------------------------------------
* Exercise Number: S07
* Title: Insertion sort implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Implements the insertion sort algorithm using binary search
@ -24,7 +24,7 @@
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void insertion_sort_binsearch_list(<params>);
void insertion_sort_binsearch_list(IntList list, criterion_fn criterion);
/**
* Sorts the given array according to the insertion sort algorithm using binary search.
@ -34,6 +34,6 @@ void insertion_sort_binsearch_list(<params>);
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void insertion_sort_binsearch_array(<params>);
void insertion_sort_binsearch_array(int array[], int length, criterion_fn criterion);
#endif

View file

@ -3,7 +3,7 @@
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Title: Linear search implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implements the linear search algorithm
@ -12,3 +12,41 @@
#include "linear_search.h"
/**
* Searches the given needle within the given haystack using linear search approach.
*
* @param haystack The list in which the `needle` is searched.
* @param needle The value to find within the haystack.
* @return The index of the `needle` within the list or a value less than zero
* if the needle was not not found.
*/
int linear_search_list(IntList haystack, int needle) {
if (!list_is_valid(haystack)) {
return -1;
}
int size = list_get_size(haystack);
for (int i = 0; i < size; i++) {
if (list_get_at(haystack, i) == needle) {
return i;
}
}
return -1;
}
/* ARRAY VARIANT */
/**
* Searches the given needle within the given haystack using binary search approach.
*
* @param haystack The sorted list in which the `needle` is searched.
* @param length The length of the haystack array.
* @param needle The value to find within the haystack.
* @return The index of the `needle` within the array or a value less than zero
* if the needle was not not found.
*/
int linear_search_array(int haystack[], int length, int needle) {
return -1;
}

View file

@ -3,7 +3,7 @@
* ---------------------------------------------------------
* Exercise Number: S07
* Title: Linear search implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Implements the linear search algorithm
@ -24,7 +24,7 @@
* @return The index of the `needle` within the list or a value less than zero
* if the needle was not not found.
*/
<type> linear_search_list(<params>);
int linear_search_list(IntList haystack, int needle);
/* ARRAY VARIANT */
@ -38,6 +38,6 @@
* @return The index of the `needle` within the array or a value less than zero
* if the needle was not not found.
*/
<type> linear_search_array(<params>);
int linear_search_array(int haystack[], int length, int needle);
#endif

View file

@ -1,13 +1,74 @@
/*-----------------------------------------------------------------------------
* HTBLA-Leonding
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S06
* Title: Insertion sort implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implements the merge sort algorithm
* Implements the merge sort strategy
*-----------------------------------------------------------------------------
*/
#include "merge_sort.h"
static void merge_sort_list_merge(IntList list, IntList left, IntList right, criterion_fn criterion) {
while (list_get_size(left) > 0 && list_get_size(right) > 0) {
if (criterion(list_get_at(left, 0), list_get_at(right, 0))) {
list_transfer_head(left, list);
} else {
list_transfer_head(right, list);
}
}
list_append(list, left);
list_append(list, right);
}
static IntList merge_sort_list_divide_merge(IntList list, IntList result, criterion_fn criterion) {
if (list_get_size(list) <= 1) {
list_append(result, list);
return result;
}
IntList left = list_obtain(), right = list_obtain();
IntList left_result = list_obtain(), right_result = list_obtain();
list_split(list, left, right, list_get_size(list) / 2);
merge_sort_list_divide_merge(left, left_result, criterion);
merge_sort_list_divide_merge(right, right_result, criterion);
merge_sort_list_merge(result, left_result, right_result, criterion);
list_release(&left);
list_release(&right);
list_release(&left_result);
list_release(&right_result);
return result;
}
/**
* Sorts the given list according to the merge sort strategy.
*
* @param list The list to be sorted.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void merge_sort_list(IntList list, criterion_fn criterion) {
IntList result = list_obtain();
result = merge_sort_list_divide_merge(list, result, criterion);
list_clear(list);
list_append(list, result);
list_release(&result);
}
/**
* Sorts the given array according to the merge sort strategy.
*
* @param array The array to be sorted.
* @param length The length of the array.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void merge_sort_array(int array[], int length, criterion_fn criterion) {
}

View file

@ -1,12 +1,12 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S06
* Title: Insertion sort implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Implements the merge sort algorithm
* Implements the merge sort strategy
* ----------------------------------------------------------
*/
#ifndef ___MERGE_SORT_H
@ -18,22 +18,22 @@
/* NOTE: Either list or array variant is required! */
/**
* Sorts the given list according to the merge sort algorithm.
* Sorts the given list according to the merge sort strategy.
*
* @param list The list to be sorted.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void merge_sort_list(<params>);
void merge_sort_list(IntList list, criterion_fn criterion);
/**
* Sorts the given array according to the merge sort algorithm.
* Sorts the given array according to the merge sort strategy.
*
* @param array The array to be sorted.
* @param length The length of the array.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void merge_sort_array(<params>);
void merge_sort_array(int array[], int length, criterion_fn criterion);
#endif

View file

@ -3,7 +3,7 @@
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Title: Quick sort implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implements the quick sort algorithm
@ -11,3 +11,62 @@
*/
#include "quick_sort.h"
/* LIST VARIANT */
static int partition_list(IntList list, int low, int high, criterion_fn criterion) {
int pivot_value = list_get_at(list, high);
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (criterion(list_get_at(list, j), pivot_value)) {
i++;
list_swap(list, i, j);
}
}
list_swap(list, i + 1, high);
return (i + 1);
}
static void quick_sort_list_helper(IntList list, int low, int high, criterion_fn criterion) {
if (low < high) {
int pivot_idx = partition_list(list, low, high, criterion);
quick_sort_list_helper(list, low, pivot_idx - 1, criterion);
quick_sort_list_helper(list, pivot_idx + 1, high, criterion);
}
}
/**
* Sorts the given list according to the quick sort algorithm.
*
* @param list The list to be sorted.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void quick_sort_list(IntList list, criterion_fn criterion) {
if (!list_is_valid(list) || criterion == 0) {
return;
}
int size = list_get_size(list);
if (size <= 1) {
return;
}
quick_sort_list_helper(list, 0, size - 1, criterion);
}
/* ARRAY VARIANT */
/**
* Sorts the given array according to the quick sort algorithm.
*
* @param array The array to be sorted.
* @param length The length of the array.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void quick_sort_array(int array[], int length, criterion_fn criterion) {
}

View file

@ -3,7 +3,7 @@
* ---------------------------------------------------------
* Exercise Number: S07
* Title: Quick sort
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Implements the quick sort algorithm
@ -24,7 +24,7 @@
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void quick_sort_list(<params>);
void quick_sort_list(IntList list, criterion_fn criterion);
/**
* Sorts the given array according to the quick sort algorithm.
@ -34,6 +34,6 @@ void quick_sort_list(<params>);
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void quick_sort_array(<params>);
void quick_sort_array(int array[], int length, criterion_fn criterion);
#endif

107
sorting.c
View file

@ -1,9 +1,9 @@
/*-----------------------------------------------------------------------------
* HTBLA-Leonding / Class: <your class name here>
* HTBLA-Leonding / Class: 2IHIF
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S05
* Title: Sorting support functions
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Basic support functions for sorting
@ -11,6 +11,12 @@
*/
#include "sorting.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "bubble_sort.h"
#include "insertion_sort.h"
/**
* This file provides some basic functions for sorting and value initialization.
@ -25,3 +31,98 @@
* Generate random values via function `random()`
* Limit random value to `MAX_VALUE` as defined in `config.h`
*/
static int is_random_initialized = 0;
/**
* Provides the name of the given sorting algorithm.
*
* @param algorithm The sorting algorithm
* @return The name of the algorithm.
*/
char* get_algorithm_name(SortingAlgorithm algorithm) {
switch (algorithm) {
case BUBBLE_SORT:
return "Bubble Sort";
case INSERTION_SORT:
return "Insertion Sort";
default:
return "Unknown Algorithm";
}
}
/**
* Initializes the given list with random elements.
*
* @param list The list to initialize.
* @param item_count The number of items to insert.
*/
void init_list_random(IntList list, int item_count) {
if (!list_is_valid(list)) {
return;
}
if (!is_random_initialized) {
srandom(time(NULL));
is_random_initialized = 1;
}
list_clear(list);
for (int i = 0; i < item_count; i++) {
int random_value = random() % (MAX_VALUE + 1);
list_insert(list, random_value);
}
}
/**
* Prints the values stored in the given list.
*
* @param prefix The optional text to print before values are printed.
* @param list The list to dump.
*/
void print_list(char* prefix, IntList list) {
if (!list_is_valid(list)) {
return;
}
if (prefix != NULL) {
printf("%s", prefix);
}
printf("[");
for (int i = 0; i < list_get_size(list); i++) {
printf("%d", list_get_at(list, i));
if (i < list_get_size(list) - 1) {
printf(", ");
}
}
printf("]\n");
}
/**
* Sorts the given list using the given sorting algorithm.
*
* @param list The list to sort.
* @param algorithm The sorting algorithm to use.
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void sort_list(IntList list, SortingAlgorithm algorithm, criterion_fn criterion) {
if (!list_is_valid(list)) {
return;
}
switch (algorithm) {
case BUBBLE_SORT:
bubble_sort_list(list, criterion);
break;
case INSERTION_SORT:
insertion_sort_list(list, criterion);
break;
default:
break;
}
}

View file

@ -1,9 +1,9 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S05
* Title: Sorting support functions
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Interface for sorting algorithms
@ -26,7 +26,6 @@ typedef enum {
BUBBLE_SORT,
FIRST_ALGORITHM = BUBBLE_SORT,
INSERTION_SORT,
MERGE_SORT,
ALGORITHM_ENUM_END
} SortingAlgorithm;
@ -36,7 +35,7 @@ typedef enum {
* @param algorithm The sorting algorithm
* @return The name of the algorithm.
*/
<type> get_algorithm_name(<params>);
char* get_algorithm_name(SortingAlgorithm algorithm);
#ifdef LIST_VARIANT
@ -47,7 +46,7 @@ typedef enum {
* @param list The list to initialize.
* @param item_count The number of items to insert.
*/
void init_list_random(<params>);
void init_list_random(IntList list, int item_count);
/**
* Prints the values stored in the given list.
@ -65,7 +64,7 @@ void print_list(char* prefix, IntList list);
* @param criterion The pointer to the function that implements the sorting criterion.
* That function accepts two integer parameters and returns a boolean value.
*/
void sort_list(<params>);
void sort_list(IntList list, SortingAlgorithm algorithm, criterion_fn criterion);
#else /* ARRAY_VARIANT */

View file

@ -1,9 +1,9 @@
/*-----------------------------------------------------------------------------
* HTBLA-Leonding / Class: <your class name here>
* HTBLA-Leonding / Class: 2IHIF
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S05
* Title: Sorting criteria
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implementation of sorting criteria
@ -11,3 +11,25 @@
*/
#include "sorting_criteria.h"
/**
* Determines whether or not `fst` is smaller than or equal to `snd` (ascending order).
*
* @param fst The value that is supposed being smaller than `snd`.
* @param snd The value to compare.
* @return True if `fst` is smaller than or equal to `snd`, false otherwise.
*/
bool is_in_asc_order(int fst, int snd) {
return fst <= snd;
}
/**
* Determines whether or not `fst` is greater than or equal to `snd` (descending order).
*
* @param fst The value that is supposed being greater than `snd`.
* @param snd The value to compare.
* @return True if `fst` is greater than or equal to `snd`, false otherwise.
*/
bool is_in_desc_order(int fst, int snd) {
return fst >= snd;
}

View file

@ -1,9 +1,9 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S05
* Title: Sorting criteria
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Interface for sorting algorithms
@ -11,12 +11,12 @@
*/
#ifndef ___SORTING_CRITERIA_H
#define ___SORTING_CRITERIA_H
#ifndef ___SORTING_CRITERIA_H
#define ___SORTING_CRITERIA_H
#include <stdbool.h>
#include <stdbool.h>
/**
/**
* Declaration of function pointer for comparison function.
* A criterion function determines whether or not the given
* values are in the order that is defined by the criterion.
@ -26,24 +26,25 @@
* @return True if `fst` IS actually ordered before `snd`
* (the values are in order), false otherwise.
*/
/* Note: Name the pointer type 'criterion_fn' */
/* Note: Name the pointer type 'criterion_fn' */
typedef bool (criterion_fn)(int fst, int snd);
/**
/**
* Determines whether or not `fst` is smaller than or equal to `snd` (ascending order).
*
* @param fst The value that is supposed being smaller than `snd`.
* @param snd The value to compare.
* @return True if the criterion is satisfied, false otherwise.
*/
<type> is_in_asc_order(<params>);
bool is_in_asc_order(int fst, int snd);
/**
/**
* Determines whether or not `fst` is larger than or equal to `snd` (descending order).
*
* @param fst The value that is supposed being smaller than `snd`.
* @param fst The value that is supposed being greater than `snd`.
* @param snd The value to compare.
* @return True if the criterion is satisfied, false otherwise.
*/
<type> is_in_desc_order(i<params>);
bool is_in_desc_order(int fst, int snd);
#endif
#endif

View file

@ -1,9 +1,9 @@
/*-----------------------------------------------------------------------------
* HTBLA-Leonding / Class: <your class name here>
* HTBLA-Leonding / Class: 2IHIF
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S05
* Title: Stopwatch
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implementation of a simple stopwatch
@ -25,3 +25,71 @@
* -> seconds = ticks / CLOCK_PER_SEC
* microseconds are s * 10^-6
*/
static clock_t start_time = 0;
static clock_t passed_time = 0;
static bool is_active = false;
/**
* Starts taking the time. This function always starts at 0.
*/
void stopwatch_start()
{
start_time = clock();
passed_time = 0;
is_active = true;
}
/**
* Stops or pauses taking the time. Time measurement can be resumed
* via `stopwatch_resume`.
*/
void stopwatch_stop()
{
if (!is_active) {
return;
}
passed_time += clock() - start_time;
is_active = false;
}
/**
* Resumes taking the time. The previously measured time value is
* used as start value.
*/
void stopwatch_resume()
{
if (is_active) {
return;
}
start_time = clock();
is_active = true;
}
/**
* Determines whether or not the stopwatch takes the time.
* @return True if the stopwatch is measuring, false otherwise.
*/
bool stopwatch_is_active()
{
return is_active;
}
/**
* The measured time in microseconds.
*
* @return Either the processor time elapsed since start_stopwatch() has been called or
* the processor time elapsed between the calls of start_stopwatch() and stop_stopwatch().
*/
double stopwatch_get_elapsed_time()
{
clock_t total_time = passed_time;
if (is_active) {
total_time += clock() - start_time;
}
return ((double)total_time / CLOCKS_PER_SEC) * 1000000.0;
}

View file

@ -1,9 +1,9 @@
/*----------------------------------------------------------
* HTBLA-Leonding
* ---------------------------------------------------------
* Exercise Number: S07
* Exercise Number: S06
* Title: Stopwatch
* Author: */<your name>;/*
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* A stop watch to measure CPU time

BIN
test_timed_sorting Executable file

Binary file not shown.