|I may be looking at this the wrong way, but it looks to me like you're overcomplicating this. Why not use a 2D array to store i and j, and to access the predecessor of i do:|
I'm not sure what you're suggesting here; either I'm misinterpreting your suggestion, or you've misunderstood my problem.
In short, given a sequence of elements (represented by ints here):
5 9 20 1 4 6
I wish to reorganize ("sort") these elements, based on a "relation" function. For example, if (relation(5, 20) > relation (5, 9)), then 9 and 20 are swapped. The actual function is a bit longer, but the idea is the same. The relation function isn't guaranteed to be symmetric (relation (5, 9) != relation (9, 5)) and isn't even guaranteed to be consistent. Technically, it shouldn't give any problems, but the result might vary depending on the starting order.
The problem is that:
a) The predicate takes two elements ("9" and "20"), but needs more information. The other information is stored in nearby cells of the array that is being "sorted". Thus, starting from an element, I need to find a position, which is then used to access a nearby element.
b) The nearby elements change too. When an element changes position, its nearby cells contain different elements.
I think viliml's suggestion might get me there; I'm trying to work it out as we speak.