Sunday, 30 March 2014

Sorting and Efficiency

     When writing code, there are many factors to consider about the data one is working with. One important thing to consider is whether the order matters. When storing a list of items, it would seem that the most efficient data type to use would be pythons builtin type list. If however, one wants specific keys instead of just indexes, a dictionary comes in handy. This is also an efficient way to store data except the items are unordered. This is where sorting comes in.

     There are lots of times when one needs to sort the data in a list in order for the data to be useful. Pythons builtin sort, Timsort, is the most efficient sorting algorithm. It is a hybrid of merge sort and quick sort, and is O(n logn). This seems to be the optimal efficiency of a sorting algorithm and is often achieved by repeatedly splitting the list into half, causing the logn, and each individual step being linear(causing n). This type of behaviour is displayed in both quick and merge sort, except merge sort is less efficient than quick sort because it takes extra storage to split the lists and then merge them together.

     There are many different sorting algorithms used in python, some better than others. The worst efficiency to have is O(n!), followed by O(n^n). The hierarchy as listed in lecture is as follows:


O(1) <= O(lgn) <= O(n) <= O(n^2) <= O(n^3) <= O(2^n) <= O(n^n)

    The worst sorting algorithm (in terms of efficiency) we've studied is bubble sort. It is O(n^2). Bubble sort starts at the beginning of the list and bubbles the item up the list until it reaches an item bigger than it. Eventually it bubbles the largest item to the end of the list and the list is sorted. Although selection and insertion sort are a little bit faster, they are also O(n^2) and have algorithms that involve iterating through n-1 items and making n comparisons. However, this varies for some lists. For example,  since there are less exchanges in selection sort, in the list below, bubble sort makes 20 exchanges and selection only makes 8 (http://interactivepython.org/courselib/static/pythonds/SortSearch/sorting.html)

[54,26,93,17,77,31,44,55,20]

The other approach to sorting is often called "divide and conquer". This involves repeatedly dividing the list into half (log n splits) and sorting it through either recursively merging the sorted lists back together (merge sort) or using a pivot and partition process (quick sort). These sorting algorithms perform at O(n logn) even for large values of n, but are limited to n <1000 because of pythons maximum recursion depth (which is 1000). The most efficient sort, as stated at the beginning, is Timsort.

If theres one thing I've learned from this course, its to always stick to the builtins. Even if you think you can write something that works really fast and it seems to work, chances are there is something you didn't consider and your code is too specialized. This is definitely applicable to sorting algorithms, as pythons sort is both efficient and general enough because it works well in all cases. Although understanding the behaviour of sorting algorithms and how to achieve n logn was difficult at first, once I learned that the divide and conquer method uses ln n approach, it became simpler to understand. Sorting is a very important thing to understand when using python, and can be much more complex than someone who doesn't study computer science would expect.

Thursday, 20 March 2014

Epiphany!

As I was going through the slogs of some of my classmates, I realized I'm not the only person who feels this way:

A lot of times with complex examples of recursion I will end up writing out the whole code, testing it, and not even fully understand what I've written until after the fact. Not necessarily through trial and error, but just by writing code for less complex cases based on what I think works, and the realizing it works for examples that are more complex than I can even process. I often find myself having to correct my expected output in docstrings for complicated test cases. As phrased ever so accurately on c3reiner's slog: http://c3reiner.blogspot.ca/  under the post about recursion woes: "it's interesting to see that something I programmed seems to understand how to solve the problem better than I do."

A2

This post is not really about any theories or coding methods we learned in class, its mostly about the stress of assignment 2. Firstly when going through part 1, I clearly read the instructions wrong and feverishly began developing a series of classes whos __eq__ methods were for the string matching of regexes. After realizing my mistake, I was able to finish part 1, just in time for the extended deadline. Then I was greeted with exercise 3, which was also no walk in the park. Finally I was able to create a successful submission. Then, bombarded with midterms and other work I began to tackle part 2. After being stuck on is_regex for a couple of days, I rediscovered the magic of helper functions.

The take home message from my struggle through this assignment was that it really helps to write things out and break them up into parts before writing any code. Sometimes thinking about the task at hand before coding seems too abstract so what I like to do is start writing the code and think through comments. And then once I get stuck thats when I pull out the paper and pen and really break down what the problem is, and why I can't get to where I want to be. Thats how I tackled finding the index of the root of regex strings. Once I realized I had no idea how to do this on my own, off to the help centre I went! haha

Saturday, 8 March 2014

LinkedList

Linked lists can be thought about in two ways, as a list of an item and the rest of the list, or as objects which reference the next object in the list. They introduce a new way of creating, viewing, and mutating a list. For example in a traditional list you can use methods such as pop or append mutate the end of the list. For linked lists however, theres no pop function and one can't simply get rid of an item. In a method called "delete" from lecture, we discovered how to do something similar to pop. When comparing linked lists with a python list, the delete method is more like reassigning a list. For example:

>>> lst = [1, 2, 3]
>>> lst
[1, 2, 3]
>>> lst = lst[ 1:]
>>> lst
[2, 3]

Since we named both lists "lst", lst now points to the new list [2, 3]

In a similar way, to delete the first item of  a LinkedList, we make its attribute front point to the second item instead of the first item, and make size = size - 1. so similar to the new lst, we don't know where the first item went, but it doesn't matter.

So far, linked lists are interesting but I think they are overcomplicated lists and can't see the benefit of them yet. Hopefully after working with them more my opinion will change.