-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add bottom-up heapsort #23
base: master
Are you sure you want to change the base?
Conversation
On Sun, Jul 26, 2015 at 11:47:42PM -0700, Maxim Zakharov wrote:
"bottom_up_heapsort" is awkwardly long, but "bsort" seems a bit How different is a bottom up heapsort from any other heapsort? Would David Gibson | I'll have my music baroque, and my code |
It is indeed a heapsort sorting, though not a classical implementation, and it can be dubbed just "heapsort" if you do not plan to include classical heapsort implementation into ccan. |
Maxim Zakharov [email protected] writes:
Hi Maxim!
only to be faster than asort (ie. glibc's sort) between 256 and 4096 asort-2 = 0.000003 Thanks, commit ad0a71e85907a50c0b2807b581142139dd937c15
diff --git a/ccan/bottom_up_heapsort/benchmark/Makefile b/ccan/bottom_up_heapsort/benchmark/Makefile
+ccan-asort.o: $(CCANDIR)/ccan/asort/asort.c
diff --git a/ccan/bottom_up_heapsort/benchmark/benchmark.c b/ccan/bottom_up_heapsort/benchmark/benchmark.c
+int main(int argc, char *argv[])
|
Hi Rusty, It is well known quicksort behaves well better heapsort on random data on average, however quicksort's worse case is much worse in comparison to heapsort. There is a quicksort killer method to build a sequence demonstrating it. See http://research.swtch.com/qsort If I switch to ccan built-in implementation which is classical quicksort as I see it shows the following performance on killing quicksort sequence in comparison to bottom_up_heapsort: $ make test diff --git a/ccan/asort/asort.c b/ccan/asort/asort.c -#if !HAVE_QSORT_R_PRIVATE_LAST /* Steal glibc's code. */ diff --git a/ccan/asort/asort.h b/ccan/asort/asort.h -#if HAVE_QSORT_R_PRIVATE_LAST clean:
+test: benchmark
ccan-asort.o: $(CCANDIR)/ccan/asort/asort.c
diff --git a/ccan/bottom_up_heapsort/benchmark/benchmark.c b/ccan/bottom_up_heapsort/benchmark/benchmark.c +/* Copyright 1998, M. Douglas McIlroy
+#include <stdlib.h>
+int
+#if 0
+#include <stdio.h>
+#endif
@@ -38,13 +176,37 @@ int main(int argc, char *argv[])
|
It look like my patch was garbled on github so I attach it here in e-mail On 28 July 2015 at 11:06, Rusty Russell [email protected] wrote:
|
On Mon, Jul 27, 2015 at 08:06:08AM -0700, Maxim Zakharov wrote:
Well, ccan isn't exactly planned at all.. IIUC the bottom up heapsort and ordinary heapsort do share part of the David Gibson | I'll have my music baroque, and my code |
Current glib's qsort() implementation uses "Towers of Hanoi" Mergesort if it can allocate required additional memory without using swap space, otherwise it falls back to quicksort (see https://sourceware.org/ml/libc-alpha/2002-01/msg00218.html ) From performance perspective, asort() module should be better updated to the latest glibc's qsort() implementation with fall back to heapsort instead of quicksort which gives protection against quicksort's worst case and heapsort generally requires less additional memory than quicksort. In general, ccan should not only check qsort_r() availability but also do benchmarking for its asort() against qsort_r() on the system and chose fastest one. |
Maxim Zakharov [email protected] writes:
Indeed.
That's not something you want to do at runtime, and I'm not sure how I would suggest:
Cheers, |
I have added bottom-up heapsort module implementing a modified version of bottom-up heapsort sorting, see details here: http://blog.dataparksearch.org/397
It is unified with asort() to be typesafe and accept context pointer for comparison function.
Would it better to rename bottom_up_heapsort() into bsort() for function name simplicity?