Complexity analysis has little to do with what value ends up in a variable, it a property of an algorithm, simplistically (for time complexity) how many steps it needs to do based on some input value.
In this case, given the input value k
, your complexity is O(log N)
because the division by two "halves the remaining solution space" on each iteration, along the lines of:
i = 128 64 32 16 8 4 2 1
The growth of the variable x
which, as mentioned, has nothing to do with the algorithmic complexity, is to double each time through the loop.
So, you have a loop controlled by a value that halves on each iteration. And you have a variable that doubles on each iteration:
i = 128 64 32 16 8 4 2 1
x = 1 2 4 8 16 32 64 128
Hence it makes sense that the relationship between the input value and the final variable value will be a linear one, as shown by the equivalent Python code:
ctrl = 1024
other = 1
while ctrl > 0:
print('{0:4d} {1:4d}'.format(ctrl, other))
ctrl = ctrl // 2
other = other * 2
which outputs:
1024 1
512 2
256 4
128 8
64 16
32 32
16 64
8 128
4 256
2 512
1 1024
Note there that, though the final value in other
is 1024
, only ten "steps" were executed, since log21024
is 10
.