# Thread: Checking to see if Array is sorted

1. Member
Join Date
Jun 2011
Location
Indiana
Posts
6
Rep Power
0

## Checking to see if Array is sorted

Hi everyone, I'm new to programming & this site has helped me a lot in the past so I thought I would ask another question. I'm trying to create the method isSorted that takes an array just returns true or false if the array is in descending order. I keep getting this error on line 10 saying "isSorted(int[]) in Sort cannot be applied to (int)" I'm not sure what this means.. my approach is to get the number at the index i and compare it to the number at i + 1. Can anyone explain this to me? I appreciate any help.

Java Code:
```class Sort {

public static boolean isSorted(int[] x) {

boolean isSorted = false;

for (int i=0; i < x.length-1; i++) {

if (x[i] < x[i+1]) {
return Sort.isSorted(x[i]); //want to continue looking through list to see if all i is less than i-1

//i get an error on this line saying "isSorted(int[]) in Sort cannot be applied to (int)"

} else if (x[i] > x[i+1]) {
return false;

} else
isSorted = true;

}
}
}```

2. The value x[ i ] is just a single int but your method takes an entire array of ints as a parameter; the compiler saw that and complained ...

kind regards,

Jos

3. Member
Join Date
Jun 2011
Location
Indiana
Posts
6
Rep Power
0
Ahh I see. So is there not a way to use recursion with this problem?
I am familiar with only scheme and know that in a list it's possible to recur by doing something to the car (first item in list) then recuring on the cdr (the rest of the list 1 by 1).

4. Originally Posted by pytho
Ahh I see. So is there not a way to use recursion with this problem?
I am familiar with only scheme and know that in a list it's possible to recur by doing something to the car (first item in list) then recuring on the cdr (the rest of the list 1 by 1).
Java isn't a functional language and a (primitive) array isn't a general list, i.e. it uses indexes and no pointer arithmetic (as in C). You can recur over an array but you have to pass the index from where to recur to your method as well; all in all it isn't worth all the trouble and go for an iterative approach instead.

kind regards,

Jos

5. Gazing into my crystal ball I see a missing return statement error in your future.

6. Member
Join Date
Jun 2011
Location
Indiana
Posts
6
Rep Power
0
Thanks Jos! I don't know why I was trying to use recursion inside of a for loop. The iterative way was not as hard to figure out as I thought it was.

Junky..

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•