semlib.sort
Algorithm
Bases: ABC
Abstract base class for sorting algorithms.
Sorting algorithms can be used with sort.
Source code in src/semlib/sort/algorithm/algorithm.py
QuickSort
Bases: Algorithm
Quicksort sorting algorithm.
This algorithm uses the Quicksort method to sort items. This algorithm does not provide theoretical guarantees with noisy pairwise comparisons, but standard sorting algorithms can perform well in practice (Qin et al., 2024) even with noisy pariwise comparisons using LLMs.
This algorithm requires O(n log n) pairwise comparisons on average. If you want higher-quality rankings and can tolerate increased costs and latency, you can consider using the BordaCount algorithm instead.
Source code in src/semlib/sort/algorithm/quicksort.py
__init__
__init__(*, randomized: bool = False)
Initialize.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
randomized
|
bool
|
If |
False
|
Source code in src/semlib/sort/algorithm/quicksort.py
BordaCount
Bases: Algorithm
Borda count sorting algorithm.
This algorithm uses the Borda count method to rank items. The algorithm has good theoretical properties for finding approximate rankings based on noisy pairwise comparisons (Shah and Wainwright, 2018).
This algorithm requires O(n^2) pairwise comparisons. If you want to reduce the number of comparisons (to reduce LLM costs), you can consider using the QuickSort algorithm instead.
This algorithm is carefully implemented so that it has O(n) space complexity.
Source code in src/semlib/sort/algorithm/borda_count.py
sort_sync
sort_sync[T](
iterable: Iterable[T],
/,
*,
by: str | None = None,
to_str: Callable[[T], str] | None = None,
template: str | Callable[[T, T], str] | None = None,
task: Task | str | None = None,
algorithm: Algorithm | None = None,
reverse: bool = False,
model: str | None = None,
max_concurrency: int | None = None,
) -> list[T]
Standalone synchronous version of sort.
Source code in src/semlib/sort/sort.py
:::