Check if array is sorted using recursion

Objec­tive:  Given an array of integer write a recursive solution to check if array is sorted.

Example:

int [] a = {1,2,3,4};
Output: true

int [] a = {1,2,3,4,2};
Output: false

Approach: This problem can easily be solved in single iteration by just comparing adjacent elements. Fun part is to write the recursive solution. Code is self explanatory.

Code:

public class IsSortedUsingRecursion {
public static boolean isSorted(int [] a, int start){
if(start==a.length1)
return true;
if(a[start]<=a[start+1])
return isSorted(a, start+1);
else
return false;
}
public static void main(String[] args) {
int [] a = { 1,2,3,4,8,8,22,50};
System.out.println(isSorted(a,0));
}
}


Output:

true