caijunfu said:
Is there a way to create a new array, and initialise every of them to
0, but maintain the order to this process to O(1)?
(usually, we do initialisation by using for loop. that is of O(n).)
This doesn't make any sense. Note that O() notation implies the limit as
n --> infinity.
Other than magical initialization, or maybe something like flash RAM that
has the ability to clear blocks using special electronics, it wouldn't be
possible. But even for that, you would still have to initialize the
blocks sequentially, so it would still be O(n), but with a smaller constant.
Well, actually, you didn't say O(n) what. It is common to describe an
algorithm in terms of its space usage and time usage. There is often a
tradeoff where more using more space can speed up an algorithm.
There are also tricks on virtual storage OS where a program may allocate
large amounts of memory, ask the OS to clear it, but never use it. The OS
can map a single page or real memory to many pages of virtual memory, and
only allocate more real pages when they are modified. This effect has
confused some benchmark programs. Note that again it is O(n), but also with
a smaller constant.
In general O(n) is the best you can do for n things, the goal is to get as
close as you can to O(n).
-- glen