Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Swapper
- {
- /**
- This method swaps the first and second half of the given array.
- @param values an array
- */
- public void swapFirstAndSecondHalf(int[] values)
- {
- int i = (values.length % 2 == 0) ? values.length / 2 : values.length / 2 + 1;
- for(int j = 0; j < ((values.length % 2 == 0) ? values.length / 2 : values.length / 2); j++)
- {
- swap(values, j, i);
- i++;
- }
- }
- public void swap(int[] values, int first, int second)
- {
- int temp = values[first];
- values[first] = values[second];
- values[second] = temp;
- }
- // This method is used to check your work
- public int[] check(int[] values)
- {
- swapFirstAndSecondHalf(values);
- return values;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement