G.3.16 Sorting values into arrays

For the semantics of the parameter ARRY see Conventions of the arrays API. For the semantics of the parameter ISEQUAL see Conventions of the arrays API. For the semantics of the parameter ISLESS see Conventions of the arrays API.

Function: mbfl_array_is_sorted ARRY
Function: mbfl_array_is_sorted ARRY ISLESS
Function: mbfl_array_is_sorted_less ARRY
Function: mbfl_array_is_sorted_less ARRY ISLESS

Return true if the values in ARRY are sorted left–to–right in strict increasing order according to ISLESS; otherwise return false.

mbfl_declare_index_array_varref(ARRY1, (a b c d))
mbfl_declare_index_array_varref(ARRY2, (a C D d))
mbfl_declare_index_array_varref(ARRY3, (a b b c))

mbfl_array_is_sorted _(ARRY1)    ⇒ 0
mbfl_array_is_sorted _(ARRY2)    ⇒ 1
mbfl_array_is_sorted _(ARRY3)    ⇒ 1
Function: mbfl_array_is_sorted_greater ARRY
Function: mbfl_array_is_sorted_greater ARRY ISLESS

Return true if the values in ARRY are sorted left–to–right in strict decreasing order according to ISLESS; otherwise return false.

mbfl_declare_index_array_varref(ARRY1, (a b c d))
mbfl_declare_index_array_varref(ARRY2, (d c b a))
mbfl_declare_index_array_varref(ARRY3, (c b b a))

mbfl_array_is_sorted_greater _(ARRY1)   ⇒ 1
mbfl_array_is_sorted_greater _(ARRY2)   ⇒ 0
mbfl_array_is_sorted_greater _(ARRY3)   ⇒ 1
Function: mbfl_array_is_sorted_leq ARRY
Function: mbfl_array_is_sorted_leq ARRY ISEQUAL ISLESS

Return true if the values in ARRY are sorted left–to–right in non–strict increasing order according to ISEQUAL and ISLESS; otherwise return false.

mbfl_declare_index_array_varref(ARRY1, (a b c d))
mbfl_declare_index_array_varref(ARRY2, (d c b a))
mbfl_declare_index_array_varref(ARRY3, (a b b c))

mbfl_array_is_sorted_leq _(ARRY1)       ⇒ 0
mbfl_array_is_sorted_leq _(ARRY2)       ⇒ 1
mbfl_array_is_sorted_leq _(ARRY3)       ⇒ 0
Function: mbfl_array_is_sorted_geq ARRY
Function: mbfl_array_is_sorted_geq ARRY ISEQUAL ISLESS

Return true if the values in ARRY are sorted left–to–right in non–strict decreasing order according to ISEQUAL and ISLESS; otherwise return false.

mbfl_declare_index_array_varref(ARRY1, (a b c d))
mbfl_declare_index_array_varref(ARRY2, (d c b a))
mbfl_declare_index_array_varref(ARRY3, (c b b a))

mbfl_array_is_sorted_geq _(ARRY1)       ⇒ 1
mbfl_array_is_sorted_geq _(ARRY2)       ⇒ 0
mbfl_array_is_sorted_geq _(ARRY3)       ⇒ 0
Function: mbfl_array_quicksort_bang ARRY
Function: mbfl_array_quicksort_bang ARRY ISLESS

Apply the quicksort 2–partition algorithm to sort the values in ARRY; this function mutates ARRY itself. Comparison between values is performed with ISLESS.

mbfl_declare_index_array_varref(ARRY1, (e b d a c))

mbfl_array_quicksort_bang _(ARRY1)
mbfl_slots_qvalues(ARRY1)        ⇒ a b c d e

mbfl_declare_index_array_varref(ARRY2, (55 33 88 99 22 11 44 77 66))

mbfl_array_quicksort_bang _(ARRY2) mbfl_integer_less
mbfl_slots_qvalues(ARRY2)        ⇒ 11 22 33 44 55 66 77 88 99
Function: mbfl_array_quicksort3_bang ARRY
Function: mbfl_array_quicksort3_bang ARRY ISLESS ISEQUAL

Apply the quicksort 3–partition algorithm to sort the values in ARRY; this function mutates ARRY itself. Comparison between values is performed with ISLESS and ISEQUAL.

mbfl_declare_index_array_varref(ARRY1, (e b d a c))

mbfl_array_quicksort3_bang _(ARRY1)
mbfl_slots_qvalues(ARRY1)        ⇒ a b c d e

mbfl_declare_index_array_varref(ARRY2, (55 33 88 99 22 11 44 77 66))

mbfl_array_quicksort3_bang _(ARRY2) mbfl_integer_less mbfl_integer_equal
mbfl_slots_qvalues(ARRY2)        ⇒ 11 22 33 44 55 66 77 88 99
Function: mbfl_array_insertsort_bang ARRY NEW_VALUE
Function: mbfl_array_insertsort_bang ARRY NEW_VALUE ISLESS

Insert NEW_VALUE in an already sorted ARRY so that the resulting array is still sorted; this function mutates ARRY itself. If ARRY is not sorted: the behaviour of this function is undefined. Comparison between values is performed with ISLESS.

mbfl_declare_index_array_varref(ARRY, (A B C E F G))

mbfl_array_insertsort_bang _(ARRY) D
mbfl_slots_qvalues(ARRY)        ⇒ A B C D E F G

If NEW_VALUE equals values already in the array: it is inserted after the values already there.

function isless () {
    mbfl_mandatory_parameter(VALUE1, 1, first value)
    mbfl_mandatory_parameter(VALUE2, 2, second value)

    mbfl_string_less mbfl_string_idx(VALUE1, 0) mbfl_string_idx(VALUE2, 0)
}

mbfl_declare_index_array_varref(ARRY, (A B C DA DB E F G))

mbfl_array_insertsort_bang _(ARRY) D0 isless
mbfl_slots_qvalues(ARRY)        ⇒ A B C DA DB D0 E F G

This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.