3

With different values in a collection, will this algorithm (pseudeocode) ever terminate?

while (curElement != average(allElements)) { curElement = average(allElements); nextElement(); } 

Note that I'm assuming that we will re-start from the beginning if we're at the end of the array.

11
  • 2
    What does nextElement() do and is the allElements() collection constant? Commented Feb 10, 2012 at 9:48
  • how you are calculating average(allElements) Commented Feb 10, 2012 at 9:49
  • It depends on your values in that collection. average(allElements) will return fractional values or absolute? Commented Feb 10, 2012 at 9:49
  • @RoyDictus: It's supposed to be pseudocode. Just imagine that nextElement() moves the pointer curElement to the next element of the collection, or to the beginning if we're at the end. Commented Feb 10, 2012 at 9:51
  • @RoyDictus: Oh, and allElements will be changed when curElement is set. Commented Feb 10, 2012 at 9:51

2 Answers 2

5

Since this is pseudocode, a simple example with 2 elements will reveal that there are cases where the program won't terminate:

x = 0, y = 1; x y Step 1: 0.5 1 Step 2: 0.5 0.75 Step 3: 0.635 0.75 //and so one 

With some math involved, lim(x-y) = lim( 1 / 2^n )

So the numbers converge, but they're never equal.

However, if you'd actually implement this on a computer, they will turn out equal because of hardware limitations - not all numbers can be expressed in a limited number of bits.

Sign up to request clarification or add additional context in comments.

7 Comments

There exist arbitrary precision real-number implementations, which would allow for a real-life implementation to run forever.
@bdares I doubt that. They still store the number in memory, and as large as it gets, it's not infinite.
@LuchianGrigore: You can store the number in disk/remote cluster and expand it on the fly. Theoretically, it can be done.
Hmmm... good point. @amit any real-life storage is finite, so his argument still holds.
@bdares: (1) You can expand the cluster on the fly, so it is infinite. (2) You could say that the universe will collapse at some point in time, and there are finite number of atoms in the universe, so every implementation of any algorithm is O(1) space & time. But I don't think that's what the discussion is about.
|
2

It depends.

If your elements hold discrete values, then most likely they will fall into the same value after a few runs.

If your elements hold limited precision values (such as floats or doubles), then it will take longer, but finite time.

If your elements hold arbitrary precision values, then your algorithm may never finish. (If you count up every piece of an integral and add it to a figure you have on a piece of paper, you need infinite time, an infinitely large piece of paper, and infinite patience with this analogy.)

There is little difference between your code and the following:

var i = 1; while (i != 0) i = i / 2; 

Will it ever terminate? That really depends on the implementation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.