From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: akpm@linux-foundation.org
Cc: lkml@sdf.org, jserv@ccns.ncku.edu.tw,
linux-kernel@vger.kernel.org,
Kuan-Wei Chiu <visitorckw@gmail.com>
Subject: [PATCH 1/2] lib/sort: Optimize heapsort for equal elements in sift-down path
Date: Sat, 13 Jan 2024 11:13:51 +0800 [thread overview]
Message-ID: <20240113031352.2395118-2-visitorckw@gmail.com> (raw)
In-Reply-To: <20240113031352.2395118-1-visitorckw@gmail.com>
Currently, when searching for the sift-down path and encountering equal
elements, the algorithm chooses the left child. However, considering
that the height of the right subtree may be one less than that of the
left subtree, selecting the right child in such cases can potentially
reduce the number of comparisons and swaps.
For instance, when sorting an array of 10,000 identical elements, the
current implementation requires 247,209 comparisons. With this patch,
the number of comparisons can be reduced to 227,241.
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
lib/sort.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/sort.c b/lib/sort.c
index b399bf10d675..fe4efd4a1410 100644
--- a/lib/sort.c
+++ b/lib/sort.c
@@ -262,7 +262,7 @@ void sort_r(void *base, size_t num, size_t size,
* average, 3/4 worst-case.)
*/
for (b = a; c = 2*b + size, (d = c + size) < n;)
- b = do_cmp(base + c, base + d, cmp_func, priv) >= 0 ? c : d;
+ b = do_cmp(base + c, base + d, cmp_func, priv) > 0 ? c : d;
if (d == n) /* Special case last leaf with no sibling */
b = c;
--
2.25.1
next prev parent reply other threads:[~2024-01-13 3:14 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-13 3:13 [PATCH 0/2] lib/sort: Optimize the number of swaps and comparisons Kuan-Wei Chiu
2024-01-13 3:13 ` Kuan-Wei Chiu [this message]
2024-01-13 3:13 ` [PATCH 2/2] lib/sort: Optimize heapsort with double-pop variation Kuan-Wei Chiu
2024-06-20 15:36 ` Julian Sikorski
2024-06-20 20:17 ` Mario Limonciello
2024-06-28 15:15 ` Linux regression tracking (Thorsten Leemhuis)
2024-06-28 17:10 ` Kuan-Wei Chiu
2024-06-29 5:03 ` Linux regression tracking (Thorsten Leemhuis)
[not found] ` <20240630210809.37550-1-visitorckw@gmail.com>
2024-06-30 21:13 ` [PATCH] ACPI: processor_idle: Fix invalid comparison with insertion sort for latency Kuan-Wei Chiu
2024-07-01 4:42 ` [PATCH v2] " Kuan-Wei Chiu
2024-07-01 5:06 ` Greg KH
2024-07-01 15:17 ` Mario Limonciello
2024-07-01 16:10 ` [PATCH v3] " Kuan-Wei Chiu
2024-07-01 17:36 ` Rafael J. Wysocki
2024-07-01 20:56 ` [PATCH v4] " Kuan-Wei Chiu
2024-07-02 7:28 ` Julian Sikorski
2024-07-02 12:59 ` Mario Limonciello
2024-07-02 18:38 ` Rafael J. Wysocki
2025-01-15 3:27 ` [PATCH 2/2] lib/sort: Optimize heapsort with double-pop variation Luke Jones
2025-01-15 12:49 ` Kuan-Wei Chiu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240113031352.2395118-2-visitorckw@gmail.com \
--to=visitorckw@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=jserv@ccns.ncku.edu.tw \
--cc=linux-kernel@vger.kernel.org \
--cc=lkml@sdf.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox