Ask HN: What's your favorite elegant/beautiful algorithm?
151–160 of 507 posts
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#152Earlier quoted context omitted.
Isn't this bit: const mid = Math.floor((right + left) / 2); susceptible to overflow? EDIT: Hm, perhaps not (in JS). Number.MAX_SAFE_INTEGER is much greater than I expected.
Indeed, Java's binary search had this bug for a while! It was found through automatic verification, by the way. https://ai.googleblog.com/2006/06/extra-extra-read-all-about...
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#153It is used in DVB-T combined with Reed-Solomon code for example.
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#154Surprised nobody's mentioned Hyper Log Log (or its more recent variants). A probabilistic constant-space unique cardinality estimator (i.e. if I see a billion events, how many are unique?) that also supports set operations.
Count-min-sketch is also beautifully simple.
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#155Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#156Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#157Burrows-Wheeler transform https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transf... Has something of Cantor's diagonal argument about it.
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#158Union-find data structure: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
I love this algorithm (I like implementing type systems) but I always feel a bit naughty when I implement it, as AFAIK it can't be implemented with immutable data structures (not efficiently, at least).
Could you expand on this? This is something I've just started reading about. I'd be interested in good resources to use to get started. At the moment I've just started reading TAPL.
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#159Binary search. Very simple, incredibly powerful; can search on data or math function. It's the basis for other CS concepts. JS implementation: https://gist.github.com/netgusto/90c8e0e7019a832cbf95eac58e1...
Isn't this bit: const mid = Math.floor((right + left) / 2); susceptible to overflow? EDIT: Hm, perhaps not (in JS). Number.MAX_SAFE_INTEGER is much greater than I expected.
mid = (lo + hi) >>> 1
https://docs.julialang.org/en/v1/manual/faq/index.html#Why-d...EDIT: legibility