complete a method that swaps the first and second half of an array of integers

I keep getting an out of bounds error whenever i try to run my code. Does anyone know what is wrong with it? I can't seem to figure it out.

public class Swapper{

    /**
    This method swaps the first and second half of the given array.
    @param values an array
     */

    public void swapFirstAndSecondHalf(int[] values) {
        // your work here

        int[] first = new int[values.length/2];
        int[] second = new int[values.length/2];
        for(int i = 0; i < values.length / 2; i++) {
            second[i] = values[i];
        }
        for (int j = values.length / 2; j < values.length; j++) {
            first[j] = values[j];
        }
        for(int k = 0; k < values.length / 2; k++) {
            values[k] = first[k];
        }
        for(int l = values.length / 2; l < values.length; l++) {
            values[l] = second[l];
        }
    }

    // This method is used to check your work
    public int[] check(int[] values) {
        swapFirstAndSecondHalf(values);
        return values;
    }
}
int[] first = new int[values.length/2];

So indexes [0..values.length/2 - 1] are valid for first.

for (int j=values.length/2; j<values.length; j++)
{
    first[j] = values[j];
}

So with the first value of j being values.length/2, it's already out of bounds.

You need to practice debugging, placing a break point and tracing the code as it executes.

Java Program to Swap First Half With Second Half of Same Array , int[] first = new int[values.length/2];. So indexes [0..values.length/2 - 1] are valid for first . for (int j=values.length/2; j<values.length; j++) { first[j] = values[j]; }. Algorithm : 1. Sort the given array. 2. Run a loop upto half the length of the array and print the elements of the sorted array. 3. Run a loop from last index of the array to the middle of the array and print the elements in reverse order.

You could have used System.arraycopy() instead of all the for looping.

public static void main(String[] args) throws Exception {
    int[] values = {1, 2, 3, 4, 5};
    values = swapFirstAndSecondHalf(values);
    System.out.println(Arrays.toString(values));

    values = new int[]{1, 2, 3, 4, 5, 6};
    values = swapFirstAndSecondHalf(values);
    System.out.println(Arrays.toString(values));
}

public static int[] swapFirstAndSecondHalf(int[] values) {
    boolean evenSize = values.length % 2 == 0;
    int half = values.length / 2;
    int[] swapper = new int[values.length];
    System.arraycopy(values, evenSize ? half : half + 1, swapper, 0, half);
    System.arraycopy(values, 0, swapper, evenSize ? half : half + 1, half);

    // The middle number stays the middle number
    if (!evenSize) {
        swapper[half] = values[half];
    }
    return swapper;
}

Results:

[4, 5, 3, 1, 2]
[4, 5, 6, 1, 2, 3]

If you're wanting the middle number, for an odd sized array, to be part of the second half then the swapFirstAndSecondHalf() would look like this:

public static int[] swapFirstAndSecondHalf(int[] values) {
    boolean evenSize = values.length % 2 == 0;
    int half = values.length / 2;
    int[] swapper = new int[values.length];
    System.arraycopy(values, half, swapper, 0, evenSize ? half : half + 1);
    System.arraycopy(values, 0, swapper, evenSize ? half : half + 1, half);
    return swapper;
}

Results:

[4, 5, 3, 1, 2]
[4, 5, 6, 1, 2, 3]

Find the sum of the first half and second half elements of an array , Program to find sum of even and odd numbers in array. Was this article helpful? Let me know through the comment section below & Don't forget  Java Program to Swap First Half With Second Half of Same Array. Array Swap - Program swaps first half with the second half of the same array. Ex: input : 12, 13, 14, 15. output : 15, 14, 13, 12. In the above example, 12 is replaced with 15 and 13 is replaced with 14. SwapArray.java.

Allocating new arrays is a waste of space. Just swap the halves in-place:

public static void swapFirstAndSecondHalf(int[] values) {
    final int len = values.length / 2;
    final int offset = values.length - len;
    for (int i = 0; i < len; i++) {
        int temp = values[i];
        values[i] = values[offset + i];
        values[offset + i] = temp;
    }
}

The code allows odd length, and will leave center value alone.

C# Cookbook, Function to find the sum of the first half. // elements and second half elements of an array. void sum_of_elements( int arr[], int n). {. int sumfirst = 0, sumsecond = 0;. Given an array of n distinct elements, find the minimum number of swaps required to sort the array. Examples: Input : {4, 3, 2, 1} Output : 2 Explanation : Swap index 0 with 3 and 1 with 2 to form the sorted array {1, 2, 3, 4}.

Sort first half in ascending and second half in descending order, Length / 2); counter*) Note that this is integer division, so if the array length is an Length - counter - 1) = tempHolder; These three lines swap the first half of the array with the second half. Reverse Method” topic in the MSDN documentation. To analyze the bubble sort, we should note that regardless of how the items are arranged in the initial list, \(n-1\) passes will be made to sort a list of size n. Table 1 shows the number of comparisons for each pass. The total number of comparisons is the sum of the first \(n-1\) integers.

C# 3.0 Cookbook: More Than 250 solutions for C# 3.0 Programmers, The algorithm here swaps elements in the array until it is fully reversed. The DoReversal<T> method accepts a single parameter, theArray, which is a array) to a value equal to the array's length divided by two: for (int counter = 0; counter < (theArray. These three lines swap the first half of the array with the second half. Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The array's size may differ from 4.

Java exercises: Swap the first and last elements of an array and , Given an array of integers, sort the first half of the array in ascending order and second half in descending order. Examples: Input : arr[] = {5, 2, 4, 7, 9, 3, 1, 6, 8}  Previous: Write a Java program to get the larger value between first and last element of an array (length 3) of integers. Next: Write a Java program to find the largest element between first, last, and middle values from an array of integers .

Comments
  • Can you provide some sample input that causes the AIOOBE?
  • Try printing out the string representation of the array you modified in each for loop.