-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternative Sorting.java
More file actions
62 lines (49 loc) · 1.61 KB
/
Alternative Sorting.java
File metadata and controls
62 lines (49 loc) · 1.61 KB
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
//{ Driver Code Starts
// Initial Template for Java
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("~");
}
}
}
// } Driver Code Ends
// User function Template for Java
class Solution {
public static ArrayList<Integer> alternateSort(int[] arr) {
// Your code goes here
ArrayList<Integer>ans=new ArrayList<>();
Arrays.sort(arr);
int start=0;int end=arr.length-1;
while(start<=arr.length/2 && end>=arr.length/2){
if(start==end){
ans.add(arr[end]);
end--;
continue;
}
ans.add(arr[end]);
ans.add(arr[start]);
start++;
end--;
}
return ans;
}
}