2013年12月6日星期五

[WEEK XII] Algorithms and Sortings

    This is the last week of the course and we are asked to write something about sorting algorithms, selection sort, quick sort, merge sort and their efficiency.

Sorting Algorithms
    Sorting algorithms is an algorithm that puts the elements of a list in a certain order. It has two basic rules that the result must be in non-decreasing order and it is a reordering or permutation of the original input. There are many popular sorting algorithms and we can roughly make four categories for it, which are simple sort, efficient sort, bubble sort and distribution sort.

Selection Sort
    Selection sort is simple but inefficient sorting algorithm. The algorithm make an addition empty list for the process. It looks for the largest or smallest item in the original list, then put it into the new empty list and find the next largest or smallest item in the remaining items in the original list. The new list is sorted, so when the algorithm is done, it returns the new list which has sorted data in it.

Efficiency: For selection sort it always performs О(n2).

Example code (from lecture notes):
def select(L):
    """Produce copy of L in sorted order

    >>> select([3, 1, 2])
    [1, 2, 3]
    """
    L1 = L[:]
    for i in range(len(L1)):
        m = min(L1[i:])
        j = L1.index(m,i)
        L1[i], L1[j] = L1[j], L1[i]
    return L1

Quick Sort
    Quick sort use the divide-and-conquer strategy as well as the merge sort. Its advantage is it does not use additional storage but the disadvantage is it has bad performance against the list that can't be divided into halves. It chooses an element from the list which is called the pivot value. It compares the value next to the pivot value until the pivot value has been placed to the correct position. Then it becomes a split point, and split the list into two parts, which are the left half and the right half. All the values in the left half are smaller than the pivot value and all the values in the right half are larger than the pivot value. After that, it continues divide the two halves by the recursively algorithm until the final result comes out.

Efficiency: O(n log n) for the worst case and  О(n2) for the best case.

Example code (from lecture notes)
def quick(L):
    if len(L) > 1 :
        return (quick([x for x in L[1:] if x < L[0]])
                + [L[0]] + quick([x for x in L[1:] if x >= L[0]]))
    else :
        return L

Merge Sort 
    Merge sort is an algorithm that improved by using divide-and-conquer strategy. It is a recursive algorithms and keep splitting the list into two halves. If the list is empty or has one item, it is sorted by the base case defined. If not, which the list has more than one item, it splits the list and recursively sort the both halves. In the end it combines the halves into a single and sorted list and that's why it is called the 'merge sort'. It is pretty efficient against very large lists.

Efficiency: O(n log n) in any case.

Example code (from lecture notes)
def merge(L1, L2):
    """return merge of L1 and L2

    >>> merge([1, 3, 5], [2, 4, 6])
    [1, 2, 3, 4, 5, 6]
    >>> merge([1, 2, 3], [0, 4, 5])
    [0, 1, 2, 3, 4, 5]
    >>> merge([0], [1, 2, 3, 4])
    [0, 1, 2, 3, 4]
    """
    L = []
    L1.reverse()
    L2.reverse()
    while L1 and L2:
        if L1[-1] < L2[-1]:
            L.append(L1.pop(-1))
        else:
            L.append(L2.pop(-1))
    L1.reverse()
    L2.reverse()
    return L + L1 + L2

def merge_sort(L):
    """Produce copy of L in non-decreasing order

    >>> merge_sort([1, 5, 3, 4, 2])
    [1, 2, 3, 4, 5]
    """
    if len(L) < 2 :
        return L[:]
    else :
        return merge(merge_sort(L[:len(L)//2]), merge_sort(L[len(L)//2:]))

2013年11月10日星期日

[WEEK X] Term Test II

Basically no class this week: No school on Monday and term test 2 on Wednesday.

Term test 2 was all about trees. BTNode, BSTNode, recursion, all such things. If I'm right, question one was asking to count the number of internal nodes and the number of leaves, the second one was asking to count the number of even integeers in a binary tree and the last one was asking to return a path to the minimum element in the BST.

 I think I'm doing fine on this test since some of the contents are covered in the lectures. The 'cheat sheet' is still a 'decoration' there though, I spent half an hour to prepare it but it seems have no actual use, lol. :/

2013年10月13日星期日

[WEEK V] Object-Oriented Programming and Recursion

    So, for this week we are asked to write two paragraphs about object-oriented programming and recursion, also explain its concept and usefulness.

Object-Oriented Programming

    I looked up on the internet about its information. Generally, object-oriented programming, which can be represented as OOP, is a programming model that 'objects' have data field and associated functions known as 'methods'. There are lots of OOP languages such as Python, Java and C++. An example for the OOP languages is they use 'class', which can be considered as a union of a certain type of information. They also use 'objects' and 'methods'. 'Objects' can be considered as a certain object belong or related to the defined class and 'methods' are under the classes and can be considered as the actions toward the defined objects by programmers. For instance, in Python, we could define a class 'Laptop' and an object under the class 'Laptop', 'Vaio', which means 'Vaio' is a 'Laptop'. In addition, we want to turn on the 'Vaio', so we define a method called 'Turn_on'. As the result, when we type  Vaio = Laptop/ Vaio.Turn_on() in the Python Shell, it means 'Turn on the laptop called Vaio'.

    It has valuable usefulness because programmers can assign value and data to any objects, and any defined objects can be used to process data and transfer data to other defined objects. It greatly enhanced the programs flexibility, compatibility and makes the program much more simple to maintain. This kind of language is widely used by software engineers, because it is easier to study and analyze the code for novices, also easier to design and maintain the program for experienced programmers compared to non-OOP language programs.

Recursion

    Recursion is a method of programming that divide a problem into two parts, which are basic cases and sub-problems. Basic case is 0 or empty list/string for the most of time and for the sub-problems, the function calls itself to repeat computing until the basic case occurs.  A failed recursion code can cause an infinite loop.

    Recursion is very useful as well since it eliminates the unnecessary code and increase the its efficiency. A complicated task can be easily done by the recursion by break it down to two parts, and for more complicated problems we can break down sub-problems into sub-sub-problems and so on to solve them nice and clean.

2013年10月6日星期日

[WEEK IV] Hello World!

    'Hello World!' Guess thats how computer programmers say hi, so I made this to my title.

    I would like to talk about something about myself here since it is my first SLOG here. I'm a second year student in U of T and I plan to take computer science specialist program. I love computer games (who doesn't? :3). When I was a kid about 3 or 4 years old I play Super Mario Bros. and Contra on NES which is an 8 bit video game console first released in 1983 (That is such a long game history you might want to say). Most of the games at that time were less than 1 MB and compared to nowadays games with a size more than 30 GB , they are pretty much'trash. However it was really fun to play them at that time.

    Back to my topic, in fact I was planning to study life science in university until the day I apply for university. I love computer games so much that I changed my mind to take computer science program in university, however I have literally no programming background at all and it gives me some real hard times last year. Good thing is things are going fine now, I have friends who study in computer science as well I can ask questions, my university is one of the best universities in the world, I have nice professors and TAs, can't complain any of it.

    Nothing much from the lecture this week, it is all about Stacks. It also involves orders, such as first-in-first-out and first-in-last-out, which are not hard as long as you understand the mechanisms.

    Peace.