Fundamentals of Computer Science II (CSC-152 97F)

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

# Assignment Six: Sorting Lists

• Assigned: Monday, November 3, 1997
• Due date: 4pm, Thursday, November 13, 1997

Summary: In this assignment, you will use your implementation of linked lists to develop and use sorting routines.

Collaboration: You can work on this assignment in groups of up to size three. You may discuss your design with any size group. You may also work with each other on general debugging issues.

If you work as part of a group, you are responsible for ensuring that all members of the group understand all sections of the assignment.

## Assignment

Using your list implementation, implement list-based variants of QuickSort and MergeSort. Here are quick (and not necessarily accurate) sketches of the two routines. Note that these sketches don't handle special cases or exceptions, and rely on a number of utility routines that are not yet written.

```/**
Sort a list using the legendary quicksort algorithm which partitions the
list into small and large parts and then sorts each part.
pre: initialized list
post: a sorted versions i returned (all the original elements are still
there and each element is no bigger than the subsequent element)
*/
public List quickSort(List unsorted) {
List smaller;	// The smaller elements of the list
List bigger;	// The larger elements of the list
Object pivot;	// A pivot used to separate the list into smaller and bigger
// elements.
// Base case: return one or fewer elements
if (unsorted.size() <= 1) return unsorted;
// Pick the pivot
pivot = pickPivot(unsorted);
// Get the smaller and bigger elements
smaller = extractSmaller(unsorted,pivot);
bigger = extractBigger(unsorted,pivot);
// Sort the two lists
smaller = quickSort(smaller);
bigger = quickSort(bigger);
// Join them
return join(smaller,bigger);
} // quickSort

/**
Sort a list using the legendary merge sort algorithm which partitions the
list into two halves, sorts each part, and joins them back together.
pre: initialized list
post: a sorted versions i returned (all the original elements are still
there and each element is no bigger than the subsequent element)
*/
public List mergeSort(List unsorted) {
List left;	// The first n/2 elements of the list
List right;	// The last n/2 elements of the list
// Base case: return one or fewer elements
if (unsorted.size() <= 1) return unsorted;
// Get the two halves of the list
left = firstHalf(unsorted);
right = secondHalf(unsorted);
// Sort them
left = mergeSort(left);
right = mergeSort(right);
// Merge them
return merge(left,right);
} quickSort
```

Both of thse routines might be expressed more concisely as

```public List quickSort(List unsorted) {
Object pivot;
// Base case
if (unsorted.size() <= 1) return unsorted;
// Recursive case
pivot = pickPivot(unsorted);
return join(quickSort(smaller(unsorted,pivot))
quickSort(larger(unsorted,pivot)));
}

public List mergeSort(List unsorted) {
// Base case
if (unsorted.size() <= 1) return unsorted;
// Normal case
return merge(mergeSort(firstHalf(unsorted)),
mergeSort(secondHalf(unsorted)));
}
```

Note that you'll be dealing with integers and integer lists rather than objects and general lists, which may make some parts easier.

In addition to fine-tuning my algorithms and changing types, you'll need to write

• `pickPivot(IntegerList l)` which picks a reasonable pivot. You may just return the first or second element of the list.
• `smaller(IntegerList l, int pivot)` which returns a list consisting of all the elments of the original list that are less than or equal to the pivot.
• `larger(IntegerList l, int pivot)` which returns a list consisting of all the elements of the original list that are bigger than the pivot.
• `join(IntegerList l1, IntegerList l2)` which joins two lists, the first immediately before the second.
• `firstHalf(IntegerList l)` which returns the first half of a list.
• `secondHalf(IntegerList l)` which returns the second half of a list.
• `merge(IntegerList l1, IntegerList l2)` which merges two sorted integer lists.

Note that some of your methods may be easier to write if you define an iterator for your integer lists.

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Examples] [Readings] [Bailey Docs]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

Source text last modified Mon Nov 3 09:37:42 1997.

This page generated on Mon Nov 3 10:02:55 1997 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu