-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alternative sorting
62 lines (47 loc) · 1.5 KB
/
Alternative sorting
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
//Alternative sorting
import java.io.*;
import java.lang.*;
import java.util.*;
class Main {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0) {
ArrayList<Integer> array1 = new ArrayList<Integer>();
String line = read.readLine();
String[] tokens = line.split(" ");
for(String token : tokens) {
array1.add(Integer.parseInt(token));
}
ArrayList<Integer> v = new ArrayList<Integer>();
int[] arr = new int[array1.size()];
int idx = 0;
for(int i : array1) {
arr[idx++] = i;
}
v = new Solution().alternateSort(arr);
for(int i = 0; i < v.size(); i++) {
System.out.print(v.get(i) + " ");
}
System.out.println();
System.out.println("~");
}
}
}
class Solution {
public static ArrayList<Integer> alternateSort(int[] arr) {
ArrayList<Integer> ans = new ArrayList<>();
Arrays.sort(arr);
int i = 0, j = arr.length - 1;
while(i < j) {
ans.add(arr[j]);
ans.add(arr[i]);
j--;
i++;
}
if(arr.length % 2 != 0) {
ans.add(arr[arr.length / 2]);
}
return ans;
}
}