Does anyone know of an algorithm for sorting that is reversible? So given this:
5,39,196,0,15,243
Sort would create:
0,5,15,39,196,243
And then reversing it without having to know any knowledge, other than perhaps which sort algorithm was used and how many iterations were run, it would go back to:
5,39,196,0,15,243
It occurred to me that bubble sort may possibly work- as long as I knew how many times the sort had to run, I could probably just reverse the steps that number of times to get back to the original. Are there any others?
This is for an experiment so time complexity isn’t an issue (I don’t care how slow it is).
If you’re really not concerned with runtime efficiency, you can use Permutation Sort.
It will generate all permutations of the list, and check which permutation has the list ordered. If you know how many rounds you’ve run before you find the sorted list, you know exactly what values have been swapped to get there.
If you run it on an unsorted list and have to run 50 rounds to get to the sorted list, you’ll know exactly what elements were swapped to get there and can reverse the sort.