Python Forum
[split] Time Complexity of Counting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Time Complexity of Counting
#6
Thanks for the plug, Mek.

@wavic:
To dig into it a bit more, the code
for s, i in enumerate(l, 1):
    l[:s]
is quadratic (O(n**2)) because slicing is a linear operation. During the first iteration, n work is done, during the second is (n-1) etc. down to 1. On average, n/2 work is done within the loop, but the halving doesn't matter.

Given, f(x)=x*x/2, doubling x results in a more than doubling of f(x), as shown below
f(2) = 2
f(4) = 8
f(8) = 32

The reason Mek's solution is O(n) is that within the loop (which runs n times), only constant-time operations are taken. Operating on a dictionary is efficient (O(1)) so long as you're not doing other linear operations, like collection.count().
Reply


Messages In This Thread
[split] Time Complexity of Counting - by Mekire - Jan-18-2017, 07:52 PM
RE: [split] Time Complexity of Counting - by micseydel - Jan-18-2017, 09:32 PM
RE: [split] Time Complexity of Counting - by wavic - Jan-18-2017, 10:48 PM
RE: [split] Time Complexity of Counting - by buran - Jan-10-2019, 10:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  counting lines in split data Skaperen 6 1,451 Oct-07-2022, 07:09 PM
Last Post: Skaperen
  Split gps files based on time (text splitting) dervast 0 1,909 Nov-09-2020, 09:19 AM
Last Post: dervast
  What is the run time complexity of this code and please explain? samlee916 2 2,322 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  Basic Time Complexity Help Adakip 1 1,857 Aug-25-2019, 09:12 AM
Last Post: ThomasL
  Time complexity of dict vs attributes heras 6 3,826 Aug-24-2018, 10:18 PM
Last Post: Gribouillis
  How to calculate the mccabe complexity in module falke8? fuqianz 1 3,147 Oct-16-2017, 02:08 AM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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