Imagine two vectors $V=[v_1, v_2, \ldots, v_n]$, $S=[s_1, s_2, \ldots, s_n]$ where $S$ is a rearrangement of $V$ based on some relation $\leq$. Imagine that, instead of computing $S$ using as input $V$ and $\leq$, and want to returns an index map $Idx$. There are two possibilities, where:
- $Idx[i] = j$ means that $v_i$ must be placed in $s_j$:
for each i in idx S[Idx[i]] = V[i]
- $Idx[i] = j$ means that $v_j$ must be placed in $s_i$:
for each i in idx S[i] = V[Idx[i]]
The first mapping helps you think in terms of "iterate over V" (and store each element in their corresponding new position), while the second mapping helps you think in terms of "iterate over S" (and retrieve the corresponding data from its old position).
Both mappings helps you achieve the same purpose in a linear fashion.
Two questions:
- Are there any "standard names" to name these both types of mappings in the context of sorting algorithms?
- Which strategy is more "efficient" in terms of CPU caching effects, prefetching, etc? Using the first mapping, you read sequentially over V, and do random jumps to write into S, while using the second mapping, you do random jumps to read from V and write sequentially into S. Which is usually more efficient, reading sequentially or writting sequentially?