+ 2

How to find complexity of my code

It finds the Nth maximum of a list , and also takes key as optional argument(ex, which is useful to find 3rd maximum odd number in a list) 1. What is time complexity of my code Can you improve it... https://code.sololearn.com/c4c3iZpb9bnJ/?ref=app

1st Oct 2021, 11:50 AM
Prabhas Koya
5 odpowiedzi
+ 2
You could either analyse your code manually here is detailed explanation of how to do so: https://yourbasic.org/algorithms/time-complexity-explained/ Or you could make python calculate it for you, using library like big-O or Wily (but those are not standard and therefore doesn't work in sololearn)
1st Oct 2021, 12:02 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
I'm not 100% sure to be honest, but Im quite sure time complexity of your max function is O(n^2 log(n)) Because there is a sort() method with time complexity of O(n log(n) ) nested inside a loop.
1st Oct 2021, 12:11 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Prabhas Koya , hmm Seriously? That's why you posted this question? To get people check out your code? I mean common.... And this code is just a common max function so really it's not too useful... There are hundreds of thousands of such codes on the Internet... I would rate the code about 5/10, mostly because its not very readable...
1st Oct 2021, 12:16 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Thanks I asked for time complexity only, and again thought of asking a legend(Aleksei Radchenkov ), about my code,by the way I am beginner
1st Oct 2021, 12:21 PM
Prabhas Koya
+ 2
My code is improved by Brian thanks to him
1st Oct 2021, 2:39 PM
Prabhas Koya