forked from theprogrammedwords/Algorithm-Solutions-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWiggleSort.java
More file actions
63 lines (47 loc) · 1.4 KB
/
WiggleSort.java
File metadata and controls
63 lines (47 loc) · 1.4 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
63
/*
Problem Description
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
Input format
First line contains an integer N ,the number of input integers in the array.
Second line contains N space separated integers.
Output format
N space separated elements which satisfy the given condition.
Sample Input 1
6
1 5 1 1 6 4
Sample Output 1
1 4 1 5 1 6
Explanation 1
nums[0] < nums[1] > nums[2] < nums[3] > nums[4] < nums[5].
Constraints
1 <= N <= 100000
-10^9 <= A[i] <= 10^9
*/
import java.util.*;
class WiggleSort{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer>nums = new ArrayList<>();
for (int i = 0; i < n; i++) {
nums.add(sc.nextInt());
}
List<Integer>ans = wiggleSort(n,nums);
for (int i = 0; i < n; i++) {
System.out.print(ans.get(i)+" ");
}
}
static List<Integer> wiggleSort(int n, List<Integer>nums){
List<Integer> mylist = new ArrayList<Integer>();
mylist.addAll(nums);
int nlen = nums.size() -1;
Collections.sort(mylist);
for(int i=1; i< nums.size(); i+=2){
nums.set(i,mylist.get(nlen--));
}
for(int i=0; i< nums.size(); i+=2){
nums.set(i,mylist.get(nlen--));
}
return nums;
}
}