solid
solid

Reputation: 387

How to setup the data structure?

First let me explain what I want to do. I have n data elements. Every element needs to be checked with every other element but not with itself. The function that checks the elements returns true if everything is ok. If something is wrong than the function deletes both elements and replace them with new ones. But the new ones need to be checked with every other element too. This will be repeated until every element was checked with every other and all checks are fine.

I am asking how to setup the data structure in a most efficient way. When I test ele1 with all other n-elements and all are fine and then I test ele2 with ele84 and both get replaced I need to check ele1 and ele2 again, if these are now not fine I need to check all for ele1 again. But how to remember in the most efficient way which elements need to be check and which don’t to avoid double checking of elements?

Upvotes: 0

Views: 41

Answers (1)

Zim-Zam O'Pootertoot
Zim-Zam O'Pootertoot

Reputation: 18148

You can use three lists: Main, CurrentNew, and NextNew

Main is initialized with the elements - use a nested loop to check them all, if you delete any elements then add the new elements to NextNew

On the next iteration, NextNew becomes CurrentNew - allocate a new NextNew list. First use a nested loop to check all elements of CurrentNew with the other elements of CurrentNew, then check the elements of Main with the elements of CurrentNew. New elements go to NextNew. Note that you're not checking the elements of Main with the other elements of Main - you already know that they're valid with each other.

On the next and each subsequent iteration, merge CurrentNew into Main, then NextNew becomes CurrentNew, repeat until all elements are valid.

Upvotes: 1

Related Questions