John hopkins
John hopkins

Reputation: 21

why will my code not return false if the array is not descending or ascending order?

public class SmallestLargest {

    public static void main(String[] args) {
        int [] arr = {3,6,2,1,23,6,7,54,3,2};
        System.out.println(isIncreasing(arr));

    }
    public static boolean isIncreasing(int[]arr)
    {
        for(int i=1; i<arr.length;i++)
        {
            if(arr[i] > arr[i+1] || arr[i] < arr[i+1] )
                return true;
        }
        return false;
     }

    }

so if the array is int[] arr = {1,2,3} it should return true and if its descending order it should also be true anything else and it should return false, but my output is always true.

Upvotes: 0

Views: 77

Answers (4)

BK1603
BK1603

Reputation: 150

Try this out:

boolean status=false;
public static boolean isIncreasing(int[]arr)
{
    for(int i=0; i<arr.length-1;i++)
    {
        if(arr[i] > arr[i+1] || arr[i] < arr[i+1] )
            status=true;
        else
            return false;
    }
    return true;
 }

Upvotes: 0

OldCurmudgeon
OldCurmudgeon

Reputation: 65811

This will return true if the array is sorted - either ascending or descending. It can also handle flat sections of the array, where all entries are flat, one entry and no entries.

It is debatable whether an empty array is sorted.

public enum Direction {

    Descending {

                @Override
                boolean is(int a, int b) {
                    return b <= a;
                }
            },
    Ascending {
                @Override
                boolean is(int a, int b) {
                    return a <= b;
                }
            };

    abstract boolean is(int a, int b);
}

public static boolean isSorted(int[] n) {
    // All possible directions.
    Set<Direction> possibles = EnumSet.<Direction>allOf(Direction.class);
    for (int i = 0; i < n.length - 1 && possibles.size() > 0; i++) {
        // Check all currently possible directions.
        for (Iterator<Direction> it = possibles.iterator(); it.hasNext();) {
            Direction d = it.next();
            if (!d.is(n[i], n[i + 1])) {
                // Not in that direction.
                it.remove();
            }
        }
    }
    return possibles.size() > 0;
}

public void test(int[] arr) {
    System.out.println(Arrays.toString(arr) + " is " + (!isSorted(arr) ? "not " : "") + "sorted");
}

public void test() {
    test(new int[]{3, 6, 2, 1, 23, 6, 7, 54, 3, 2});
    test(new int[]{3, 4, 5, 6, 7, 8, 9});
    test(new int[]{3, 4, 4, 4, 5, 6, 7, 9});
    test(new int[]{4, 4, 4, 4});
    test(new int[]{1});
    test(new int[]{});
    test(new int[]{100, 50, 25, 12, 6, 3, 1});
}

Upvotes: 0

Serge Nassar
Serge Nassar

Reputation: 166

The problem is as long as you have one value that is smaller than the other your code will return true. Your code should look like this.

public class SmallestLargest {

public static void main(String[] args) {
    int [] arr = {3,6,2,1,23,6,7,54,3,2};
    System.out.println(isIncreasing(arr));

}
public static boolean isIncreasing(int[]arr)
{
    for(int i=0; i<arr.length - 1;i++)
    {
        if(arr[i] > arr[i+1]){
            return false;
        }
    }
    return true;
 }

}

Upvotes: 1

Jens
Jens

Reputation: 69440

If you will check if your Array is assending you have to Change your check:

public class SmallestLargest {

    public static void main(String[] args) {
        int [] arr = {3,6,2,1,23,6,7,54,3,2};
        System.out.println(isIncreasing(arr));

    }
    public static boolean isIncreasing(int[]arr)
    {
        for(int i=0; i<arr.length-1;i++)
        {
            if( arr[i] > arr[i+1] )
                return false;
        }
        return true;
     }

    }

And your loop must end at arr.length-1 If not you get an ArrayIndexOutOfBoundsException.

And you have to start at 0, because Arrays in Java are Zero based.

Upvotes: 3

Related Questions