C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
binary_search.c File Reference

Iterative + recursive binary search. More...

#include "binary_search.h"
#include <string.h>

Go to the source code of this file.

Functions

size_t binary_search_ints (const int *arr, size_t n, int target)
 
size_t binary_search_ints_recursive (const int *arr, size_t lo, size_t hi, int target)
 
size_t binary_search_strings (const char *const *arr, size_t n, const char *target)
 

Detailed Description

Iterative + recursive binary search.

Note the lo + (hi - lo) / 2 midpoint computation — (lo + hi) / 2 would overflow on huge arrays. With size_t we'd need n > SIZE_MAX/2 to actually trip it, but the safer form is free.

Definition in file binary_search.c.

Function Documentation

◆ binary_search_ints()

size_t binary_search_ints ( const int *  arr,
size_t  n,
int  target 
)

Definition at line 13 of file binary_search.c.

References BSEARCH_NOT_FOUND.

Referenced by main().

◆ binary_search_ints_recursive()

size_t binary_search_ints_recursive ( const int *  arr,
size_t  lo,
size_t  hi,
int  target 
)

Definition at line 46 of file binary_search.c.

References binary_search_ints_recursive(), and BSEARCH_NOT_FOUND.

Referenced by binary_search_ints_recursive().

◆ binary_search_strings()

size_t binary_search_strings ( const char *const *  arr,
size_t  n,
const char *  target 
)

Definition at line 29 of file binary_search.c.

References BSEARCH_NOT_FOUND.