Generic shuffle algorithm