Compare 16 values to each other most efficiently

R

Roger Twomey

I have 16 string values that I need to compare to each other. No two are
supposed to hold the same value (if they do something is amiss and I need to
abort the operation).

Short of 'IF Then' comparisons for every possible combination (do-able but
very cludgy) is there not a better way to code this?

Any idea will be considerted.

Thanks.
 
M

MattB

I'd put them in an array and loop through with each value comparing it to
the rest (with logic to only make comparisons that haven't been made yet if
you want the most efficent way).

Matt
 
S

Scott Mitchell [MVP]

I'd put them in an array and loop through with each value comparing it to
the rest (with logic to only make comparisons that haven't been made yet if
you want the most efficent way).

Matt, this approach is not the most efficient one. Consider that you
have n strings you want to compare. In the worst case you'll have to
compare the first string with n-1 other strings, then the second one
with n-2 other strings, then the third with n-3, and so on. The total
number of comparisons you'll have to do will be:

(n-1) + (n-2) + ... + 1

which is, in closed form, (n^2 - n)/2 comparisons.

Now, if you first *sort* the strings using, say, quicksort, this will
take n * log(n) comparisons. Once the strings were sorted, you could
then walk over the string once, seeing if any matched up (by just
comparing one with another). The asymptotic running time of this second
approach would be n * log(n) + n, which beats (n^2 - n)/2 for
sufficiently large n.


--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top