-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReArrangeArray.java
More file actions
45 lines (34 loc) · 888 Bytes
/
ReArrangeArray.java
File metadata and controls
45 lines (34 loc) · 888 Bytes
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
package pkg;
public class ReArrangeArray {
/*
* Given a sorted array of positive integers.
* Your task is to rearrange the array elements alternatively
* i.e first element should be max value, second should be min value, third should be second max,
* fourth should be second min and so on.
* */
int[] a,aux;
int len;
ReArrangeArray(int[] a,int len){
this.a = a;
this.aux = new int[len];
this.len = len;
}
void rearrange(){
/*
* It uses an auxilary array to rearrange
*/
int max = len-1,min = 0,i=0;
while( min <= (max/2) ){
System.out.println("copying "+max+" to "+i);
aux[i++]=a[max--];
System.out.println("copying "+min+" to "+i);
aux[i++]=a[min++];
}
}
void printarr(){
for(int i=0;i<len;i++){
System.out.print(aux[i]+" ");
}
System.out.println();
}
}