tags:

views:

118

answers:

1

i am using this site as a resource http://www.perlmonks.org/?node_id=573138

I am trying to understand about O notation and it gives two examples of searching two arrays for the same element. First example has O(n^2) as does the second, however the second has an enhancement to it so it runs quicker but still maintains the same O notation, I will paste the code samples below. What I would like to know is how they work, I have limited programming knowledge and am most comfortable in java, i can understand the first I think, just two for loops and check, something like;

for (int i = 0; i < arrarysize ; i++){
    for (int j = 0; j < arraysize; j++){
        if(getElementFromArray(i).equals(getElementFromArray(j))){
            //do something
        }
    }
}

but how the second works is beyond me, i just don't get the "enhancement"

for my $i (0 .. $#array) {
    for my $j (0 .. $#array) {
        next if $j == $i;
        # Compare $i, $j
    }
}

for my $i (0 .. $#array - 1) {
    for my $j ($i + 1 .. $#array) {
        # Compare $i, $j
    }
}
+6  A: 

Think of it in terms of a rectangle of possible (i, j) values. The first loop compares every pair - so it compares (5, 0) and later compares (0, 5) which will obviously just give the opposite result.

The second loop divides that rectangle in half - basically it only checks one "triangle" of it - every value where j > i so it would check (0, 5) but not (5, 0). This avoids redundancy - but it just means that it's checking n*(n-1)/2 values instead of n^2 values - it's still O(n^2).

The second loop in Java would be:

for (int i = 0; i < arraysize - 1; i++) {
    for (int j = i + 1; j < arraysize; j++){
        if(i == j) {
            //do something
        }
    }
}
Jon Skeet
the if in the loop is weird. I would expect one to compare against the value IN the array, and not the indexvalues
Toad
+1 for the explanation, but `i == j` can't be the right test in this context, it would always yield `false`...
hjhill
ah.....I see now....you copied the example given by the question asker. It's clear that there too a fault is made. The perl example is obviously correct.
Toad
is there maybe another, more simple example for enhancement - where it maintains the same o notation you could provide? if not then never mind, thanks everyone
tommy
What do you mean by "where it maintains the same o notation"? Trying to find duplicate elements in an array is possibly best done with a hash, but that's a somewhat different approach...
Jon Skeet
well - i am trying to find limitations of o notations, i was wondering if there was a simple example, ie) not with coordniates that demonstrates an enahncement to a task, where version 1 is the same o notation as version 2, yet version 2 works more efficiently
tommy
That's fairly common. Asymptotic notation hides constants. Rob Pike wrote, “Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don't get fancy. (Even if n does get big, use Rule 2 first.) For example, binary trees are always faster than splay trees for workaday problems.” http://www.quut.com/c/pikestyle.html
Greg Bacon