This
week we finally learned the python convention of information hiding, even
though it is still not strongly enforced since it is only a "convention",
unlike in Java the private keyword will ban the users from accessing certain
data. I was shocked the first day of class by how casual and frivolous python's
format and rules are. There are no braces, no variable definition. Everything
is dynamic and defined at run-time. However, I gradually got used to it and the
convenience it brings. I feel like python is way more user-friendly with its
less rigid rules and more comprehensive library and it feels great to just code
for fun. But still I heard that in the later years we are going to focus on
java to make some serious programs with copyright. Each language has its
merits, and I like all of them. Python trusts the users to be professional programmers
who wouldn't be meddling around with unauthorized data, so it regulates
legitimate programming to the specific field, which is suitable for our use
since we are all programmers-to-be that would follow the rules. We also learned
about the __eq__ and the __mul__ function and how you could override it to your
own implementation, which is similar to the str and the __repr__ functions. I
think I've learned a similar method to __eq__ in java but I can't remember it
now. I do remember the toString() method in java which works the same way as
str in python. We also learned about the reduce function which is something
I've never heard before but is yet another one of those convenient and
logically clear functions introduced in python. I've never thought of
multiplication as reducing several numbers to a single number. That logic is
intriguing to me. There is no homework this week. The lab this week is not
really related to coding it just asks us to graph the efficiencies of all the
sorting algorithms and do the same if the list is already sorted or the reverse
of a sorted one. The cprofile was really useful in that it maps out the process
of the algorithm and number of calls on the way so that in the future we can improve
our code using this and fixing the areas where there are too many unnecessary
calls. I could answer all of the questions posed in the lab such as why
mergesort and quicksort would be faster if you directly change the list
parameter. It is because the program does not need to spend time copying the
list into another one. There was one result that did bother me. It was that when
I tested the reverse case bubblesort being a worst case of n^2 the same as
selectionsort, took 10 seconds while selection took around 6 I think. I was
very confused of why this would happen. I thought bubblesort was a pretty
efficient algorithm. Then I thought for a while and remembered that the reason
why mergesort and quicksort are slowing if they need to copy is because of the
number of calls, while the big-O is obviously still nlogn no matter you copy it
or not. So this time tells you the time spent on calls of all kinds of methods,
while the number of iterations needed for sorting is still the same. Therefore
I thought to myself why bubblesort has too many calls. Then I realized it is
because it swaps every single time (which is a call) it finds next element
smaller than the element in question while selectionsort finds the max which is
one call if using built-in python one and simply puts it at the back which is
another. Even though the finding max part could use a lot of calls such as
assigning a different value it is definitely more efficient than swapping every
single time with changing two variables. In the end their big-Os are still n^2
but bubblesort takes longer.
I
agree with the author of http://cf148.blogspot.ca/ that the big-O of an
algorithm is very hard to determine since it ties in the knowledge of limits
which we learn in calculus. For example selection sort should have an order of
n(n-1)/2 but in reality it is n^2 since when n gets large n-1 is n and the
division by 2 doesn't make much of a difference in the face of infinity so we
can ignore it. The other big-Os are even more related to intuition so it is
pretty confusing at times.
No comments:
Post a Comment