Here you can find the source of zigZagSort(int[] array)
Parameter | Description |
---|---|
array | input array |
public static int[] zigZagSort(int[] array)
//package com.java2s; //License from project: Open Source License public class Main { /**// ww w . ja va 2 s . c om * Sort an array in zig-zag pattern * i.e. second element is larger than first, * third is smaller than second, * fourth is larger than third, * and so on ... * @param array * input array * @return zig-zag sorted array */ public static int[] zigZagSort(int[] array) { for (int i = 1; i < array.length; i++) { if ((i % 2 == 1 && array[i] < array[i - 1]) || (i % 2 == 0 && array[i] > array[i - 1])) { int temp = array[i]; array[i] = array[i - 1]; array[i - 1] = temp; } } return array; } }