Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Saulo Dias
Saulo Dias

Posted on

     

Time complexity of two by two comparison

You have probably been faced with a very simple and common problem: comparing unique elements in a list, two by two, when the order doesn't matter.

Comparingn elements withn elements is going to have a complexity of. But when the order doesn't matter, comparing element A to B is the same thing as comparting B to A, so the complexity is going to be less than.

For this algorithm all we have to do is to implement a for loop that increments the initial position each time we finish comparing to the remaining items in the list.

But the question remains: How faster is it going to be in theory?

I didn't know the complexity of this second approach, soI googled it and it turns out that it isn(n-1)/2, where n is the number of elements. Now I wanted to know how faster it was going to be relative to.

So all I had to do was to divide both complexities to see how they grow relative to each other. See the graph below:

It turns out that whenn goes to infinite, this value goes to 2. So that means that the second approach is about two times faster.

Now it is obvious to me that it is 2 times faster. I mean, each comparison should appear twice. For example, we have the elements A, B, and C so we would have AB, AC, BA, BC, CA, CB. Half of those are the same comparison when the order doesn't matter.

In this case if I had been smarter I wouldn't have needed to do any calculations. However, this very same exercise of comparing different algorithmic complexities can be useful for you when your theoretical gains are not that obvious, and this information might help you decide if the complexity of the implementation outweighs the gains in processing time.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer graduated in Automation and Control Engineering trying way too hard to become a brogrammer.
  • Location
    Rio de Janeiro, Brazil
  • Education
    Automation and Control Engineering at CEFET/RJ
  • Work
    Solutions Developer
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp