, . Unicode , , . , .
.
private static int compareStrings ( String a , String b) {
int i = 0;
while ( i < a . length () && i < b. length ( ) ) {
if (a . charAt ( i ) < b. charAt ( i ) ) {
return -1;
}
if (a . charAt ( i ) > b. charAt ( i ) ) {
return 1;
}
i ++;
}
if (a . length () < b. length ( ) ) {
return -1;
}
if (a . length () > b. length ( ) ) {
return 1;
}
return 0;
}
, .
, . , 1 .
5 .
, 1 , , .
int i = 0;
//time taken 1 unit.
while ( i < a . length () && i < b. length ( ) )
//time taken 5n + 2 unit
5
- 2
length a b - 2
length i - 1
length (&&)
, n .
max n , n , m, , i n. , 5n
i, n, , i < a.length() <=> i < n false. 2 , .
a.length()//1i < a.length()//1
5n+2 while.
, . , , .
if (a . charAt ( i ) < b. charAt ( i ) ) {
return -1;
}
//3n unit of time
if (a . charAt ( i ) > b. charAt ( i ) ) {
return 1;
}
//3n unit of time
n . 3 (2 charAt(i) 1 ).
, , , 6n .
i++;
//n unit of time
, n .
,
if (a . length () < b. length ( ) ) {
return -1;
}
//4 units of time
4 (2 , 1 1 )
, , n <
.
sum = 1 + (5n+2) + (6n) + n + 4
= 12n + 7
sum = 12n + 7
, n , m
n, . , n, m-1 n < m ( )
n = m - 1
12n + 7
sum = 12n + 7
= 12(m-1) + 7
= 12m - 5
sum = 12m - 5
12 - 5
, Big-O O (m)
Big-O
f(x) g(x)

if and only if, for some sufficiently large constant x 0 , for which it exists m, for which below it is true.

Here
f(m) = 12m -5
g(m) = m
m0 = 5/12
M = 12
therefore, O(g(x) = O(m)is the top estimate of the amount.
Simplified amount in big-oh bound
O (m)
source
share