SwapHalves.java.html

  1  import java.util.Arrays;
  2  
  3  public class SwapHalves
  4  {
  5     public static void main(String[] args)
  6     {
  7        int[] values = { 9, 13, 21, 4, 11, 7, 1, 3 };
  8  
  9        int i = 0;
 10        int j = values.length / 2;
 11        while (i < values.length / 2)
 12        {
 13           // Swap values at positions i and j
 14           int temp = values[i];
 15           values[i] = values[j];
 16           values[j] = temp;
 17  
 18           i++;
 19           j++;
 20        }
 21  
 22        System.out.println(Arrays.toString(values));
 23     }
 24  }