-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArrayReversing.java
91 lines (68 loc) · 1.78 KB
/
ArrayReversing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.Arrays;
public class ArrayReversing
{
public static void main(String[] args)
{
int[] arr = {1,2,3,4,5,6,7,8,9} ;
// swap(arr, 5, 1) ;
// reversing(arr);
another_way(arr);
System.out.println(Arrays.toString(arr));
}
static void swap(int[] arr, int index1, int index2)
{
arr[index1]^=arr[index2];
arr[index2]^=arr[index1];
arr[index1]^=arr[index2];
// int temp = arr[index1];
// arr[index1] = arr[index2] ;
// arr[index2] = temp ;
}
static void reversing(int[] arr)
{
int start = 0 ;
int end = arr.length-1 ;
while (start < end)
{
swap(arr, start, end);
start++ ;
end-- ;
}
}
static void another_way(int[] swapp)
{
int end = swapp.length;
for (int i = 0; i < swapp.length/2; i++)
{
// temp = swapp[i] ;
// swapp[i] = swapp[end-i-1] ;
// swapp[end-i-1] = temp ;
swapp[i]^=swapp[end-i-1];
swapp[end-i-1]^=swapp[i];
swapp[i]^=swapp[end-i-1];
}
// for (int element : swapp)
// {
// System.out.print(element + " ");
// // System.out.print(element + " ");
// }
static void interchange(int a[][],int r, int c){
for(int i=0; i<r; i++)
{
// a[i][0]=a[i][0]^a[i][c-1];
// a[i][c-1]=a[i][0]^a[i][c-1];
// a[i][0]=a[i][0]^a[i][c-1];
int temp = a[i][0];
a[i][0] = a[i][c-1];
a[i][c-1] = temp;
}
for(int i = 0;i<r;i++){
for(int j = 0;j<c;j++){
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
}
}