This repository illustrates the core Cartesian sort procedure of the SquareNet ❒ gridification engine for demonstration purposes.
It showcases the live progress of the multi-key Cartesian sorting algorithm 𝄜 and includes an animated GIF alongside the simple Python script used to generate it.
Note: The generalization to higher dimensions is straightforward.
Initialization:
Take
Iterative Sorting Procedure:
- Sort the
$x$ -coordinate along the row key$i$ : update$[i, j]_k \leftarrow [i', j]_k$ where$i'$ ensures the$x_k$ coordinates are sorted along the$i$ -axis (all columns$j$ are processed in parallel). - Sort the
$y$ -coordinate along the column key$j$ : update$[i', j]_k \leftarrow [i', j']_k$ . - Check if the
$x$ -sorting was broken by applying the$y$ -sorting step (which is highly probable). If so, return to step 1 and repeat until both dimensions are simultaneously satisfied.
The algorithm produces a bijective mapping from the raw points RP (shape [4225, 2]: $(x_k, y_k)$) to a gridded tensor GT (shape [65, 65, 2]: $(x_{ij}, y_{ij})$).
Upon termination, the resulting gridded view GT is guaranteed to be monotonic 📈 :
-
$x$ strictly increases along$i$ ($\rightarrow$ ) -
$y$ strictly increases along$j$ ($\uparrow$ )
This ensures that the multi-key
By construction, the transformation is a bijection between the raw point key
A notable aspect of this algorithm is its proof of termination, which is relatively simple and establishes a link to Optimal Transport.
Based on the classical Rearrangement Inequality, each sorting step strictly decreases the following global structural quantity (average energy) on the GT:
This provides a solid monovariant guaranteeing that no cycles will occur, and thus, that the algorithm will mathematically terminate. In practical—and even adversarial—cases, no more than 100 total iterations are typically required.