In Class: Week 12, Monday, Merge Sort


Create a week12 subdirectory in your cs21/class directory and copy over my starting point files:

    $ cd 
    $ cd cs21/class
    $ pwd
      /home/your_user_name/cs21/class

    $ mkdir week12        
    $ cd week12
    $ pwd
      /home/your_user_name/cs21/class/week12

    $ cp ~newhall/public/cs21/week12/* .
	

Before you sit down:
  1. Get a playing card from me

  2. Find the student with the matching playing card and sit next to him/her. Introduce yourself to your partner.

  3. 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.

  1. Open recursivelist.py, and we will look at the implementation of the mergeSort function together.

  2. Next, you and your partner are going to come up with a merging algorithm using 8-10 cards that we give you.
    1. Divide the cards into two sets, representing two sorted lists of size 4 or 5.
    2. 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.
    3. Your algorithm should only make a single pass through each list (use the fact that the two lists are sorted)
    4. 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?
    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.

    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:
    	selectionSort(my_list):