24-timed-sorting-4/linear_search.c
2025-05-14 09:01:03 +02:00

52 lines
No EOL
1.6 KiB
C

/*-----------------------------------------------------------------------------
* HTBLA-Leonding
*-----------------------------------------------------------------------------
* Exercise Number: S07
* Title: Linear search implementation
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implements the linear search algorithm
*-----------------------------------------------------------------------------
*/
#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;
}