# 对奇偶下标分别排序
import java.util.Arrays;
import java.util.Collections;
/**
* @author yagol
* @date 上午10:30
* @desc the description of this class
**/
class Solution6000 {
public static void main(String[] args) {
System.out.println(Arrays.toString(new Solution6000().sortEvenOdd(
new int[]{4, 1, 2, 3, 6}
)));
}
public int[] sortEvenOdd(int[] nums) {
int[] temp1 = new int[nums.length - nums.length / 2];
int[] temp2 = new int[nums.length/ 2];
int indexone = 0;
int indexsecond = 0;
for (int i = 0; i < nums.length; i++) {
if (i % 2 == 0) {
temp1[indexone] = nums[i];
indexone++;
} else {
temp2[indexsecond] = nums[i];
indexsecond++;
}
}
Arrays.sort(temp1);
Arrays.sort(temp2);
int[] res = new int[nums.length];
int index1 = 0;
int index2 = 0;
for (int i = 0; i < res.length; i++) {
if (i % 2 == 0) {
res[i] = temp1[index1];
index1++;
} else {
res[i] = temp2[temp2.length - 1 - index2];
index2++;
}
}
return res;
}
}
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
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