question on revrse array
Posted
by davit-datuashvili
on Stack Overflow
See other posts from Stack Overflow
or by davit-datuashvili
Published on 2010-06-16T10:14:27Z
Indexed on
2010/06/16
10:22 UTC
Read the original article
Hit count: 155
algorithm
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
© Stack Overflow or respective owner