Hello, I have decided to try and solve one of the most common problems, that is the travelling salesman problem. For some reason my algorithm is not giving me the correct path from lowest distance to highest. I don't know where I'm going wrong but I'd gladly appreciate your input.
The problem is Swap(cities, i, i++);, for two reasons.
1. i is modified so if you go into the else and swap again you will not swap the same cities.
2. The evaluation of arguments are unsequenced. You want the third argument (i++) to be evaluated first so that the second argument (i) evaluates to the new value of i but that's not a safe assumption to make. The code has undefined behaviour so anything is allowed to happen.
I don't think you need to change i at all here. Instead of i++ you probably should use i+1.