forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortUtils.java
More file actions
35 lines (32 loc) · 881 Bytes
/
Copy pathSortUtils.java
File metadata and controls
35 lines (32 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.examplehub.utils;
public class SortUtils {
/**
* Test if the array is sorted.
*
* @param array the array to be check.
* @return {@code true} if given array is sorted, otherwise {@code false}.
*/
public static boolean isSorted(int[] array) {
for (int i = 0; i < array.length - 1; ++i) {
if (array[i] > array[i + 1]) {
return false;
}
}
return true;
}
/**
* Test if the generic array is sorted.
*
* @param array the array to be checked.
* @param <T> the class of the objects in the array.
* @return {@code true} if given array is sorted, otherwise {@code true}.
*/
public static <T extends Comparable<T>> boolean isSorted(T[] array) {
for (int i = 0; i < array.length - 1; ++i) {
if (array[i].compareTo(array[i + 1]) > 0) {
return false;
}
}
return true;
}
}