Laboratory Exercises For Computer Science 213

Laboratory Exercise on Arrays, Sorting, and Searching

Laboratory Exercise on Arrays, Sorting, and Searching

Goals: This laboratory exercise provides practice with arrays, in the context of procedures. Details focus on a procedure to partition an array, with applications to sorting and searching.

Problems: In this lab, you are to write solutions to the following two problems. In each case, turn in your solutions utilizing the format for submitting assignments.


  1. Partition: Write a procedure
    
    void partition (int a[], int left, int right, int *middle)
    
    which rearranges elements those within the a array with indices between left and right, so that

    Thus, the array segment a[left] ... a[right] is rearranged as required, to give a new arrangement of values a[left] ... a[(*middle)] ... a[right], where all array elements before position *middle have values <= a[(*middle)] and where a[(*middle)] is less than or equal to all array elements after position *middle.

    Include your partition procedure in an appropriate test program, so that you can adequately check the correctness of your code.

    Discussion: Work within partition should be done with a single pass through the elements a[left] ... a[right], as follows.

    1. move upward in the array through a[right], a[right-1], ... until finding an element a[r_spot] where a[r_spot] < a[left], if such an element exists.
    2. move downward in the array through a[left], a[left+1], ... until finding an element a[l_spot] where a[l_spot] > a[left], if such an element exists.
    3. swap a[l_spot] and a[r_spot].
    4. continue steps 1, 2, and 3 until searching all elements in the array segment. (At this point, "small" values will have been moved early within the array segment, while "large" values will have been moved late.)
    5. place a[left] in its appropriate position a[*middle], where *middle is the index where l_spot and r_spot have come together.

  2. Quicksort: The quicksort algorithm proceeds by applying partition of problem 1 recursively, until all subintervals have no more than a single element (and thus are already sorted).

    Write a procedure

    
    void quicksort (int a[], int n)
    
    that uses the partition procedure and sorts the array a[0], ..., a[n-1] using the quicksort algorithm. Since this quicksort is to be a general sorting procedure for any array, the integer n is used to indicate the size of the array.

    As in the previous problem, you should include your quicksort procedure in an appropriate test program, so that you can adequately check the correctness of your code.

  3. Finding Median Values: The partition procedure of problem 1 may be used to find the kth smallest element in an array segment a[left], ..., a[right], as follows:

    1. If there is only one element in a[left], ..., a[right], then one expects k = 1, and one can return that element.
    2. Use the partition procedure to rearrange a[left], ..., a[right] into a[left] ... a[(*middle)] ... a[right] as described in problem 1.
    3. If k = ((*middle)-left+1), then a[*middle] is the desired element, and stop.
    4. If k <= ((*middle)-left), then apply this algorithm recursively to find the kth smallest element in an array segment a[left], ..., a[(*middle)-1].
    5. If k >= ((*middle)-left), then apply this algorithm recursively to find the k-((*middle)-left)-1th smallest element in an array segment a[(*middle)+1], ...,a[right].

    Problem:

    1. Write a procedure
      
      void select (int a[], int k)
      
      that uses the above algorithm and procedure partition to find the kth smallest element in an array a.

    2. Write a procedure
      
      void median (int a[], int n)
      
      that uses the above algorithm and procedure partition to find the median element in an array a, where n is the size of the array.


    This document is available on the World Wide Web as

         http://www.math.grin.edu/~walker/courses/213.fa98/lab1.html
    

    created September 8, 1998
    last revised September 8, 1998