question on reverse array
- by davit-datuashvili
we know algorithm how reverse array of n integers
for (int i=0;i<n/2;i++){
swap(a[i],a[n-1-i]):
}
is this method better according the speed of algorithm or not because swap using xor is more fast then in other method
here is code
public class swap {
public static void main(String[]args){
int a[]=new int[]{2,4,5,7,8,11,13,12,14,24};
System.out.println(" array at the begining:");
for (int i=0;i<a.length;i++){
System.out.println(a[i]);
}
for (int j=0;j<a.length/2;j++){
a[j]^=a[a.length-1-j];
a[a.length-1-j]^=a[j];
a[j]^=a[a.length-1-j];
}
System.out.println("reversed array:");
for (int j=0;j<a.length;j++){
System.out.println(a[j]);
}
}
}
Result:
array at the begining:
2
4
5
7
8
11
13
12
14
24
reversed array:
24
14
12
13
11
8
7
5
4
2