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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:54:40+00:00 2026-06-17T15:54:40+00:00

I have to calculate the time complexity or theoretical running time of an algorithm

  • 0

I have to calculate the time complexity or theoretical running time of an algorithm (given the psuedocode), line by line as T(n). I’ve given it a try, but there are a couple things confusing me. For example, what is the time complexity for an “if” statement? And how do I deal with nested loops? The code is below along with my attempt which is commented.

length[A] = n

    for i = 0 to length[A] - 1    // n - 1 
      k = i + 1                   // n - 2
      for j = 1 + 2 to length[A]  // (n - 1)(n - 3)
        if A[k] > A[j]            // 1(n - 1)(n - 3)
          k = j                   // 1(n - 1)(n - 3)
      if k != i + 1               // 1(n - 1)
        temp = A[i + 1]           // 1(n - 1)
        A[i + 1] = A[k]           // 1(n - 1)
        A[k] = temp               // 1(n - 1)
  • 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-17T15:54:40+00:00Added an answer on June 17, 2026 at 3:54 pm

    Blender is right, the result is O(n^2): two nested loops that each have an iteration count dependent on n.

    A longer explanation:

    The if, in this case, does not really matter: Since O-notation only looks at the worst-case execution time of an algorithm, you’d simply choose the execution path that’s worse for the overall execution time. Since, in your example, both execution paths (k != i+ 1 is true or false) have no further implication for the runtime, you can disregard it. If there were a third nested loop, also running to n, inside the if, you’d end up with O(n^3).

    A line-by-line overview:

    for i = 0 to length[A] - 1    // n + 1 [1]
      k = i + 1                   // n
      for j = 1 + 2 to length[A]  // (n)(n - 3 + 1) [1]
        if A[k] > A[j]            // (n)(n - 3)
          k = j                   // (n)(n - 3)*x [2]
      if k != i + 1               // n
        temp = A[i + 1]           // n*y [2]
        A[i + 1] = A[k]           // n*y
        A[k] = temp               // n*y
    

    [1] The for loop statement will be executed n+1 times with the following values for i: 0 (true, continue loop), 1 (true, continue loop), …, length[A] - 1 (true, continue loop), length[A] (false, break loop)

    [2] Without knowing the data, you have to guess how often the if‘s condition is true. This guess can be done mathematically by introducing a variable 0 <= x <= 1. This is in line with what I said before: x is independent of n and therefore influences the overall runtime complexity only as a constant factor: you need to take a look at the execution paths .

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

Sidebar

Related Questions

I have two fields which calculate a total, but I have a radio button
I have a method to calculate the exponent, but it doesnt like the c
Suppose I have, Heap Sort method which its complexity time is O(nlogn). When I
I have to support IE6 and I calculate rendering time by creating a timestamp
I have login and logout events and i need to calculate time between them.
I have a table with column StartDate, I want to calculate the time difference
I have a question to find a complexity class estimate of a algorithm. The
I was interested in knowing how to calculate the time and space complexity of
I have to calculate time difference in hours, minutes and seconds and update time
I have created a time function to calculate time as media is playing. It

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.