Thursday, March 31, 2016

Week 11 Algorithm on Linked-list, Recursion and Tree


Today, I want to analyze the algorithm of previous ADT or functions.
1) Linked-list:
Linked-list acts like list. It has the property that we can simply remove or add items at any position without moving other items.
Therefore, doing sorting may cost less time or work. For example, if we apply the bubble sort on the Linked-list, compare with the case on the list, we don't need to move other items when we put the max or min at the first position. However, the Big-O of bubble sorting on linked-list is still n^2 with slightly better improvement, because of the same sorting strategy.

2) Recursion (Nested list):
In my opinion, recursion actually takes extra work to check whether the current case is basic or general. Consequently, the Big-0 of recursive function is at least n. I think recursion is a good strategy to solve problems with self-similar sub-problems, but recursion could not help improve the algorithm.

3) Tree:
General tree:
General tree is the same as recursion (nested list).
BST:
Binary search tree acts like ascending list. Compared with non-ascending list, it truly has advantages that the big-0 of searching should be lg(n) ~ n. However, compared with ascending list, it is hard to determine whether BST is better because the parent node could be more than or less than the average of two children which will cause the searching more difficult and increase the Big-O from lg(n) to n.

Friday, March 25, 2016

Week 10 Algorithm


We encountered algorithm this week. Here are some types of Big-O:

1) lg(n):
First of all, in computer science, the favorite base is 2, not e or 10.
Therefore, lg(n) could represent the number of times which we divide n by 2 until we reach 1. This idea is the same as the number of times which we double 1 until we reach n.
2) n:
This is such a case that we have dealt with many times before. It is simply linearly increasing. For example, when we try to check whether a certain integer is in a non-ascending list, we are doing the n algorithm.
3) n^2:
Many sort strategies we have learned in CSC108 belong to this type. For example, the bubble sort is to search for the maximum or minimum and put it at the first position and keeps sorting on the unsorted parts. The step of searching will take n times, and this step will also be applied n times. Therefore, we finally get n * n times, which is n^2

Friday, March 18, 2016

Week 9 Test 2


On Wednesday, we took a test, which covered the linked list, recursion and the algorithm. Having practiced lots of recursive functions, I summarize a strategy to write a correct recursive function. The first step is to check the special condition, such as None type or inappropriate type. The second step is to consider the most basic case. Finally, for those recursive parts, apply the function to each item.

Thursday, March 3, 2016

Week 7 More about Recursion



Last week, we encountered recursion, a useful technique to solve self-contained problems. We mainly discussed how recursive functions, such as sum_list and rec_max, work on the nested list. By tracing the repeating progress from simple to complex, we first got the idea of the recursion.

This week, rather than following given functions, we tried to design recursion. However, it’s hard for me to write a correct recursive function in the beginning. My problem is every time I started to write my function, I assumed my object is a nested list. Therefore, I chose to use for-loop to deal with each element of the original list.

This strategy is wrong, because it will stop and return 1 when the first element in “t” is not a list. I tried to figure out the right way to solve the problem by comparing my codes with the professor’s and I found that the most basic case is when t is an integer, not the first element in t is an integer.


Through practicing, we came up with a general way to design recursion:
1.         Figure out what the basic case, special case, and the general case.
2.         Using if statement to deal with each case, for the general case, use recursion.


Also, I found it is really important and useful for me to write comments under each case to help me figure out whether I need to use recursion or not.