Python Forum
Big O runtime nested for loop and append
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Big O runtime nested for loop and append
#1
Hello, after an argumant I would like to get answer from people who know about that bigO

What is the runtime big O for this code?

I think this is n^2 but someone said its n^3 what is the big o for this code?

Thank you.

CODE:
BigO_2(n):
L = []
for i in range(n):
    for j in range(n):
        L.append(i*j)
for i in range(n):
IDK what is that VV
def 
   
   
Reply
#2
Post working code,now is very far from that.
To make it work and the last loop dos not make sense or work.
def BigO_2(n):
    L = []
    for i in range(n):
        for j in range(n):
            L.append(i * j)
    return L

result = BigO_2(10)
>>> len(result)
100
So for this code is the time complexity is O(n^2).
So it give 100 as input the len() should increase quadratic to 10000.
# result = BigO_2(100)
>>> len(result)
10000
Calculate n^2 in Python would be like this.
>>> 10 ** 2
100
>>> 100 ** 2
10000
Reply
#3
If you really want to be a stickler, you could say is O(n^2 + n). Normally you don't worry about the O(n) part.
Reply
#4
(Dec-23-2022, 06:54 PM)deanhystad Wrote: If you really want to be a stickler, you could say is O(n^2 + n). Normally you don't worry about the O(n) part.
To be an even more stickler, O(n^2 + n) and O(n^2) are exactly the same thing mathematically speaking. There is no difference at all.
Reply
#5
The code you posted has a runtime of O(1) because it is a SyntaxError at compile-time, so there is no runtime behaviour Big Grin

list.append has amortised O(1) behaviour. Which means that if you start with a list and do lots and lots and lots of appends, on average each append will take the same amount of time. So we can treat the appends as a constant time operation, just like the i * j multiplication.

(Note: this is only true if i and j are relatively small, say less than 20 or 30 digits. For huge ints with hundreds of digits, multiplication takes time that depends on the number of digits.)

Your code loops N times for each value in range(N), so altogether it does N*N loops. That's quadratic O(N^2) behaviour, not cubic O(N^3).

Whoever told you this was cubic behaviour probably thinks that appending to a list is O(N). It isn't. However, inserting into the start or middle of a list can be. So if you change the code to do L.insert(0, i*j) instead of an append, you will probably see roughly cubic behaviour.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class and runtime akbarza 4 353 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  painfully slow runtime when handling data dadazhu 3 960 Jan-20-2023, 07:11 PM
Last Post: snippsat
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,572 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,010 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,217 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  How do I add another loop to my nested loop greenpine 11 4,540 Jan-12-2021, 04:41 PM
Last Post: greenpine
  Error on nested loop : Invalid syntax dvazquezgu 3 3,229 Nov-25-2020, 10:04 AM
Last Post: palladium
  Nested loop indexing Morte 4 3,895 Aug-04-2020, 07:24 AM
Last Post: Morte
  Append list into list within a for loop rama27 2 2,374 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  Nested for loop not looping puttingwordstogether 0 1,706 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020