Returns true if the array is in increasing order - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

Returns true if the array is in increasing order

Demo Code


//package com.java2s;

public class Main {
    /**// w  ww .  j ava 2s  . c  o m
     * Returns true if the array is in increasing order
     * 
     * You'll want to use a recursive helper function
     * 
     * Examples:
     * {1,2,3,4} returns true
     * {2,3,4} returns true
     * {4,3} returns false
     * {4} returns true
     * 
     * @param array
     * @return true if list is sorted in increasing order
     */
    public static boolean isOrdered(int[] array) {
        return isOrderedHelper(0, array);
    }

    private static boolean isOrderedHelper(int index, int[] array) {
        if (index > array.length - 2) {
            return true;
        } else if (array[index] > array[index + 1]) {
            return false;
        } else {
            return isOrderedHelper(index + 1, array);
        }
    }
}

Related Tutorials