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 7629055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:44:16+00:00 2026-05-31T05:44:16+00:00

When I determine the complexity of a Java code like this , do I

  • 0

When I determine the complexity of a Java code like this , do I have to express that in a Theta or a big O notation?

List<Person> sortedPersons = new ArrayList<Person>();
List<Person> people = new ArrayList<Person>();

    for (int i = 0; i < people.size(); i++) {
        Person toadd = people.get(i);

        int index = 0;
        while(index < sortedPersons.size() && sortedPersons.get(index).compareTo(toadd) < 0)
            index++;

        sortedPersons.add(index, toadd);

    }

I know the for-loop is O(n) (or is it \Theta(n)?)
A get-operation runs in constant time, so O(1).
But what about the while loop?
sortedPersons.size(): O(1)
sortedPersons.get(): O(1)
Is the compareTo-operation linear?
And I think the add-operation also runs in constant time.
What’s the total complexity of the code?

  • 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-31T05:44:17+00:00Added an answer on May 31, 2026 at 5:44 am

    the code is O(n²)
    If you consider numbers and you sort them from small to large.

    • If your input is reverse sorted (from large to small), the code will be Θ(n).
    • If your input is already sorted, this code will be Θ(n²)

    The code is just a variant of Insertion sort, it just uses a list instead of an array.

    Consider this example for the complexity:

    The numbers are 1 2 3 4 5 and you want to sort from small to big.

    1. After first itteration your list will consist of 1. (this happens in o(1) ) and you won
      t visit the inner loop.
    2. In the second itteration you have a list of { 1 } since you are inserting 2 you ‘ll visit the while loop once, after it you ‘ll insert it.
    3. Third itteration the list is { 1 2 } you visit the while loop twice and insert 3 after it.
    4. …

    In the end you will have something like:
    0 1 2 3 4 time’s visited the inner loop.

    Now you can write 1 2 3 4 5 as (5(5+1))/2).

    Now you can write O(n(n+1) / 2 + n) as O(n²).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to determine time complexity of this code ? I guess that modPow method
I have a list of passwords that I need to examine and determine if
I have around 3500 flood control facilities that I would like to represent as
I have a list of favourite movies and I'd like to sort them according
Is there anyway to determine the Exceptions that can be thrown by methods in
How do you determine if all hash keys have some value ?
For my algorithm design class homework came this brain teaser: Given a list of
I have a VERY simple windows form that the user uses to manage Stores.
A client has asked me to reduce the complexity of Magento's …/template/checkout/cart/shipping.phtml so that
Given a snipplet of code, how will you determine the complexities in general. I

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.