wiggle sort solution in O(n)