Algorithms#
A general-purpose set of serial (single-threaded, not parallelized) algorithms.
Dynamic Programming#
All DP modules follow the same lifecycle: initialize a handle, call the compute
function(s), then destroy. The caller provides the handle (on the stack is
fine); internal scratch memory is always heap-allocated, so these modules
can’t be used in a build with RCSW_CONFIG_NOALLOC.
Edit Distance (rcsw/algorithm/edit_dist.h) — Works on any element
type, not just characters; the caller supplies comparator and length
callbacks. Initialize with edit_dist_init(), compute with
edit_dist_find(), then destroy with
edit_dist_destroy().
Longest Common Subsequence (rcsw/algorithm/lcs.h) — Character
sequences only. Initialize with lcs_init(), compute with
lcs_rec() (top-down; returns the length) or lcs_iter()
(bottom-up; preferred for long sequences, and also fills in the subsequence
itself), then destroy with lcs_destroy().
Matrix Chain Optimization (rcsw/algorithm/mcm_opt.h) — Initialize
with mcm_opt_init(), run with mcm_opt_optimize(),
retrieve the optimal ordering with mcm_opt_report() or print it
with mcm_opt_print(), then destroy with
mcm_opt_destroy(). Note that mcm_opt_optimize() must
be called before report or print.
String Parenthesization (rcsw/algorithm/algorithm.h) —
str_is_parenthesizable() tests whether a string can be
parenthesized to produce a target element under a caller-supplied binary
operator. Useful for context-free grammar parsing and expression
evaluation.
Searching#
#include "rcsw/algorithm/search.h"
bsearch_iter() and bsearch_rec() implement binary
search. Both require a sorted input array — see the API documentation for
the warning on unsorted input. Use the iterative variant for large arrays
or constrained stacks; the recursive variant grows the stack O(log n).
Sorting#
#include "rcsw/algorithm/sort.h"
Function |
Average |
Worst Case |
Notes |
|---|---|---|---|
O(n log n) |
O(n²) |
Recursive; depth at most log2(n). Median-of-three pivot, so sorted, reverse-sorted and all-equal input stay O(n log n); only adversarial input reaches O(n²). Not stable. |
|
O(n log n) |
O(n²) |
Same as |
|
O(n²) |
O(n²) |
Only appropriate for very small or nearly-sorted arrays. |
|
O(nk) |
O(nk) |
Non-comparative. |
The comparison sorts take a void* array, its bounds or element count, the
element size, and a comparator int cmpe(const void* e1, const void* e2).
radix_sort() takes an array of size_t keys, a temporary array of
the same size, and a base. None are thread-safe.
Choosing a sort#
RCSW’s sorts are organized by the data access they need, not by container type:
Memory-based sorts (
qsort_rec(),qsort_iter(),insertion_sort(),radix_sort()) work on any contiguous buffer with O(1) random access, swapping elements by copying memory.Structure-based sorts belong to one data structure and use its layout, such as
llist_sort(), which relinks nodes instead of copying them.
A single “universal” sort interface would need access callbacks (get,
set, swap) and an indirect call on every operation, for no real
benefit. The memory-based sorts live in algorithm/; each structure-based
sort lives in the ds/ module that owns its structure.
Memory-based sorts#
These algorithms operate on raw memory buffers and require:
A pointer to the data
The number of elements
The size of each element
A comparison function
Assumptions
Contiguous storage
O(1) random access
Ability to swap elements by copying memory
Characteristics
Fully generic (independent of any data structure)
Reusable across all contiguous containers (e.g., dynamic arrays)
Minimal abstraction overhead
Usage
These algorithms are appropriate for:
Arrays
Dynamic arrays (
darray)Any user-managed contiguous buffer
Structure-based sorts#
These algorithms operate on specific data structures and require:
Pointer traversal
Structural manipulation (e.g., relinking nodes)
Assumptions
Non-contiguous storage
No efficient random access
Direct access to internal structure (e.g., node pointers)
Characteristics
Tightly coupled to the data structure
Optimized for that structure’s properties
Located within the corresponding
ds/moduleDo not attempt to be generic across unrelated structures
Example: linked list sorting
Linked lists cannot efficiently support array-based algorithms like quicksort due to:
Lack of random access
High cost of element swapping
Instead, llist_sort() uses mergesort (recursive or iterative),
because it:
Operates via sequential traversal
Re-links nodes instead of copying memory
Maintains stable performance characteristics