44.1k views
2 votes
Comparable isSorted As a final wrap-up to our series of homework problems on sorting we'll write a function to test if a List of Comparables is sorted is ascending order or not. Implement a public non-final class called sorted that provides a single static method issorted. issorted should accept a List of comparables and return true if the list is sorted and false otherwise. If the list is null issorted should return false. Sorted.java

User Aseidma
by
5.4k points

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

import java.util.List;

public class Sorted {

public static boolean isSorted(List<Comparable> arr) {

for (int i = 1; i < arr.size(); ++i) {

if (arr.get(i).compareTo(arr.get(i - 1)) <= 0) {

return false;

}

}

return true;

}

}

User Cusmar
by
5.6k points