Data Structures and Algorithms: Problem Name: Checking for sorted array (Solution in Java)

//To check if array is a sorted array.

package DS;

public class CheckForSortedArray {

public static void main(String[] args) {

int[] a = new int[] {3,5,6,8,9};

CheckForSortedArray isCheckForSortedArray = new CheckForSortedArray();

System.out.println(isCheckForSortedArray.isArraySorted(a, a.length));

int[] b = new int[] {7,4,3,8,9};

System.out.println(isCheckForSortedArray.isArraySorted(b, b.length));

}

public boolean isArraySorted(int[] a,int arrayLength)

{

if(a.length == 1)

return true;

if(arrayLength == 1)

return true;

return (Boolean) (a[arrayLength-1] < a[arrayLength-2] ? false : isArraySorted(a,arrayLength-1));

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s