Well, I did it once and for all. First, the binary search method I used here is used:
public static int binarySearch(String[] array, String word, long resultsArray[]) {
int start = 0;
int end = array.length - 1;
int midPt;
int pos = -1;
int comparisons2 = 0;
long start2 = System.nanoTime();
while (start <= end) {
midPt = (start + end) / 2;
int comparisonResult = array[midPt].compareToIgnoreCase(word);
comparisons2++;
if (comparisonResult == 0) {
pos = midPt;
break;
} else if (comparisonResult < 0) {
start = midPt + 1;
} else {
end = midPt - 1;
}
}
long stop2 = System.nanoTime();
long total2 = stop2 - start2;
resultsArray[0] = total2;
resultsArray[1] = (long) array.length;
resultsArray[2] = (long) comparisons2;
return pos;
}
, , .
235882 . , . , . .
, : , Arrays.sort(...) , compareToIgnoreCase , !. , . , Arrays.sort(...) . , compareTo(...) .
,
- , ,
compareToIgnoreCase compareTo
: equals equalsIgnoreCase. . :
equals: : 725536 ; : 117941; /: 6.15equalsIgnoreCase: : 1064334 ; : 117940; /: 9,02compareToIgnoreCase: : 1619 ; : 16; /: 101.19compareTo: : 763 ; : 16; /: 47,69
, : compareToIgnoreCase 16 , equals!. , 16 , , 124 . , , , , , , - .
, , : 164 compareTo 614 compareToIgnoreCase. 235882 0,3 . , , , .:)
, : binarySearch, . , , . , :
. , , , O (n log n), O (log n), O (n log n). O (n) , , O (n log n). , O (log n) , O (n).
, binarySearch, , 140000000 0,14 . 23000000 equals, , a) b) - .
. , String, , . , , , - , . , ().