What is the complexity of the below program? I think it must be O(n), since there is a for loop that runs for n times.
It is a program to reverse the bits in a given integer.
unsigned int reverseBits(unsigned int num)
{
unsigned int NO_OF_BITS = sizeof(num) * 8;
unsigned int reverse_num = 0;
int i;
for (i = 0; i < NO_OF_BITS; i++)
{
if((num & (1 << i)))
reverse_num |= 1 << ((NO_OF_BITS - 1) - i);
}
return reverse_num;
}
What is the complexity of the above program and how? Someone said that the actual complexity is O(log n), but I can’t see why.
If n is the input number, then NO_OF_BITS is O(log n) (think about it: to represent a binary number n, you need about log2(n) bits).
EDIT: Let me clarify, in the light of other responses and comments.
First, let
nbe the input number (num). It’s important to clarify this because if we considernto beNO_OF_BITSinstead, we get a different answer!The algorithm is conceptually
O(log n). We need to reverse the bits ofn. There areO(log n)bits needed to represent the numbern, and reversing the bits involves a constant amount of work for each bit; hence the complexity isO(log n).Now, in reality, built-in types in C cannot represent integers of arbitrary size. In particular, in this implementation uses
unsigned intto represent the input, and this type is limited to a fixed number of bits (32 on most systems). Moreover, rather than just going through as many bits as necessary (from the lowest-order bit to the higher-order bit which is 1), this implementation chooses to go through all 32 bits. Since 32 is a constant, this implementation technically runs inO(1)time.Nonetheless, the algorithm in conceptually
O(log n), in the sense that if the input was2^5,5iterations would be sufficient, if the input was2^10,10iterations would be sufficient, and if there were no limit on the range of numbers anunsinged intwould represent and the input was2^1000, then1000iterations would be necessary.Under no circumstances is this algorithm
O(n)(unless we definento beNO_OF_BITS, in which case it is).