27.16 PositionSorted

PositionSorted( list, elm )
PositionSorted( list, elm, func )

In the first form PositionSorted returns the position of the element elm, which may be an object of any type, with respect to the sorted list list.

In the second form PositionSorted returns the position of the element elm, which may be an object of any type with respect to the list list, which must be sorted with respect to func. func must be a function of two arguments that returns true if the first argument is less than the second argument and false otherwise.

PositionSorted returns pos such that list[pos-1] < elm and elm <= list[pos]. That means, if elm appears once in list, its position is returned. If elm appears several times in list, the position of the first occurrence is returned. If elm is not an element of list, the index where elm must be inserted to keep the list sorted is returned.

    gap> PositionSorted( [1,4,5,5,6,7], 0 );
    1
    gap> PositionSorted( [1,4,5,5,6,7], 2 );
    2
    gap> PositionSorted( [1,4,5,5,6,7], 4 );
    2
    gap> PositionSorted( [1,4,5,5,6,7], 5 );
    3
    gap> PositionSorted( [1,4,5,5,6,7], 8 );
    7 

Position (see Position) is another function that returns the position of an element in a list. Position accepts unsorted lists, uses linear instead of binary search and returns false if elm is not in list. Previous Up Top Next
Index

GAP 3.4.4
April 1997