We've been learning the efficiency of different algorithms. A good algorithm will have a high efficiency which takes less computer memory and time to process it. Therefore, it is important to understand different algorithms and how to estimate the efficiency (run-time) of functions.
So far we have learned a few sorting algorithms: insertion, selection, bubble, quick, merge. These sorting methods has different efficiency. Although some of them have the same efficiency on the worst case, it does not mean that they have exactly the same run-time. The Big-O notation ignores the constants, so that, although you might see that they have the same Big-O, it doe not mean that they have the same efficiency. For instance, insertion sort and selection sort have the same efficiency on the worst case. However, we can still find that insertion sort is faster than selection sort if we test them precisely.
2014年3月30日 星期日
2014年3月2日 星期日
Recursion: An Useful/Helpful Coding Technique
Recursion has been my favorite coding technique since I learned it recently. I found out that it is very useful when I was writing assignment one: Hanoi Tower. Using recursion saves us a lot of time and the code will be a lot shorter since it keeps on calling the function itself until it meets the base case, particularly, when we're dealing with a huge and complicated algorithm. To know how to write a recursion, we have to find the base case first since it is the trigger of stopping a recursion, else the recursion will never end. It is also important for understanding the recursion we are writing. Then we think about the next recursive step in order to complete the recursion.
Writing recursion was hard for me at first. The idea of calling the function itself without actually doing anything confused me a lot. Later, I figured out that I was confused since I was not considering about the base case. List comprehension is a lovely recursive technique. I used a lot of recursions when I am coding. Especially, for the tree methods, list comprehension makes life a lot easier for us to understand how the methods were written. Recently, when I am dealing a list of lots of inner lists, I always use list comprehension to sort the list first, then I do whatever I need to meet the function.
Writing recursion was hard for me at first. The idea of calling the function itself without actually doing anything confused me a lot. Later, I figured out that I was confused since I was not considering about the base case. List comprehension is a lovely recursive technique. I used a lot of recursions when I am coding. Especially, for the tree methods, list comprehension makes life a lot easier for us to understand how the methods were written. Recently, when I am dealing a list of lots of inner lists, I always use list comprehension to sort the list first, then I do whatever I need to meet the function.
訂閱:
文章 (Atom)