CS209 lab2


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




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

  1. Consider the QUICKSORT algorithm covered in lectures, with code http://www.maths.nuigalway.ie/~gettrick/teach/cs209/progs/qs.c Adapt this code
    • To sort a random list of 1,000 integers between 1 and 1,000,000.
    • To print out (to the screen) the number of comparisons made (in a sort of 1,000 integers between 1 and 1,000,000). (The relevant lines of code are while(arr[j] > z) and while( arr[i] > z)). Run this a few times.
    • To print out (to the screen) the number of times the actual function quicksort is called.
  2. Adapt the program http://www.maths.nuigalway.ie/~gettrick/teach/cs209/progs/ms.c so that it (merge) sorts a random array of NUMINT integers, each between 1 and MAXINT (using rand as in the qs.c program).
  3. For this program print out to the screen
    • how many times function merge is called
    • how many times function mergesort is called
    • how many comparisons in total are made (the relevant line is if a[i] < a[j] ).

© NUI, Galway