COM1201 Spring 1997 Professor Fell QUIZ 5

1(7 points) Sort the array
37651482
using Insertion Sort . Show the results after each pass in the table below. Explain, briefly, what happened next to each row.
01234567index     
37651482original array           
          
          
          
          
          
          
          

2(6 points) Write a recursive function

	int S(int A[], int size)

that returns the sum of
A[0] + A[1] + A[2] + . . . + A[size - 1].

3(7 points) The idea of the partition in QuickSort is:

  • Define M = (Lower + Upper) / 2.
  • Set X = A[M].
  • Initialize indices I = Lower and J = Upper.
  • Run a main loop while I ² J:

    A. Run a subloop to increase I until a large element is found
    B. Run a subloop to decrease J until a small element is found
    C. If it happens that I ² J after steps A and B then:

    Show the work of the partition function on the array below.

    a.	M =
    
    b.	X =
    
    c.	Show the contents of the array after each swap
    
    d.	What are the values of I and J when the partition is finished?
    
    

    01234567index     
    37651482original array           
              
              
              
              
              
              
              


    Last Updated: May 5, 1997 7:01 pm by

    Harriet Fell
    College of Computer Science, Northeastern University
    360 Huntington Avenue #161CN,
    Boston, MA 02115
    Internet: fell@ccs.neu.edu
    Phone: (617) 373-2198 / Fax: (617) 373-5121
    The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1101/QUIZ/Q5/quiz5.html