Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 543669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:34:29+00:00 2026-05-13T10:34:29+00:00

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

  • 0

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
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:34:30+00:00Added an answer on May 13, 2026 at 10:34 am

    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
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I followed the steps using this site : http://wiki.apache.org/nutch/RunNutchInEclipse I encountered a problem while
from this site there is header compress.h http://www.fredosaurus.com/notes-cpp/examples/compress/compress.html i am using visual c++ 2010
i am using the Toxi solution on this site for tagging bookmarks: http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html In
I'm using two functions one is from this site http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/ and its using function
I have been trying to parse the bottom table on this site using different
I am using this plugin for my new site: http://colorpowered.com/colorbox/ The page in concern
I'm creating new Site Definitions using this method: http://weblogs.asp.net/paulballard/archive/2007/04/09/creating-a-custom-sharepoint-2007-portal-site-definition-using-the-portalprovisioningprovider-class.aspx and when they get created,
This is my first time using this site and I am quite new to
Please help. I am obviously no expert but using suggestions from this site, I
I'm using this code to allow only my ip to access the site <IfModule

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.