void quicksort (integer A[], integer p, integer r) begin integer q; if p < r then q := partition(A, p, r) quicksort(A, p, q) quicksort(A, q+1, r) end if end quicksort integer partition (integer A[], integer p, integer q) begin integer pivot := A[p] integer i := p-1 integer j := q+1 while (true) repeat j := j-1 until (A[j] <= pivot) repeat i := i+1 until (A[i] >= pivot) if i < j then swap(A[i], A[j]) else return j end if end loop end partitionYour job is to write the two procedures partition and quicksort, as well as a main. Your main should do the following:
Enter size of array to sort (max 10000; negative size to quit): 100 Enter the random seed: 23 Array sorted successfully. Enter size of array to sort (max 10000; negative size to quit): 320 Enter the random seed: 42 Array sorted successfully. Enter size of array to sort (max 10000; negative size to quit): -1 Bye.
# initRandArray # arguments: $a0 base address of array of words # $a1 length of array (integer) # $a2 seed for random number generator (integer) # return val: none # Fill in the specified number of words starting at the memory location # given in $a0. Behavior is unspecified for non-positive size # arrays. # isSorted # arguments: $a0 base address of array of words # $a1 length of array (integer) # return val: 1 if sorted; 0 if otherwise # Check if given array is sorted in ascending order. # By default, arrays of length 1 are "sorted." The "sortedness" of # non-positive length arrays is unspecified.These routines are automatically loaded in by spim. DO NOT load another file that has these routines. If, for your own edification, you would like to look at the code for these routines, they can be found in /cse/courses/cse378/CurrentQtr/lib/hw3lib.spim. DO NOT load this file into spim.
For those of you using PCSpim at home, we have inserted the code of hw3lib.spim into trap.handler, which spim loads automatically. This is located in the course lib/ directory, i.e. /cse/courses/cse378/CurrentQtr/lib/. You can take a copy of trap.handler home with you, put that in the appropriate place in your PCSpim install, and everything should work. Note that if we update trap.handler (with bug fixes for example), you are responsible for updating any home copy you have. trap.handler will be more up-to-date than hw3lib.spim. Talk to Jon if you have any questions or problems.
You are writing everything from scratch (besides the routines we provide) this time. Of course, you'd probably be wise to start from some gutted version of your 378 machine solution. As always, clarity, simplicity and efficiency are valued, as are legible comments.
Quicksort and partition are not terribly complicated procedures, but because of the recursive nature of quicksort, debugging it can sometimes be a challenge. An hour of thinking about the design will be worth 10 hours of debugging! Good design and implementation techniques will make your life easier. So start small and work incrementally towards your solution (for instance, understanding, writing, and testing partition before worrying about quicksort seems a good tack to take). In all, you shouldn't find yourself writing more than a couple pages of code (excepting comments). And the usual words to the wise apply here: START NOW!