pydistsim.utils.helpers.measure_sortedness

measure_sortedness(sequence: Iterable[T], key: Callable[[T], Any] = None, reverse: bool = False) float[source]

Measure the sortedness of a sequence. A higher value means the sequence is more sorted.

sortedness = 1 - inversions / (n * (n - 1) / 2), so if sortedness == 1 corresponds to a sorted sequence.

Parameters:
  • sequence (Iterable[T]) – The sequence to measure the sortedness of.

  • key (Callable[[T], Any]) – The key function to use to extract a comparison key from each element.

  • reverse (bool) – Whether to sort the sequence in reverse order.

Returns:

The sortedness of the sequence (between 0 and 1) and the inverted pairs found.

Return type:

tuple[float, list[tuple[int, int]]]