2014年3月22日星期六

Week 11 Sorting and efficency

Actually, I try to analyse some kind of sorting during last week slog...For this, its more likely to be a comparison.



The graph above is a brief summary to three sorting method, generally speaking, it seems that the least efficiency should be selection sort... 


According ti the code and example given during the lecture (http://www.cdf.toronto.edu/~heap/148/W14/Lectures/dustin/W10/sort.py), the result is 

Random list generated

Example code in python:
def select(L):
    """Produce copy of L in sorted order by selection sort
    """
    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

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 :
        left_sublist = L[:len(L)//2]
        right_sublist = L[len(L)//2:]
        return merge2(merge_sort(left_sublist), 

                      merge_sort(right_sublist))

 def quick(L):
    if len(L) > 1:
        pivot = L[0] (*)
        smaller_than_pivot = [x for x in L[1:] if x < pivot]
        larger_than_pivot = [x for x in L[1:] if x >= pivot]
        return quick(smaller_than_pivot) + [pivot] + quick(larger_than_pivot)
    else:
        return L


FOR 1000 ITEMS:

0.029001951217651367 - select sort
0.004999876022338867 - quick sort
0.10900616645812988 - quick sort on already-sorted list
0.008000850677490234 - merge sort
0.0010001659393310547 - built-in sort
0.0009999275207519531 - count_sort


This result gives a opposite conclusion...When I try more, the result varies

FOR 1000 ITEMS:
0.030001163482666016 - select sort
0.00400090217590332 - quick sort
0.11100602149963379 - quick sort on already-sorted list
0.009001016616821289 - merge sort
0.0 - built-in sort
0.0009999275207519531 - count_sort


However, if change the * to pivot = L[(len(L)//2)],  the efficiency of quick sort on already-sorted list has improved. This is because when choosing the middle one as the pivot for the sorted list, the elements before pivot are both smaller than pivot, there is no need for recursion.   
FOR 1000 ITEMS:
0.031002044677734375 - select sort
0.005000114440917969 - quick sort
0.00400090217590332 - quick sort on already-sorted list
0.00800013542175293 - merge sort
0.0 - built-in sort

0.0009999275207519531 - count_sort





  • Quicksort: 2nln(n) comparisons and 13nln(n) swaps on average
  • Mergesort: 1.44nln(n) comparisons, but up to 8.66nln(n) array accesses (mergesort is not swap based, so we cannot count that).
  • Insertionsort: 14n2 comparisons and 14n2 swaps on average.

It seems that  the type of input can change the efficiency. For example, an input in reverse-sorted order renders the quick sort very inefficient, whereas it may not have as big an impact on other sorts, which was another thing I had never know of from previous experience. In such cases, randomizing the input, or increasing the max-recursion limit can be very important. 







A website found useful: http://www.cs.princeton.edu/courses/archive/spr07/cos226/lectures/04MergeQuick.pdf

2014年3月16日星期日

WEEK N?

 Selection

Selection sort is probably one of the simplest and most intuitive approaches to sorting - you look through the the list (or in this case the deck of cards) to find the smallest element, and put that aside. Then you go through the rest of the deck and continue the process as many times as there were cards you started with and you end up with a sorted deck. In terms of code, a possible implementation is starting at the first (0th) element of an array, and switching it with the smallest value of the rest of the array. Then the second time through you can start on the second (1st) element and so on until you have gone through the array. 

If the size of the input (the number of elements) is n, then the first time through the list you have to check n - 1 elements. The second time, you ignore one element, so you only need to check n - 2 elements, and so on until you have 0 elements to check. So in relation to n, the number of operations you have to do is (n - 1) + (n - 2) + ... + 1 =  (n^2 + n) / 2, which means selection is O(n^2)

Recall from CSC108:
Bubble Sort:


Selection Sort:
The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
64 25 12 22 11

11 25 12 22 64

11 12 25 22 64

11 12 22 25 64

11 12 22 25 64

2014年3月7日星期五

Tree pre_order, in_order

Preorder is 
  1. Visit the root.
  2. Traverse the left subtree.
  3. File:Sorted binary tree preorder.svg
    Preorder
  4. Traverse the right subtree.

Inorder is 
  1. Traverse the left subtree.
  2. Visit root.
  3. Inorder
  4. Traverse the right subtree. 











In python, using recursive to travel around, code is:

def preorder(tl: 'TreeList') -> list:
    
    return [] if tl is None else ([tl[0]] + preorder(tl[1]) + preorder(tl[2]))


def inorder(tl: 'TreeList') -> list:
  
    return [] if tl is None else inorder(tl[1]) + [tl[0]] + inorder(tl[2]).

In the exercise 3, we are asked to return the tree according to the preorder list and inorder list given. Accroding to the characteristic of two Tree traversal, it can be known that the first element in preorder is the first root node. Use .find to find the index i for preorder[0] in inorder, inorder[0:i]is the left subclass and inorder[i+1:]is the right subclass. And then the second element in preorder will be another node  ...(using recursion)until all the element has been structed.