Selection Sort: This sort works by splitting the list into two parts: a sorted half and a non-sorted half. The algorithm then looks through the sorted half and puts the smallest element at the end of the sorted half.This occurs until the whole list is in the sorted half. Because the algorithm has to go through the whole list for every element of the list Selection Sort is O(n^2)
Merge Sort: Merge sort recursively separates the list into its smallest case (1 element) then takes two small lists and looks at the first elements. It then repetitively puts the smallest element in a new list until both the smallest lists are empty (or until one is empty and the rest of the other can go into the new list). The continues until all the 'mini-lists' are merged together into one big sorted one. Because Merge Sort divides the problem into smaller problems it has O(nlogn)
Quick sort: This chooses a point in the list then puts all smaller elements on one side and all the bigger elements on the other side. Now the point is at the right index. Quick sort then recursively Quick sorts the two sides. Quick sort also splits the problem into smaller problems and as such has a O(nlogn)
Thursday, 28 November 2013
Monday, 25 November 2013
Private and Classy
Private classes are now a thing in Python! Kinda. Not really but we at least now have a way to prevent people from easily tampering with our variables. They can still do I: just not easily. This is all thanks to the built in 'property' which takes 4 values. The first two are what you'd like to be the get and set methods for this variable. They can literally be anything (that compiles) but ideally they should return a copy of the requested variable and allow you to change said variable within the appropriate restrictions. The next parameter the property takes is delete. When attempting to delete this variable this is what is called. I wonder if by simply passing you could make your variable very hard to Delete. Lastly you can add read only docstring. At first I didn't see the point if your method already has docstring but then I realized that it might be useful to have docstring specific to the user to give him/her more relevant information ( or so that we're able to call the user funny names behind his back)
Friday, 8 November 2013
Test: Round Two
And so begins the second round of Tests. I think the things that killed me the first time all amounted to me not reading the question right so I going to make sure that I do that. A2 went well, I worked with a partner this time so it was nice to bonce off ideas with someone. A2 took a lot less time, partially I think because it was easier, but also because I spent a lot more time considering the most basic cases before I wrote anything. More time thinking = Less time writing code = Less time doing homework overall.
One of the things on the test will be binary search trees. The most complicated thing to do with trees seems to be deleting nodes so Ill now explain that here:
- first check if this is a child of a leaf (ie None). In that case we didnt find the item we wanted to delete in this branch so return None. This is the simplest case
- Then check if the item to delete (ITD) is smaller then the current Node. If that is true the ITD is in the left subtree so look in there.
- Then check if the ITD is greater then the current Node. If that is true the ITD is in the right subtree so look in there.
- If the ITD is not smaller or greater and the Node is not None, that means we have found a match. And should remove it
- We can do this by first checking if either child is None, and if this is true then we can replace the node with the other child
- If neither child is None then we can either replace the node with the largest item from the left subtree or the smallest item from the right subtree. We then need to delete the original item we just moved.
One of the things on the test will be binary search trees. The most complicated thing to do with trees seems to be deleting nodes so Ill now explain that here:
- first check if this is a child of a leaf (ie None). In that case we didnt find the item we wanted to delete in this branch so return None. This is the simplest case
- Then check if the item to delete (ITD) is smaller then the current Node. If that is true the ITD is in the left subtree so look in there.
- Then check if the ITD is greater then the current Node. If that is true the ITD is in the right subtree so look in there.
- If the ITD is not smaller or greater and the Node is not None, that means we have found a match. And should remove it
- We can do this by first checking if either child is None, and if this is true then we can replace the node with the other child
- If neither child is None then we can either replace the node with the largest item from the left subtree or the smallest item from the right subtree. We then need to delete the original item we just moved.
Subscribe to:
Posts (Atom)