1

This is the question : An array is sorted (in ascending order) if each element of the array is less than or equal to the next element.

Write a boolean-valued method named isSorted that accepts an integer array, and the number of elements in the array and returns whether the array is sorted.

Before showing the code : my logic is that an if else-if and else statement should first determine if the size of the array is 0,1,or 2. This is because when the size equals 1 or 2, the program must break. When the size is larger than 2, the program should check arr[size-1] > arr[size-2] and then call the method again with size decremented if that is true and just return false if it is untrue. When I ran that program, the following 2 tests failed : [1,3,2,4] and [2,1,2,3,4]. Because of this I specified that when the size is equal to 2, the method returns false if arr[0] > arr[1] however it didn't work. What am I doing wrong? I don't want to just look up the answer because I am studying for a test so I am sorry if there are repeated answers.

I know the loop is better I just wanted to study recursion

public boolean isSorted(int[] arr, int size) { 


    if(size == 0 || size == 1) { 

         return true; 

    } else if (size == 2) { //this is the part I don't get. 

        if (arr[0] > arr[1]) { 

            return false; 

        } else { 

             isSorted(arr,size-1); 
             return true;   

        }


    } else {

         if (arr[size-1] < arr[size-2]) { 

             return false;  

         } else { 

            isSorted(arr, size-1); 
            return true; 

         }

    }

}
1
  • 1
    There is not restriction in the posted task that you have to use recursion to solve the task. Using simple for loop actually is better approach Commented Oct 5, 2018 at 16:57

2 Answers 2

3

Recursion is not good way to solve this problem. What if your array will be very big and you could get StackOverflowError. Why not to use simple if operator:

public static boolean isSorted(int[] arr, int size) {
    if (arr.length >= 2)
        for (int i = 1; i < arr.length; i++)
            if (arr[i - 1] > arr[i])
                return false;

    return true;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need return the result from isSorted, for example, change:

isSorted(arr, size-1); 
return true; 

to

return isSorted(arr, size-1); 

And, the case else if (size == 2) is redundant. size 2 should have the same logic with size 3, 4, 5, ...


Complete code:

public boolean isSorted(int[] arr, int size) {
    if (size == 0 || size == 1) {
        return true;
    } else {
        if (arr[size - 1] < arr[size - 2]) {
            return false;
        } else {
            return isSorted(arr, size - 1);
        }
    }
}

5 Comments

Thank you, can you explain why we have to have a separate else if statement for when the size equals 2?
@Person No, it's redundant. When the size is 2, it has the same logic with size 3, 4, 5, ...
Seems the size == 0 condition is redundant as well
@Maxim It's a corner case. When the original array's length is 0.
@Person You might want to learn how to use a debugger. It helps a lot. You can debug with IDEs such as Eclipse, IntelliJ IDEA.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.