1
0
Fork 0

Finished implementing binary search, need to implement tests for it still.

This commit is contained in:
Wael Karram 2021-10-20 12:48:34 +03:00
parent 6d667cf75a
commit 333521e25d
2 changed files with 26 additions and 17 deletions

View File

@ -8,29 +8,36 @@
#include "../utils/swap_void.h"
#endif /* BINARY_SEARCH_C */
/* Function implementations. */
/* Actual function doing the search, does not do any sanity-checking! */
/* index_start is the search start index (inclusive), index_end is the search end index (exclusive). */
int binary_search_nocheck(void* array, void *value, int (*comparison_function)(const void*, const void*), const int length, size_t size, int index_start, int index_end) {
/* Stopping conditions. */
if (index_end <= index_start)
return (comparison_function(/* FILL THIS IN! */) == 0) ? /* FILL THIS IN! */ : BINARY_SEARCH_INDEX_NOT_FOUND;
/* Recurse. */
return (comparison_function(/* FILL THIS IN! */) == 0) ? /* FILL THIS IN! */ : (comparison_function(/* FILL THIS IN! */) == 1)
/* WRITE THE FUNCTION! */
}
/* Note that if the comparison function is NULL, the array pointer is NULL or the search value is NULL, then the function will quit. */
/* Note that the comparison function should return 1 if the second argument is larger than the first, zero if they're both equal and -1 otherwise. */
/* The array's length should be passed in the length argument. */
/* Size should match a single object's size, and should be > 0. */
/* The function returns BINARY_SEARCH_INDEX_NOT_FOUND on error or not found, otherwise will return the index of the value. */
int binary_search(void* array, void *value, int (*comparison_function)(const void*, const void*), const int length, size_t size) {
/* Local variables. */
int index_left, index_right, current_index;
/* Sanity check. */
if (array == NULL || value == NULL || comparison_function == NULL || size <= 0 || length <= 0)
return BINARY_SEARCH_INDEX_NOT_FOUND;
/* The values seem to be OK, delegate the search. */
return binary_search_nocheck(array, value, comparison_function, length, size);
/* Input values are correct, commence the search. */
/* Set the initial index values. */
index_left = 0;
index_right = length - 1;
/* Loop and search. */
while (index_left <= index_right) {
/* Calculate the current index. */
current_index = (index_left + index_right) / 2;
/* Check where the index is in the array in comparison to the value we're looking for. */
if (comparison_function(array + current_index, value) == BINARY_SEARCH_INDEX_LESS_THAN)
index_left = current_index + 1;
else if (comparison_function(array + current_index, value) == BINARY_SEARCH_INDEX_LARGER_THAN)
index_right = current_index - 1;
else
return current_index;
}
/* Search failed! */
return BINARY_SEARCH_INDEX_NOT_FOUND;
}

View File

@ -3,9 +3,11 @@
#define BINARY_SEARCH_H
/* Constants. */
#define BINARY_SEARCH_INDEX_NOT_FOUND -1
#define BINARY_SEARCH_COMPARISON_LARGER_THAN 1
#define BINARY_SEARCH_COMPARISON_LESS_THAN -1
#define BINARY_SEARCH_INDEX_NOT_FOUND -1
/* Function definitions. */
int binary_search_nocheck(void* array, void* value, int (*comparison_function)(const void*, const void*), const int length, size_t size, int index_start, int index_end);
int binary_search(void* array, void* value, int (*comparison_function)(const void*, const void*), const int length, size_t size);
#endif /* BINARY_SEARCH_H */