SHARE
TWEET

Untitled

advictoriam Jan 25th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Swapper
  2. {
  3.    /**
  4.       This method swaps the first and second half of the given array.
  5.       @param values an array
  6.    */
  7.    public void swapFirstAndSecondHalf(int[] values)
  8.    {
  9.       int i = (values.length % 2 == 0) ? values.length / 2 : values.length / 2 + 1;
  10.       for(int j = 0; j < ((values.length % 2 == 0) ? values.length / 2 : values.length / 2); j++)
  11.       {
  12.          swap(values, j, i);
  13.          i++;
  14.       }
  15.    }
  16.    
  17.    public void swap(int[] values, int first, int second)
  18.    {
  19.       int temp = values[first];
  20.       values[first] = values[second];
  21.       values[second] = temp;
  22.    }
  23.    
  24.    
  25.    // This method is used to check your work
  26.    public int[] check(int[] values)
  27.    {
  28.       swapFirstAndSecondHalf(values);
  29.       return values;
  30.    }
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top