# Square Matrix Permutation

I have a set of items of size N. The items are sorted by probability. A square matrix m[N][N] of those items, in C style memory organization, would have elements with similar probabilities spread out. For example m[0][100] will be very far from m[100][0] and all others with similar probability. I need to permutate the elements in a simple way so the more likely ones tend to be closer to 0. It doesn't need to be a square matrix, it can be a vector [N*N]. And it doesn't need to be perfect, just good enough that elements with similar probability are somewhat grouped together. I'm looking for a function f(i,j) to give the position on the permutated matrix/vector. If possible with very simple operations (e.g. no squares and division but programatic conditionals are OK) For a more graphical reference, I'm looking for something like this. [From BBC's The Story of Maths on Cantor's argument] ![alt text][1] But it doesn't need to be exactly that permutation. Just that the elements walked on the diagonals are mostly grouped nearby. Well, I know this is probably something very simple but it's been many years since school/uni and Wolframalpha isn't helping. Thanks! [1]: http://i.stack.imgur.com/4SXFQ.png
Are you looking for space filling curves? en.wikipedia.org/wiki/Space-filling_curve