1
0
Fork 0

Initial implementation for binary search - still not done.

This commit is contained in:
Wael Karram 2021-10-20 11:18:22 +03:00
parent d24da97b49
commit 6d667cf75a
3 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,36 @@
/* This file has the implementation of the function for bubble sort. */
/* Include guards + include statements. */
#ifndef BINARY_SEARCH_C
#define BINARY_SEARCH_C
#include <stdlib.h>
#include <string.h>
#include "binary_search.h"
#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) {
/* 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);
}

View File

@ -0,0 +1,11 @@
/* This header defines the function for binary search in a sorted array. */
#ifndef BINARY_SEARCH_H
#define BINARY_SEARCH_H
/* Constants. */
#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 */

View File

@ -4,6 +4,8 @@ TESTS:
1) Fix the bug in the single linked list serialization code.
2) Write tests for doubly-linked list and queue serialization code.
Implement the binary search function.
Future goals:
Implement more data structures, such as hash tables, hash maps, more trees.
Implement algorithms to go with said DSs.