In week 9, lectrues were about sorting algorithm. In lab 10 , different sorting method run for sorting already sorted list, random list and sorted but reversed list.
The method includes: selection sort, insertion sort, bubble sort, merge sort, quick sort and bulit_in sort.
After running sort.py, the result for speed of each sort was clear to be seem from time they used. Built_in sort, which is also called tim_sort, is the fastest. Contradictory, three former sorts that learnt from csc108 are slowest.
By viewing the code, three slowest sorts have common strategies, they go over the list during each single element sorting. It obviously decreases the speed of finishing sorting the whole list. However, some other sorts use some method like cut the list into two parts and sort in just one part, after all the times of sorting will only be lgn, where n is the length of list.
There is also a way to improve the efficiency of tranditional sorting method like selection sorts or bubble sorts. It is adding a swap step between sorting each element.
Still, I don't know the principle of bulit_in sort algorithm.
Thursday, 27 March 2014
Binary Search Trees
After LLNode and LinkedList, professor taught us a new class which was rooted at binary tree.It is called BTNode.
BTNode contains three parts: an object which is the root of BST, and left, a BST, right , also a BST.For BTNode, if it only has numbers, the sorting step will be much easier. As the left tree only contains smaller number whereas right tree only contains larger number.
BTNode contains three parts: an object which is the root of BST, and left, a BST, right , also a BST.For BTNode, if it only has numbers, the sorting step will be much easier. As the left tree only contains smaller number whereas right tree only contains larger number.
Class LLNode
We already learnt LinkedList, object LLNode represents a node in LinkedList, it also contains an object named item and a following LLNode.
At first I could not find the difference between LinkedList and LLNode. In my point of view, these two object have the same structure, an object linked with an inner itself. So why we should use two different classes to respresent the same chain of objects?
At first I could not find the difference between LinkedList and LLNode. In my point of view, these two object have the same structure, an object linked with an inner itself. So why we should use two different classes to respresent the same chain of objects?
Linked List
For last week, we've learnt a class called Linked List. Different from list, LinkedList is divided into two parts, an object called item and link which is also a LinkedList. As result, normal method like insert() or len() cannot be used on LinkedList. But in LinkedList class, we can design functions to help get the length of LinkedList or insert item into the LinkedList.
Still, even I know the structure of LinkedList and how to write parts of the functions, I still struggle in why we have to use this class rather than list class.
Still, even I know the structure of LinkedList and how to write parts of the functions, I still struggle in why we have to use this class rather than list class.
Subscribe to:
Posts (Atom)