ZigZag array implementation I have implemented a zigzag array which will sort smaller greater smaller and goes on...
For example :
3 7 4 8 2 6 1 is a valid ordering.
The relationship between numbers is
3 &lt; 7 &gt; 4 &lt; 8 &gt; 2 &lt; 6 &gt; 1
3 smaller than 7
7 greater than 4
4 smaller than 8
8 greater than and so on 

the ordering always has to be &quot;smaller than&quot; then &quot;greater than&quot; then &quot;smaller than&quot;...
Are there any other suggestions for improving the solution?
Thanks.
package main.algorithms;

public class ZigZag {

    public static void main(String[] args) {
        int[] array = {4,3,7,8,6,2,1};
        zigZag(array);
        for (int i: array){
            System.out.println(i);
        }
    }

    // Time O(n)
    // Space O(1)
    private static int[] zigZag(int[] array) {

        int status = 0;
        int counter = 0;
        while(counter &lt; array.length-1){
            if(status ==0) {
                if (array[counter] &gt; array[counter + 1]) {
                    int temp = array[counter + 1];
                    array[counter + 1] = array[counter];
                    array[counter] = temp;
                }
                status = 1;
            } else {
                if (array[counter] &lt; array[counter + 1]) {
                    int temp = array[counter + 1];
                    array[counter + 1] = array[counter];
                    array[counter] = temp;
                }
                status = 0;
            }
            counter++;
        }
        return array;
    }
}
Instead of that while-loop, I'd use a for-loop and use the common loop/index variable i.
Instead of a separate status variable, I'd simply use i % 2.
I'd deduplicate the two cases.
Putting the current two array elements into variables avoids duplicating the longer array[i] and array[i + 1] and simplifies the swap.
As Joop Eggen points out, it's probably better to not modify the array in-place and return it. (Unless you have a good reason to, which it seems you don't, as your own calling code ignores the returned value.)

Code:
    private static void zigZag(int[] array) {
        for (int i = 0; i &lt; array.length - 1; i++) {
            int a = array[i], b = array[i + 1];
            if (i % 2 == 0 ? a &gt; b : a &lt; b) {
                array[i] = b;
                array[i + 1] = a;
            }
        }
    }