Added binarySearch, and isSorted functions.

This commit is contained in:
Бранимир Караџић
2022-08-07 19:26:28 -07:00
parent 74b5b7ef11
commit 81025f36da
3 changed files with 127 additions and 10 deletions

View File

@@ -10,9 +10,21 @@
namespace bx
{
/// The function compares the `_lhs` and `_rhs` values.
///
/// @returns Returns value:
/// - less than zero if `_lhs` is less than `_rhs`
/// - zero if `_lhs` is equivalent to `_rhs`
/// - greater than zero if `_lhs` is greater than `_rhs`
///
typedef int32_t (*ComparisonFn)(const void* _lhs, const void* _rhs);
/// Performs sort (Quick Sort algorithm).
///
/// @param _data Pointer to sorted array data.
/// @param _num Number of elements.
/// @param _stride Element stride in bytes.
/// @param _fn Comparison function.
///
void quickSort(
void* _data
@@ -55,6 +67,42 @@ namespace bx
, uint32_t _size
);
/// Performs check if array is sorted.
///
/// @param _data Pointer to sorted array data.
/// @param _num Number of elements.
/// @param _stride Element stride in bytes.
/// @param _fn Comparison function.
///
/// @returns Returns `true` if array is sorted, otherwise returns `false`.
///
bool isSorted(
const void* _data
, uint32_t _num
, uint32_t _stride
, const ComparisonFn _fn
);
/// Performs binary search of a sorted array.
///
/// @param _key Pointer to the key to search for.
/// @param _data Pointer to sorted array data.
/// @param _num Number of elements.
/// @param _stride Element stride in bytes.
/// @param _fn Comparison function.
///
/// @remarks Array must be sorted!
///
/// @returns Returns index of element or -1 if the key is not found in sorted array.
///
int32_t binarySearch(
const void* _key
, const void* _data
, uint32_t _num
, uint32_t _stride
, const ComparisonFn _fn
);
} // namespace bx
#include "inline/sort.inl"