2

In the definition of in-place algorithm, from Wiki, " However, this form is very limited as simply having an index to a length n array requires O(log n) bits." How does an array index requires log n bits? For example, A[0], 0 here just take O(1) bits, right? It is just 1 number.

I guess my question is about how an array establish its index. An array of size n has n space for its elements, and n space for storing the index numbers 0,1,2... I assume I am wrong, but I don't know how.

wangge
  • 123
  • 3

1 Answers1

5

If you have a variable $x$ that can represent any number from $0$ to $n-1$ (or any value from a set of of $n$ possible values, really), such an the index of an array of $A[0 \dots n-1]$ of $n$ elements, then $x$ needs to use at least $\lceil \log_2 n \rceil = \Omega(\log n)$ bits.

This is because there are $2^b$ combinations for the values of $b$ bits. Then, to represent $n$ values you need to have $2^b \ge n$, i.e., $b \ge \log_2 n$. You can add the ceiling since $b$ must be an integer.

When you say "an array of $n$ elements uses $O(n)$ space" you are probably thinking of a word-RAM model. Here the memory is split into addressable words, i.e., groups of $w$ bits. Each entry in the array occupies one (or a constant number of) words of memory (this also means that the elements in the array can't represent more than $2^{\Theta(w)}$ values each).

Usually the word size $w$ is chosen to be $\Theta(\log n)$, where $n$ is some natural parameter of the input problem (e.g., the size of the instance). In this case an index of an array of $n$ elements can be stored into a single memory word, i.e., in $O(1)$ space.

However this is not contradicting the above lower bound of $\lceil \log_2 n \rceil$ bits, it's just that these bits are all packed into a single (or into a constant number of) memory word(s).

Steven
  • 29,724
  • 2
  • 29
  • 49