Reputation: 3039
I am trying to reverse an array in 2 ways:
1) By creating a new array which was very easy:
public static int[] reverse(int[] array) {
int[] reverseArray = new int[array.length];
for(int i = 0; i < reverseArray.length; i++) {
reverseArray[i] = array[array.length - i - 1];
}
return reverseArray;
}
2) The second method I got my answer but I actually don't understand it very well, it actually makes use of swapping, giving the value of the array to a temporary variable then changes it and returns it to the original variable:
public static int[] reverse2(int[] array)
{
for (int i=0; i < array.length / 2; i++)
{
int temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
return array;
}
Could someone explain to me the second code? I don't understand the division by 2? What happens if the array size is even or odd?
Upvotes: 1
Views: 20913
Reputation: 81
Simple and quick...
public class ReverseAnIntegerArray {
static void reverseAnArray(int[] arrNum) {
System.out.println("Original Array :" + Arrays.toString(arrNum));
for (int i = arrNum.length - 1; i >= 0; i--) {
System.out.print(arrNum[i] + " ");
}
}
public static void main(String[] args) {
int myArr[] = { 1, 2, -3, 4, 5, 34, 50 };
reverseAnArray(myArr);
}
}
Out put will be - Original Array :[1, 2, -3, 4, 5, 34, 50]
50 34 5 4 -3 2 1
Upvotes: 0
Reputation: 1
The divide by 2 won't work entirely. It will only work if you have an odd number of integers.
For instance:
Give me an integer that would represent the length of an array: 5
Enter 5 value(s)
Value #0: 1
Value #1: 2
Value #2: 3
Value #3: 4
Value #4: 5
Your current array: 1 | 2 | 3 | 4 | 5 |
Your array reversed: 5 | 4 | 3 | 2 | 1 | BUILD SUCCESSFUL (total time: 11 seconds)
Now, if you were to put in an even number integers, let's say 6, this is what would happen:
Give me an integer that would represent the length of an array: 6
Enter 6 value(s)
Value #0: 1
Value #1: 2
Value #2: 3
Value #3: 4
Value #4: 5
Value #5: 6
Your current array: 1 | 2 | 3 | 4 | 5 | 6 |
Your array reversed: 6 | 5 | 3 | 4 | 2 | 1 | BUILD SUCCESSFUL (total time: 5 seconds)
Source code:
/* Write a program that prompts the user for an integer that would represent the length of an array, then asks the user to enter that many values. Store these values in an array and print the array. Then reverse the array elements so that the first element becomes the last element, the second element becomes the second to last element, and so on, with the old last element now first. Do not just reverse the order in which they are printed; actually change the way they are stored in the array. Do not create a second array; just rearrange the elements within the array you have. (Hint: Swap elements that need to change places.) When the elements have been reversed, print the array again. */
package reversinganarray;
import java.util.Scanner;
public class ReversinganArray {
public static void main(String[] args) {
int i = 0;
Scanner input = new Scanner(System.in);
System.out.print("Give me an integer that would represent the length of an array: ");
int integer = input.nextInt();
int[] test = new int[integer];
System.out.println("Enter " + integer + " " + "value(s)");
while (i < integer) {
System.out.println("Value #" + i + ": ");
test[i] = input.nextInt();
i++;
}
System.out.print("Your current array: ");
i = 0;
while (i < integer) {
System.out.print(test[i] + " | ");
i++;
}
i = 0;
while (i <= integer / 2) {
int temp = test[i]; //a = b
test[i] = test[(integer - i - 1)]; //b = c
test[(integer - i - 1)] = temp;// c = a
i++;
}
System.out.println("");
System.out.print("Your array reversed: ");
i = 0;
while (i <= integer - 1) {
System.out.print(test[i] + " | ");
i++;
}
}
}
I happen to be trying to figure this problem out myself...
Upvotes: -2
Reputation: 26530
Imagine your array is this:
[ 1 2 3 4 5 ]
The second solution you've posted works as follows:
[ 1 2 3 4 5 ]
^--swap-^
[ 5 2 3 4 1 ]
^swp^
[ 5 4 3 2 1 ]
As you can see, you only need to traverse half of the array for this to work (hence making it perform better than the first solution, where you need to traverse the whole thing). This is where the division by two comes in; half of the array equates to only needing to check elements up to array.length / 2
.
For an even number of elements, it will do the same thing, only swapping the innermost pair as well:
[ 1 2 3 4 5 6 ]
^--swap---^
[ 6 2 3 4 5 1 ]
^swap-^
[ 6 5 3 4 2 1 ]
^-^
[ 6 5 4 3 2 1 ]
Upvotes: 7
Reputation: 3975
The division by 2 just means that you don't have to loop through all the elements in the array. Since you are reversing the array, while the loop is at the first element, it means that it should just swap it with the first element from the other end.
Basically the division by 2 is just to reduce the number of passes of the loop. Think of it as a performance enhancement.
The loop still works fine regardless if the number of elements in the array is odd or even. If the number of elements is odd, the loop stops before the middle element.
Upvotes: 2
Reputation: 963
The array is divided by two because you will swap positions 0 and n, 1 and n-1, etc. If the array has an odd amount of values, the last value should be directly in the center of the array and will not need to be swapped. We can loop over the array size divided by n because only n/2 swaps need to happen.
Upvotes: 1
Reputation: 1647
The division by 2 is merely so you only go through the first half of the array. If you swap the first and last items, you don't want to do it again when i reaches array.length. If the size is even, it will stop before the second half, if the size is odd, it will stop before the center position, which doesn't need to be switched anyway. Hope that helps!
Upvotes: 9