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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:06:49+00:00 2026-06-03T01:06:49+00:00

Say I wanted to perform complexity analysis from first principles on this simple loop

  • 0

Say I wanted to perform complexity analysis from first principles on this simple loop –

for (int i = 0; i < n; i++)
{
    a = i + 1;
}

Here is what I have done, is this the correct procedure or am I way off?

Initial assignment of 0 to i: 1 operation

Loop executed n times:

  • Comparison of i to n: performed n+1 times
  • Increment i: operation performed n times
  • Assignment of i +1 to a: two operations performed n times

So total number of operations: 1 + (n+1) + n + 2n = 4n + 2
And this has Big Oh(n) complexity.

Is this correct? Is there a better way of doing it?

  • 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-06-03T01:06:51+00:00Added an answer on June 3, 2026 at 1:06 am

    The final conclusion is correct, the algorithm is O(n)

    However, when analyzing algorithms we usually avoid counting exactly how many ops are done, and look only for upper and lower bounds, since the exact details might be implementation dependent.

    For example, in your code – loop-unrolling might decrease the number of compare ops in the code, and the exact number of ops you calculated is no exactly the number of ops done in practice.

    Also, this assumes a is an int or some primitive type, and the operator=, operator+ are done in constant time. If a is for example some kind of big-integer, or string like, and you overloaded the operators – maybe each operator= is O(|a|), which makes the algorithm O(nlogn), or O(n^2), (or something else), depending on the specific implementation of the type of a.

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

Sidebar

Related Questions

Say I wanted to have the first six characters in black and the next
Say I wanted to have one variable in a class always be in some
Let's say you wanted to implement a breadth-first search of a binary tree recursively
Say I wanted to print a ÿ (latin small y with diaeresis) from its
i'm wanted to perform some ajax calls in a certain way. I have a
Let's say I want to have a function which reads data from the SerialPort
Say I wanted to have a project, and one-to-many with to-do items, and wanted
so lets say I wanted to essentially do this: $.post( 'search_item.php', { serialzed_data, save:
Say I wanted to have a worker thread that has slots for signals emmited
Say I wanted to perform an update in a database that held information on

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.