Currently, the stdlib_sorting module provides robust O(N log N) and O(N) sorting algorithms but lacks a fundamental O(N) utility to verify array monotonicity prior to invoking computationally expensive sorting routines. This issue proposes the implementation of a pure, generic is_sorted(array, reverse) function to unblock conditional sorting workflows and data validation. The implementation will utilize fypp templating to generate highly optimized, early-exit validation loops for all module-supported intrinsic types (int8 through int64, sp/dp/qp reals, characters) and derived types (string_type, bitset). The function will return a standard logical scalar indicating whether the array satisfies a non-decreasing order, or a non-increasing order if the optional intent(in) :: reverse logical flag is asserted as .true.. Adding this feature aligns Fortran's stdlib with standard library capabilities found in C++ (std::is_sorted) and Rust, providing an essential primitive for algorithmic optimization.
Currently, the stdlib_sorting module provides robust O(N log N) and O(N) sorting algorithms but lacks a fundamental O(N) utility to verify array monotonicity prior to invoking computationally expensive sorting routines. This issue proposes the implementation of a pure, generic is_sorted(array, reverse) function to unblock conditional sorting workflows and data validation. The implementation will utilize fypp templating to generate highly optimized, early-exit validation loops for all module-supported intrinsic types (int8 through int64, sp/dp/qp reals, characters) and derived types (string_type, bitset). The function will return a standard logical scalar indicating whether the array satisfies a non-decreasing order, or a non-increasing order if the optional intent(in) :: reverse logical flag is asserted as .true.. Adding this feature aligns Fortran's stdlib with standard library capabilities found in C++ (std::is_sorted) and Rust, providing an essential primitive for algorithmic optimization.