简体   繁体   中英

Time complexity of Math.abs in Java?

For an assignment I'm doing, I need to make sure I have an algorithm running in O(n) time, and I'm using the Math.abs() function inside some loops, so I'm wondering if Math.abs() runs in O(1) time?

I would think that it would, but I can't find an answer to this anywhere. Just want to make sure I'm not accidentally making an O(n 2 ) algorithm without knowing it.

The Math.abs implementation:

public static int abs(int a) {
    return (a < 0) ? -a : a;
}

This will be O(1) time complexity, since the operation itself is constant time and the input is fixed. Regardless of the input, the operation will always take the save time.

Loop: O(n): Time Complexity of a loop is considered as O(n) if the loop variables is incremented / decremented by a constant amount. For example following functions have O(n) time complexity.

   // Here c is a positive integer constant   
   for (int i = 1; i <= n; i += c) {  
        // some O(1) expressions
   }

   for (int i = n; i > 0; i -= c) {
        // some O(1) expressions
   }

That depends on what data type you performing the abs function:

  1. IEEE754 floating point

    abs means just clearing of MSB bit which holds the mantissa sign bit. This is unconditional bit operation on single well placed bit so it is O(1) . Here C++ example (sorry not a JAVA programmer)

     float x; // 32bit variable to perform abs on int *p=(int*)&x; // this just makes p pointer pointing to x p[0]&=0x7FFFFFFF; // clear highest bit by AND 
  2. non 2'os complement signed integer types

    These have separate sign bit just like previous bullet so all the stuff from #1 applies for these too.

     int x; // 32bit variable to perform abs on x&=0x7FFFFFFF; // clear highest bit by AND 
  3. 2'os complement signed integer types

    abs for these means negate the sign if MSB is set. That means you need to negate all bits an increment.

     int x; // 32bit variable to perform abs on if (int(x&0x80000000)!=0) // negative means MSB is set { x^=0xFFFFFFFF; // negate all bits x++; // increment } 

    This can be done also brunch less. Anyway for basic data types is this still O(1) . But if you start using bigint or bigdecimal then both negation of all bits and incrementation will become O(n) where n is the number of "digits" used to form the number. By the "Digit" I mean what base is used to internally store the number. usually it is 2^32 or 2^64 word or highest power of 10 that can be fit inside such number. That is why is usually better not to use 2'os complement for big numbers ... (it complicates thing not just abs operation).

Conclusion

On basic data types you can safely assume abs is O(1) and also most HW architectures support this operation as an atomic instruction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM