Swapping/Sorting elements in an Array

Hello! Ok, so first off, I'm not asking for any help on an assignment or anything here. I'm just asking out of curiosity...

So I just had a Java Exam, and one of the questions asked us to write a method that sorts an array the following way:

So given an array: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

The output would be: {6, 7, 8, 9, 10, 1, 2, 3, 4, 5}

I figured that out by doing this:

Code:

` public static int [] arraySwap(int [] a) {`

int pos = 0;

int mid = a.length / 2;

for(pos = 0; pos < mid; pos = pos + 1) {

int temp = a[pos];

a[pos] = a[mid + pos];

a[mid + pos] = temp;

}

return a

}

However, I could only get it to work to an array with an even number of elements, not odd. So I ended up submitting the question half finished :(doh):

But anyway, I'd like to know how to make this work for an odd array of elements. Can somebody show me?

Cheers.