Building summation from code analysis?

I have an average deadline tomorrow, and we need to analyze the code. This, frankly, is the only thing I don’t understand at all. Basically we are given something like this:

Consider this Java method for comparing two strings. This method has two inputs, so we must use two variables to express the runtime. Let be the nlength a, and let be the mlength b. We can express the operating time as a function nand / or m. This will help if we accept, without loss of generality, that n < m.

  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; 
 }

a) Express the worst run time of this method as a sum.

b) Simplify this amount and indicate its large-edge.

The designation I need is:

n
Ξ£ i
i= 1

, . . , . .

+4
3

, (i), , (, , n).

, n , " " , , "if", , "2n + 2". Big-O, O(n). , , .

, , , :

n
Ξ£ k
i = 1

" 1 n i, k ", k * n ( O O (n)). k , , . , 1 " ", .

+1

- O (n).

?
1. , n < m, , [ ].

, , , .

  1. , while .

, n .

, a b , a [0] == b [0] .

-

  • while
  • ..
  • i

nc + k, c - while, k - , (, ), n .

n , , O (n).

+1

, . 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 .

  • ( )
  • 2
  • .

, 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()//1
  • i < 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)

g (x) is bounded by f (x)

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

enter image description here

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)

+1
source

All Articles