Inside your loop you basically do the same thing 4 times just with different numbers, you should write a swap function to get rid of all the redundant code.