Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pop(i) is O(k) not O(n) #53

Open
irunestone opened this issue Feb 28, 2018 · 1 comment
Open

pop(i) is O(k) not O(n) #53

irunestone opened this issue Feb 28, 2018 · 1 comment

Comments

@irunestone
Copy link

Error reported in course FIT5211 on page Lists by user lwoo0004 [email protected]
Incorrect info on page ... pop(i) is O(k) not O(n).
e.g. popping from an indexed location from the end of a list

See python docs:
https://wiki.python.org/moin/TimeComplexity
Pop intermediate O(k)

And revised code:

popzero = timeit.Timer("x.pop(0)", "from main import x")
popend = timeit.Timer("x.pop()", "from main import x")
popend_minus = timeit.Timer("x.pop(-2)", "from main import x") . # ADDED

print("pop(0) pop() pop(-2)")
for i in range(1000000,100000001,1000000):
x = list(range(i))
pt = popend.timeit(number=1000)
x = list(range(i))
pz = popzero.timeit(number=1000)
x = list(range(i))
pe = popend_minus.timeit(number=1000)
print("%15.5f, %15.5f, %15.5f" %(pz,pt,pe))
pop(0) pop() pop(-2)
0.40309, 0.00010, 0.00015
1.20204, 0.00009, 0.00017
1.93542, 0.00009, 0.00014
2.73203, 0.00009, 0.00014
3.45721, 0.00010, 0.00015
4.21476, 0.00011, 0.00015
5.14111, 0.00010, 0.00017

@bnmnetp
Copy link
Member

bnmnetp commented Feb 28, 2018

The wiki is reporting Average times and Amortized times, not worst case times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants