Advertisement
advictoriam

Untitled

Jan 25th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement