CS209 lab2


http://www.maths.nuigalway.ie/~gettrick/teach/cs209/l2.html




http://www.maths.nuigalway.ie/~gettrick/teach/cs209/progs/
Consider the INSERTION SORT and MERGE SORT algorithms covered in lectures, with code at http://www.maths.nuigalway.ie/~gettrick/teach/cs209/progs/

  1. Build a new hybrid recursive algorithm, that calls insertion sort and merge sort alternately. So, say, first time around merge sort is called, and this function will then call two copies of insertion sort, which then calls merge sort, etc. (with of course successively decreasing sizes of the lists).
    Your program will have to have two functions. (Example: Say we were sorting [4,-1,2,9,0]. We call mergesort on this, which will lead to a merge of the 2 lists [4,-1] and [2,9,0]. For [4,-1], we call insertion sort, which will insert the element -1 into the list [4]. For [2,9,0] we call insertion sort, which will insert 0 into the list [2,9]. For [2,9], we call merge sort which will merge the lists [2] and [9]).
  2. Adapt your code as follows. If the (sub)list has an even number of elements, always carry out mergesort, while if the (sub)list has an odd number of elements, always carry out insertion sort (all within the one algorithm).

© NUI, Galway