Skip to main content
added 75 characters in body
Source Link
gnasher729
  • 31.1k
  • 35
  • 54

If you know all pointers to nodes, and you have enough free space for a single node, then you iterate through the nodes. For the I-th node, you copy the node stored at address I to the free space, copy the I-th node to address I, remember that the old location of the I-th node is now unused, and update all the pointers to those two nodes. You also need to check whether by coincidence the I-th node is already stored at address I; in that case you must do nothing for this node.

This takes time O(n), so massively faster than any O(n log n) sorting.

If you know all pointers to nodes, and you have enough free space for a single node, then you iterate through the nodes. For the I-th node, you copy the node stored at address I to the free space, copy the I-th node to address I, remember that the old location of the I-th node is now unused, and update all the pointers to those two nodes. You also need to check whether by coincidence the I-th node is already stored at address I; in that case you must do nothing for this node.

If you know all pointers to nodes, and you have enough free space for a single node, then you iterate through the nodes. For the I-th node, you copy the node stored at address I to the free space, copy the I-th node to address I, remember that the old location of the I-th node is now unused, and update all the pointers to those two nodes. You also need to check whether by coincidence the I-th node is already stored at address I; in that case you must do nothing for this node.

This takes time O(n), so massively faster than any O(n log n) sorting.

Source Link
gnasher729
  • 31.1k
  • 35
  • 54

If you know all pointers to nodes, and you have enough free space for a single node, then you iterate through the nodes. For the I-th node, you copy the node stored at address I to the free space, copy the I-th node to address I, remember that the old location of the I-th node is now unused, and update all the pointers to those two nodes. You also need to check whether by coincidence the I-th node is already stored at address I; in that case you must do nothing for this node.