This topic is 3734 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The purpose of this is confusing, especially since it involves a stack.

Stack as ADT has two operations, push and pop. Direct manipulation of stack elements doesn't exist at algorithmic level. Even more, your example doesn't even use a stack, since it appears to change object owned indices, not rearange the stack.

If you have a list or array of objects, and this list is strictly ordered, then whenever an object is changed, it needs to be relocated.

This would be essentially a form of partial insertion or bubble sort, given that each time the list changes, the order is fixed so that it strict order is established again.

The efficiency of reordering will depend, but the O complexity can be calculated, realistic efficiency will however depend on "Is Closer". Also, performance of algorithm itself if ran constantly will be similar to that of bubble sort, which isn't known for its speed.

What exactly are you trying to achieve?

Share this post


Link to post
Share on other sites
You're missing something obvious here.

You're keeping a whole list of objects, ordered by distance, while all you really need to know is which one object is the nearest. That list isn't needed at all. No sorting is required. The following (psuedo-code) would do the job just nicely:

closestObject = nothing yet
closestDistance = infinitely high

for each object in list:
if distance to object < closestDistance:
closestObject = object
closestDistance = distance to object

return closestObject

Share this post


Link to post
Share on other sites
Sign in to follow this