5
\$\begingroup\$

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 < 7 > 4 < 8 > 2 < 6 > 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 "smaller than" then "greater than" then "smaller than"...

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 < array.length-1){
            if(status ==0) {
                if (array[counter] > array[counter + 1]) {
                    int temp = array[counter + 1];
                    array[counter + 1] = array[counter];
                    array[counter] = temp;
                }
                status = 1;
            } else {
                if (array[counter] < array[counter + 1]) {
                    int temp = array[counter + 1];
                    array[counter + 1] = array[counter];
                    array[counter] = temp;
                }
                status = 0;
            }
            counter++;
        }
        return array;
    }
}
\$\endgroup\$
  • \$\begingroup\$ @superb rain I just added extra explanation. \$\endgroup\$ – Neslihan Bozer Aug 3 at 12:02
5
\$\begingroup\$
  • 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 < array.length - 1; i++) {
            int a = array[i], b = array[i + 1];
            if (i % 2 == 0 ? a > b : a < b) {
                array[i] = b;
                array[i + 1] = a;
            }
        }
    }
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Very nice. It would be perfect when advising to use a void result, as the passed array is modified in-situ. Also a proof with pre and post conditions would be feasible, but that would ruin the compactness of your code. \$\endgroup\$ – Joop Eggen Aug 3 at 13:52
  • \$\begingroup\$ @JoopEggen Right, added that (rephrased a bit). \$\endgroup\$ – superb rain Aug 3 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.