I am trying to find the time complexity for this 2 methods

Viewed 34
def mysort1(x):
    y = list(x)
    y.sort() 
    z = [0]*len(y)
    for i in range(0, len(y)):
        z[i] = y[i]
    return z

def mysort2(x):
    y = list(x)
    y.sort()
    z = []
    for i in range(0, len(y)):
        z.insert(0,y[len(y)-i-1])
    return z

I think both of them have a linear time complexity, considering the loop runs n times in both the methods. What do you think is the time complexity of the code blocks given below.

1 Answers

In my point of view, time complexity of your algorithm is O(nlog(n)) because time complexity of array sorting is O(nlog(n)).

Related