1

I want to write a method (returning a boolean value) named itisSorted that takes two arguments;

  • data: an integer array
  • n: the number of elements  in the array
 and recursively checks whether the array is sorted. That is, returning true if (and only if) the data array is sorted.

public boolean itisSorted(int [] data, int n)
{
  if(data.length ==0 || data.length==1) 
  return true;
  else if (data[n] > data[n-1]) //here i compare the first two elements 
  return false;
   else //here is where i put the recursive call to check if 
    // the rest of the array is sorted, but I am having difficulties with the 
    // part of the code
}
1
  • If n is the number of elements, data[n] will be out of bounds since the first elements in the array will be data[0], not data[1]. Commented Nov 22, 2013 at 16:35

4 Answers 4

3

I think you want something like this

public static boolean itisSorted(int[] data, int n) {
  // Null or less then 2 elements is sorted.
  if (data == null || n < 2) {
    return true;
  } else if (data[n - 2] > data[n - 1]) {
    // If the element before (n-2) this one (n-1) is greater,
    return false;
  }
  // recurse.
  return itisSorted(data, n - 1);
}

public static void main(String[] args) {
  int [] data = {1,2,3};
  System.out.println(Arrays.toString(data) //
      + (itisSorted(data, data.length) ? " Sorted" : " Unsorted"));
  data = new int[] {3,2,1};
  System.out.println(Arrays.toString(data) //
      + (itisSorted(data, data.length) ? " Sorted" : " Unsorted"));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I see where I went wrong. I can now run through the code to fully understand the problem. Thank you again.
1

Solution using recursion

int isArraySorted(int []a, int index)
{
    if(index == 1 || a.length == 1)
        return 1;
    return (a[index -1] <= a[index-2]) ? 0 : isArraySorted(a, index-1) ;
}

change the condition for descending accordingly.

Comments

1

//with this method there is no need to pass the length of the array

public static boolean isOrdered(int nums[])
{
    // Base Cases
    if (nums.length == 0)
        return true;
    if (nums.length == 1)
        return true;

    int[] temp = new int[nums.length - 1];

    if (nums[0] <= nums[1]) {
        for (int i = 1; i < nums.length; i++) {
            temp[i-1] = nums[i];
        }
        return isSorted(temp);
    } else 
        return false;

}

Comments

0
 public boolean itisSorted(int [] data, int n)
{
if(data.length ==0 || data.length==1) 
  return true;
else if (data[n] > data[n-1]) //here i compare the first two elements 
  return false;
 else
 {
  if(n==data.length-1)
  {
      return true;
  }
  return itisSorted(data, n+1); //recursion, checks next integer in the array
 }

is this the answer you want?

3 Comments

If n is the number of elements, I think he's comparing them backwards. Which is fine, but then recursive call has to use n-1. Also in that case, the first check at data[n] would be out of bounds.
@Frank you are right. The original code refers data[n] and data[n-1] as the first two elements, so I'm assuming you would call itisSorted(data, 1) as the start of the recursion. I will update my answer accordingly soon.
@Henry this function will check if the array is sorted in decreasing order right?

Your Answer

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