Skip to content

Commit 4e0a15f

Browse files
visitorckwakpm00
authored andcommitted
lib/sort: clarify comparison function requirements in sort_r()
Patch series "lib: clarify comparison function requirements", v2. Add a detailed explanation in the sort_r/list_sort kernel doc comment specifying that the comparison function must satisfy antisymmetry and transitivity. These properties are essential for the sorting algorithm to produce correct results. Issues have arisen in the past [1][2][3][4] where comparison functions violated the transitivity property, causing sorting algorithms to fail to correctly order elements. While these requirements may seem straightforward, they are commonly misunderstood or overlooked, leading to bugs. Highlighting these properties in the documentation will help prevent such mistakes in the future. Link: https://lore.kernel.org/lkml/[email protected] [1] Link: https://lore.kernel.org/lkml/[email protected] [2] Link: https://lore.kernel.org/lkml/[email protected] [3] Link: https://lore.kernel.org/lkml/[email protected] [4] This patch (of 2): Add a detailed explanation in the sort_r() kernel doc comment specifying that the comparison function must satisfy antisymmetry and transitivity. These properties are essential for the sorting algorithm to produce correct results. Issues have arisen in the past [1][2][3][4] where comparison functions violated the transitivity property, causing sorting algorithms to fail to correctly order elements. While these requirements may seem straightforward, they are commonly misunderstood or overlooked, leading to bugs. Highlighting these properties in the documentation will help prevent such mistakes in the future. Link: https://lkml.kernel.org/r/[email protected] Link: https://lore.kernel.org/lkml/[email protected] [1] Link: https://lore.kernel.org/lkml/[email protected] [2] Link: https://lore.kernel.org/lkml/[email protected] [3] Link: https://lore.kernel.org/lkml/[email protected] [4] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Kuan-Wei Chiu <[email protected]> Cc: Ching-Chun (Jim) Huang <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent bb2de9b commit 4e0a15f

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/sort.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ static size_t parent(size_t i, unsigned int lsbit, size_t size)
200200
* copy (e.g. fix up pointers or auxiliary data), but the built-in swap
201201
* avoids a slow retpoline and so is significantly faster.
202202
*
203+
* The comparison function must adhere to specific mathematical
204+
* properties to ensure correct and stable sorting:
205+
* - Antisymmetry: cmp_func(a, b) must return the opposite sign of
206+
* cmp_func(b, a).
207+
* - Transitivity: if cmp_func(a, b) <= 0 and cmp_func(b, c) <= 0, then
208+
* cmp_func(a, c) <= 0.
209+
*
203210
* Sorting time is O(n log n) both on average and worst-case. While
204211
* quicksort is slightly faster on average, it suffers from exploitable
205212
* O(n*n) worst-case behavior and extra memory requirements that make

0 commit comments

Comments
 (0)