Given an array of (unsorted) integers, arrange them such that a < b > c < d > e... etc.
---------------------------------------------------------------------------------
After using a heap, I found O(n) is enough...
private static void arrangeList(int A[]) {
for (int i = 1; i < A.length; i++) {
if (i % 2 == 1 && A[i] < A[i - 1])
swap(A, i, i - 1);
if (i % 2 == 0 && A[i] > A[i - 1])
swap(A, i, i - 1);
}
}
private static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
The above solution isn't right. Go to https://blog.youkuaiyun.com/taoqick/article/details/105310050