You’ve spent hours tuning quadtrees, k-d trees, and R-trees. You’ve read papers on spatial hashing, read about Hilbert curves, and you still feel like you’re building a jet engine just to organize points on a grid. What if the answer was sitting in plain sight—something so simple you’d feel stupid for missing it?
I stumbled on a GitHub repo that does exactly that. It’s called Cartesian-Grid-Sort, and it proves one thing: you don’t need a distance-aware data structure to build a spatially coherent grid. You just need two sorts.
Here’s the idea: take a bunch of 2D points (x, y). Map them to a grid by sorting first on x, then on y, then on x again, then on y—repeat. After a few alternating one-dimensional sorts, the points settle into a monotonic, spatially coherent order. That’s it. No geometry, no octrees, no fancy math.
Think about what that means. Every time you reach for a spatial index, you’re reaching for complexity. But the machine is already a sorting monster—CPUs have dedicated sort instructions, databases sort billions of rows per second. Why not let the primitive you already have do the work?
This is the kind of ‘aha’ moment that makes you rethink everything you thought you knew about data structures. The algorithm is myopic—each pass only looks at one axis. Yet the global structure emerges, like a chaotic room slowly tidying itself when you just keep putting one thing in order at a time.
I’m not saying this replaces quadtrees for every use case. But if you’re working with point clouds, grid generation, or any problem where you need a fast approximation of spatial locality, this is absurdly effective. And it’s trivial to implement. In fact, the author’s implementation is under 200 lines of Python.
Here’s the twist: most spatial indexing assumes you need to be aware of distance. This algorithm doesn’t even know what distance is. It just sorts. And the result is a grid where nearby points in the raw data end up nearby in the sorted order. Locality becomes an emergent property of sort order—not a geometric index.
I’ve seen this pattern before. The best optimizations often come from realizing you don’t need the complex structure at all. You just need to use what’s already there, harder. Sorting is the most optimized operation in modern computing. Leverage it.
So next time you’re about to implement a k-d tree, ask yourself: can I just sort? The answer might surprise you.
FAQ
Q: How does this algorithm handle points that are far apart but similar in one axis?
A: It doesn't matter. The alternating axes ensure that after a few passes, the ordering becomes globally monotonic. Neighbors in the grid are spatially close in the original space, not just along one axis. The myopia of each pass is corrected by the sequence.
Q: What's the practical advantage over a k-d tree or quadtree?
A: Simplicity and speed. Sorting is hyper-optimized in CPUs and databases. You don't need to build, balance, or traverse a tree. For static or semi-static point sets, this can be orders of magnitude faster, especially if you're already working in a sort-friendly environment like SQL or array-based languages.
Q: Isn't this just a poor man's space-filling curve?
A: No. Space-filling curves like the Hilbert curve require a predetermined mapping that often depends on the grid resolution. This algorithm adapts to the point distribution because it sorts the actual data, not a derived coordinate. It's more like a dynamic, data-driven ordering that emerges from the points themselves.