Saturday, 7 February 2015

SLOG #3 MERGE SORT, AND THE MIDTERM

Last week we started our first lesson on recursion, an extremely powerful tool for computer scientist. So far, I only have a brief and vague idea of what recursion is, and I feel writing recursive function quite tricky(the last question in last week's lab). I attribute this feeling to a lack of exposure to this subject, so I will reserve it to the slog later in the semester that is prescribed to have this topic. Hopefully by that time I will feel more comfortable writing recursive functions.

The other topic that we covered last week was merge sort. Diane did not show us the code for this algorithm in the class. Instead she brought eight playing cards to show us how merge sort works. She first divided the cards into two groups, each group having four sorted cards. Then she put the sorted cards in two piles, held the cards at the top of each pile of cards, compared them, and put the card with smaller face value down on another area of the table. By doing so continuously, she completed the sort within four comparisons, suggesting that this way of "dividing and conquer" might be efficient in sorting lists. Then we had this vivid presentation of merge sort: Diane handed out the eight cards to a student in the first row. That student divided the cards into two piles, giving each pile to a person in the second row. The students in the second row and third row did the same thing until each of the eight students of the fourth row had one card. This is the "divide" part. The "conquer" part starts with students in the fourth row passing back the cards to the person in the third row, and that person needed to sort the cards. This is easy because these students only need to sort two cards. Now we have four sorted groups of cards. Apply this process again, we have two sorted groups of cards. Apply this process for the last time, we have a sorted list!

It is notable that the whole process consists of three "divide", and in each of the three sorting parts, we made four comparisons. This might be much faster than insertion sort if the list is not originally well sorted, in which case the run time may be as many as 64 times!

We also had a midterm this Wednesday. I have to say that the test was fair. Nothing really unexpected: two question about the design and implementation of class, the second of which tests on subclasses, and one that asks you to trace a recursive function. I don't find anything that is particularly tricky in these questions. Hopefully I did well in the midterm.

No comments:

Post a Comment