Sunday, March 23, 2014

Week10 SLOG

This week we mostly talked about the assignment and big-oh notation. For the big-oh notation, we mostly talked about the performance of quick sort.
In the first trail for the code of quick sort, we chose the first element of the list to be the pivot. This way, the code first gives a runtime of n^2 for worst cases, and nlgn for average cases. However, when the list is already sorted, quick sort performs very bad and a sorted list of 1000 elements would raise RuntimeError. This problem occurs because python will have to create a stack for every element in the list. To solve the problem, it is very easy, as we can just use a random pivot in the list to break lists into two parts.
Another interesting part we talked about in class is the default value for a function. For example,
def f(n: int, L: list=[]) -> list:
    L.append(n)
    return L
f(7) -> [7]
f(17) -> [7, 17]
Python function creates the object to run(default value) and it runs as long as python can access the list(lists are mutable). If L were to be a tuple, an error would occur. L is a default value that is a part of the function f.

No comments:

Post a Comment