In Class: Week 12, Monday, Merge Sort
Create a week12 subdirectory
in your cs21/class directory and copy over my starting point files:
$ cd cs21/class
$ mkdir week12
$ cd week12
$ cp ~newhall/public/cs21/week12/* .
Before you sit down:
- Get a playing card from me
- Find the student with the matching playing card and sit next to
him/her. Introduce yourself to your partner.
- If there is not yet such a student, sit someplace with an empty seat
next to you for your partner to sit when s/he finds you.
- Open recursivelist.py, and we will look at the implementation of
the mergeSort function together.
- Next, you and your partner are going to come up with a merging algorithm
using 8-10 cards that we give you.
Write up the steps on your algorithm on a piece of paper, and once you
think you have an algorithm that works, show it to me or a Ninja.
- Divide the cards into two sets, representing two sorted lists of
size 4 or 5.
- Using the cards, come up with an iterative (non-recursive) algorithm
for merging the cards together to create a new sorted list of
twice the size.
- Your algorithm should only make a single pass through each list
(use the fact that the two lists are sorted)
- Try your algorithm out for different cases of the two sorted lists:
what if all the larger elements are in the first list?
what if they are in the second list? what if they are distributed
between the two? what if the smallest element is in the first list?
the second list? what happens when you run out of elements in one of
the two lists but the other list still has some?
Once you have shown us your algorithm, you and your partner should try
coding it up in the recursivelist.py file in the merge function stub:
def merge(lst1, lst2):
Then, comment out the call to selection sort in main and call mergeSort
instead. Remember that mergeSort creates and returns a new sorted list,
so it call must look like:
my_list = mergeSort(my_list):
vs. selection sort which is an in-place sort, meaning that it sorts the
passed list as a "side-effect" and thus does not return the sorted list: