Explanation is better
I have the following problem: I have a source array of integer values, then I need to order the source array with a suffix comparator and put it in a sorted array. The problem is that I want to know what the temporary complexity of building a suffix array is (sorting the original array)
This is the sort method:
Collections.sort(sorted, new SuffixComparator<Integer>(source));
and this is the SuffixComparator class:
public class SuffixComparator<Type extends Comparable<Type>> implements java.util.Comparator<Integer> { List<Type> array1; List<Type> array2; /** * Constructor with a single array */ public SuffixComparator (ArrayList<Type> array) { array1 = array; array2 = array; } /** * Constructor with two arrays */ public SuffixComparator (List<Type> a1, List<Type> a2) { array1 = a1; array2 = a2; } /** * Constructor with two arrays */ public SuffixComparator (ArrayList<Type> a1, ArrayList<Type> a2) { array1 = a1; array2 = a2; } /** * Compare two suffixes * @param offset1 first offset * @param offset2 second offset */ public int compare (Integer offset1, Integer offset2) { int result; if ( array1 == array2 && offset1.equals(offset2) ) { result = 0; } else { int n1 = offset1; int n2 = offset2; while (n1 < array1.size() && n2 < array2.size() && array1.get(n1).equals(array2.get(n2))) { ++n1; ++n2; } if (n1 == array1.size()) { result = (n2 < array2.size()) ? -1 : 0; } else if (n2 == array2.size()) { result = 1; } else { // n1 < array1.size && n2 < array2.size result = array1.get(n1).compareTo(array2.get(n2)); } } return result; }
}
Any suggestions?
, array1.get() array2.get() O (1) println(), , , . Collections.sort() Java 8 O (n log n) . ( , .) O (1), , , O (k), k - array1.size() array2.size(). , , , O (nk log n).
array1.get()
array2.get()
println()
Collections.sort()
array1.size()
array2.size()
, , , .
, , array1.get() array2.get() . , (, ), , .
if, O (1).
else, O (n1xn2) + O (logn).
n1 = 1 n2 = 2.
O (n1xn2) + O (logn)
3 : sorted, array1 array2. sorted, . , Collections.sort(), AFAIK - O (n log n), n - sorted. "" .
sorted
array1
array2
?