Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Compare 16 values to each other most efficiently

Reply
Thread Tools

Compare 16 values to each other most efficiently

 
 
Roger Twomey
Guest
Posts: n/a
 
      04-28-2004
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.


 
Reply With Quote
 
 
 
 
MattB
Guest
Posts: n/a
 
      04-28-2004
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

Roger Twomey wrote:
> 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.




 
Reply With Quote
 
 
 
 
Scott Mitchell [MVP]
Guest
Posts: n/a
 
      04-28-2004
> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the most efficient way to compare similar contents in two lists? Zachary Dziura Python 12 06-13-2011 09:33 PM
How to efficiently work with gettext PO files when making small editsto large text values python@bdurham.com Python 0 06-03-2010 01:00 PM
How Are Variables Kept Independent of Each Other Yet Pass Values? Mason Kelsey Ruby 12 09-12-2009 12:47 AM
Numarray - Using Integers Efficiently for Boolean Values andrewfelch@gmail.com Python 0 02-16-2006 06:19 PM
Efficiently Extracting Identical Values From A List/Array Adam Hartshorne C++ 7 02-21-2005 04:58 PM



Advertisments