集册 Java实例教程 如果数组按递增顺序返回true

如果数组按递增顺序返回true

欢马劈雪     最近更新时间:2020-01-02 10:19:05

408
如果数组按升序排列,则返回true


//package com.nowjava;


public class Main {// 来自 n o w j a   v  a . c o m - 时  代  Java

    /**

     * 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 + 
展开阅读全文