News for hackers. Stuff that matters.
Re:Faster Than Dijkstra? by vprcic on Friday February 13, 2026 @12:44PM (#47005433)
Each time a discussion about sorting starts, I'm reminded of a "lively debate" I had with my boss/friend about the most optimal sorting approach. He claimed it's O(n) pointing to counting sort as an example. This didn't sit well with me. A sorting algorithm, I insisted, should be defined something like "a function taking an unsorted array of elements and returning a sorted one". But it seems there is no agreed upon definition and you can have something like counting sort where you get an array in O(n) but still need O(m) to get the index of a specific element. I argued that then the best sorting algorithm is the do-nothing algorithm. It returns the initial array in O(1), but needd O(m log m) to get you the index of an element (it uses merge sort to do it).
Re:Faster Than Dijkstra? by Epa095 on Friday February 13, 2026 @02:24PM (#47006644)
Usually when one talk about sorting, without specifying closer, one means comparison sort [1], which indeed has an average-case lower bound of O(n*log(n)). In more special cases all kinds of other runtimes are possible.

1: https://en.wikipedia.org/wiki/Comparison_sort

Re:Faster Than Dijkstra? by NooneAtAll3 on Friday February 13, 2026 @01:24PM (#47005927)
counting sort is O(nW), where W is largest value

if you don't care about W or it is essentially constant - then it can be dropped

but it is an input parameter that will change execution time

Re:Faster Than Dijkstra? by marcosdumay on Friday February 13, 2026 @02:58PM (#47007029)
It's O(n+W), not O(n*W).

> if you don't care about W or it is essentially constant - then it can be dropped

Also, every algorithm that ends in a real computer is bound to a constant time. That's still not a practical thing to do.

Re:Faster Than Dijkstra? by vlovich123 on Friday February 13, 2026 @08:18PM (#47010296)
The simplification is correct but not correctly stated (even though colloquially it's common to state it that way). Technically it's when some component of the algorithm is dwarfed by another, then you can exclude it (i.e. O(n+W) =~ O(n) when W <<< n, O(mn) =~ O(n) when m <<< n etc). It's colloquially shortened to "when it's a constant" because generally it's constant regardless of program execution whereas the variable parts of the formula actually do change and thus the big-O analysis does still give you a correct lower bound on the performance.

TLDR: You're being unhelpfully pedantic.

Re:Faster Than Dijkstra? by emil-lp on Friday February 13, 2026 @02:22PM (#47006619)
W is the span or range.
Re:Faster Than Dijkstra? by hinkley on Friday February 13, 2026 @02:16PM (#47006561)
I can’t think of a single time I’ve needed a sorted list of only numbers. It’s always numbers and something else, like names or dates. Maybe for median calculations, but I don’t even use those that much either. Especially in telemetry, where mean is easy and median is not.
Re:Faster Than Dijkstra? by pvillano on Friday February 13, 2026 @02:33PM (#47006742)
To be pedantic, median is cheaper than sorting. O(n) with a quicksort-like algorithm.

Also, if you're taking an average of floating point numbers, you might want to sort it first and add from smallest to largest, to better preserve precision

Re:Faster Than Dijkstra? by lscharen on Friday February 13, 2026 @03:09PM (#47007170)
An aside, but I recently learned -- if one is willing to use a very modest amount of memory -- summing floating-point numbers with no loss of precision is effectively a solved problem with the XSUM algorithm.

https://glizen.com/radfordneal/ftp/xsum.pdf

Re:Faster Than Dijkstra? by tialaramex on Friday February 13, 2026 @04:28PM (#47008090)
That paper explains some useful optimisation details, but obviously since the floats are all (either infinity or) some multiple of a known tiny fraction (their smallest non-zero number), we can definitely sum them accurately.
Re:Faster Than Dijkstra? by mjcohen on Friday February 13, 2026 @06:32PM (#47009313)
Not if the ratio between the largest and smallest floats is very large (2^(2^n)) where n is the number of bits in the exponent.
Re:Faster Than Dijkstra? by tialaramex on Friday February 13, 2026 @07:37PM (#47009953)
I think you either haven't thought about this or you did your math wrong.

You need (2^e)+m+1 bits. That is more bits than would fit in the cheap machine integer type you just have lying around, but it's not that many in real terms.

Let's do a tiny one to see though first, the "half-precision" or f16 type, 5 bits of exponent, 10 bits of fraction, 1 sign bit. We need 43 bits. This will actually fit in the 64-bit signed integer type on a modern CPU.

Now lets try f64, the big daddy, 11 exponent, 52 fraction, 1 sign bit so total 2048 + 52 + 1 = 2101 bits. As I said it doesn't fit in our machine integer types but it's much smaller than a kilobyte of RAM.

Edited: I can't count, though it doesn't make a huge difference.

Re:Faster Than Dijkstra? by LegionMammal978 on Saturday February 14, 2026 @05:04AM (#47013261)
You also need some extra bits at the top so that it doesn't overflow (e.g., on an adversarial input filled with copies of the max finite value, followed by just as many copies of its negation, so that it sums to 0). The exact number will depend on the maximum input length, but for arrays stored in addressible memory it will add up to no more than 64 or so.
Re:Faster Than Dijkstra? by tialaramex on Saturday February 14, 2026 @07:18AM (#47014004)
Thanks, you're right there for accumulating excess, I don't think you can actually get to 64 extra bits but sure, lets say 64 extra bits if you want a general purpose in memory algorithm, it's not cheap but we shouldn't be surprised it can be done.
Re:Faster Than Dijkstra? by hinkley on Sunday February 15, 2026 @12:01PM (#47025279)
That’s great for mean, but you don’t need to sort for mean.
Re:Faster Than Dijkstra? by vlovich123 on Friday February 13, 2026 @02:37PM (#47006792)
If the primary key is the number, it still works (and dates are just numbers by the way) because you can sort a heterogenous dataset by a single numeric key pretty trivially.

But sorting by arbitrary strings like names can’t avoid comparison sort.

Re:Faster Than Dijkstra? by hinkley on Sunday February 15, 2026 @12:05PM (#47025328)
That data structure isn’t an array of numbers, it’s an array of pointers to objects.
Re:Faster Than Dijkstra? by geocar on Saturday February 14, 2026 @04:09AM (#47012960)
> I can’t think of a single time I’ve needed a sorted list of only numbers.

Gosh. Let me try to convince you.

I use permutation arrays all the time: lists of indexes that can be used across multiple vectors.

This is much faster than the pattern of scanning rows, constructing tuples of (thingToSort . thingIWantInThatOrder) and making a custom sort function, and destructuring those tuples...

And really, not having to write custom sort functions is really really nice.

> Especially in telemetry, where mean is easy and median is not.

Funny. Yes median is obvious with a permutation array, and maybe mean is less so.

When your data is really big and not very variable, mean of x is roughly the same as the mean of any sufficient sample of x, and that sample can be meaningfully represented as a permutation array!

You can get such an array with reservoir sampling and some maths, and (depending on what you know of your data and variance) sometimes even simpler tricks.

That's kindof actually how the "faster than dijkstra" trick referred-to in the article works: Data sets with small variance has this same property that the min of x is roughly the same as the min of a sufficient sample of x (where the size of sufficient has to do with the variance). And so on.

Another big use-case in my code is trees: Apter trees have a flat memory layout which is convenient for permutation arrays which can simultaneously represent index, rotation, tombstones, and all sorts of other things you might need to do with a tree.

Give it a dig. There's good stuff in there.

Re:Faster Than Dijkstra? by charleslmunger on Saturday February 14, 2026 @09:12AM (#47014678)
You have a list of IDs, and want to make them compact for storage or transport - fast and simple way is to sort and delta encode.
Re:Faster Than Dijkstra? by hinkley on Sunday February 15, 2026 @12:05PM (#47025317)
Hmm. That’s fair, though I’d probably use set operations instead. What you find though is that for most other problems besides diffing, ID order is not chronological order, so you need to sort by a date stamp instead. But I’m typically letting the database do that, so I’m a consumer of sorted numbers, but not an implementor. Because what I sort is nearly always compound sorts. By field A, then field B and field C if those two still don’t cut it.
Re:Faster Than Dijkstra? by myhf on Friday February 13, 2026 @01:52PM (#47006269)
"ordering" means arranging things in order by some metric.

"sorting" means assigning things into bins (which are usually ordered).

Re:Faster Than Dijkstra? by emil-lp on Friday February 13, 2026 @02:23PM (#47006631)
This is news to me. Source?
Re:Faster Than Dijkstra? by nh23423fefe on Friday February 13, 2026 @04:00PM (#47007757)
"The children were sorted in to two lines by gender then ordered by height"

You might substitute "sorted by height" but its certainly not a correction. While "ordered into lines" would be an error.

Re:Faster Than Dijkstra? by thom on Friday February 13, 2026 @03:55PM (#47007699)
What do you do when you sort your washing?
Re:Faster Than Dijkstra? by ninalanyon on Friday February 13, 2026 @04:47PM (#47008279)
The sorting office for a postal service.
Re:Faster Than Dijkstra? by michaelt on Friday February 13, 2026 @07:47PM (#47010030)
That's because it's not true.

https://www.merriam-webster.com/dictionary/ordering

Order - transitive verb - 1. to put in order : arrange - "The books are ordered alphabetically by author."

noun - 4. b(1) the arrangement, organization, or sequence of objects or of events - "alphabetical/chronological/historical order" "listed the items in order of importance"

https://www.merriam-webster.com/dictionary/sorting

Sort - transitive verb - 1. to put in a certain place or rank according to characteristics - "sort the mail" "sorted the winners from the losers" "sorting the data alphabetically"

noun - 5. an instance of sorting - "a numeric sort of a data file"

Re:Faster Than Dijkstra? by rkomorn on Friday February 13, 2026 @10:36PM (#47011279)
I don't get how this disagrees with what GGP wrote.
Re:Faster Than Dijkstra? by aGHz on Friday February 13, 2026 @10:28PM (#47011238)
https://dictionary.cambridge.org/dictionary/english/sort

to put a number of things in an order or to separate them into groups: Paper, plastic, and cans are sorted for recycling.

sort something into something I'm going to sort these old books into those to be kept and those to be thrown away.

sort something by something You can use the computer to sort the newspaper articles alphabetically, by date, or by subject.

sort (through) She found the ring while sorting (through) some clothes.

Re:Faster Than Dijkstra? by tpoacher on Friday February 13, 2026 @01:09PM (#47005724)
You reminded me of "Sleep Sort" [0]

[0] https://news.ycombinator.com/item?id=2657277

Re:Faster Than Dijkstra? by pelcg on Friday February 13, 2026 @04:47PM (#47008281)
Don't know about if Sleep Sort even is a valid sorting algorithm? Is this even real?
Re:Faster Than Dijkstra? by tpoacher on Monday February 16, 2026 @03:40AM (#47032492)
it's real (in that it works) but obviously it's a programming joke rather than anything anyone would ever use in a real system.
Re:Faster Than Dijkstra? by onecommentman on Saturday February 14, 2026 @01:17AM (#47012091)
Obligatory mention of the linear time analog sorting algorithm known as the “spaghetti algorithm”.

https://en.wikipedia.org/wiki/Spaghetti_sort

https://every-algorithm.github.io/2023/11/25/spaghetti_sort....

The equivalent for positive link weight shortest paths is the “string algorithm” — build the network out of string and pull taut between the two knots/nodes, the resulting set of taut links is the shortest path.