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

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

# Outline of Class 21: Sorting

## Miscellaneous

• This week there are a number of Take Back the Night events. I strongly encourage you to go, particularly if you don't know or understand why we need such events.
• Modified bribe day today: I'll just give you candy at the beginning of class.
• I still haven't tested all the code for today's class, but I'm working on it.

## Sorting

• Typically, computer scientists look at collections of problems and attempt to generalize subproblems. They then look for optimal solutions to those subproblems.
• One problem that seems to crop up a lot is that of sorting. Given a list, array, or vector of comparable elements, put the elements in order.
• in order means that each element is no bigger than the next element. (You can also sort in decreasing order, in which case each element is no smaller than the next element.)
• you usually need to ensure that all elements in the original list are in the sorted list.
• In evaluating sorting methods, we should concern ourselves with both the running time and the amount of extra storage (beyond the original vector) that is required.
• In place sorting is a special subclass of sorting algorithms in which the original object is modified, and little, if any, extra storage is used.
• Most often, sorting is accomplished by repeatedly swapping elements of the vector. However, this is not the only way in which sorting can be done.

### Selection sort

• Selection sort is among the simpler and more natural methods for sorting.
• In this sorting algorithm, you break the vector up into two subvectors, a sorted part and an unsorted part. You repeatedly find the largest of the unsorted elements in the Vector, and put that at the beginning of the sorted part. This continues until there are no unsorted elements.
• Here's my version of selection sort. It is a method of a `Vector`-like object, so we don't explicity refer to the vector.
```/**
Sort all the elements in the subvector between lb and ub.
pre: 0 <= lb <= ub < size()
pre: the elements in the vector are comparable using
a lessEqual method
post: elementAt(lb) <= elementAt(lb+1) <= ... elementAt(ub)
*/
public void selectionSort(int lb, int ub) {
// Variables
int index_of_largest;	// ... element in subrange
// The preconditions will be checked in the following
// function call, so we don't check them here

// Base case: one element, so it's sorted
if (lb == ub) {
return;
}
// Find the index of the largest element in the subrange
index_of_largest = indexOfLargest(lb,ub);
// Swap that element and the last element
swap(index_of_largest, ub);
// Sort the rest of the subvector (if there is any)
// Note that we don't have to compare ub-1 to lb, since
//   the preconditions and the base case take care of it.
selection_sort(lb,ub-1);
} // selectionSort

/**
Find the index of the largest element in a subvector
pre: 0 <= lb <= ub < size()
pre: the elements in the vector are comparable using
a lessEqual method
post: returns I s.t. for all i, lb <= i <= ub,
elementAt(I) >= elementAt(i)
*/
public int indexOfLargest(int lb, int ub) {
// Variables
int guess;	// Current guess as to index of largest
// Check the preconditions
Assert.pre(lb <= ub, "nonempty subrange");
Assert.pre(0 <= lb, "reasonable starting point");
Assert.pre(ub < size(), "reasonable ending point");
// Make initial guesses
guess = lb;
// Repeatedly improve our guesses until we've looked at
// all the elements
for(int i = lb+1; i <= lb; ++i) {
if (elementAt(guess).lessEqual(elementAt(i))) {
guess = i;
} // if
} // for
// That's it
return guess;
} // index_of_largest
```
• What's the running time of this algorithm? To sort a vector of n elements, we have to find the largest element in that vector in O(n) steps, and then recurse on the rest. The first recursive call takes O(n-1) steps plus the recursion. And so on and so forth. This makes it an O(n^2) algorithm.
• What's the extra memory required by this algorithm (ignoring the extra memory for recursive calls)? It's more or less O(1), since we only allocate a few extra variables and no extra vectors.
• How much extra memory is required for recursive method calls? This is a tail-recursive algorithm, so there shouldn't be any.

### Insertion sort

• Another simple sorting technique is insertion sort.
• Insertion sort operates by segmenting the list into unsorted and sorted portions, and repeatedly removing an element from the unsorted portion and inserting it into the sorted portion.
• This may be likened to the way typical card players sorts their hands.
• In approximate code (assuming that we're writing this as part of a class that subclasses `Vector`)
```/**
Sort a subvector.
pre: 0 <= lb <= ub < size()
pre: the elements in the vector are comparable using
a lessEqual method
post: elementAt(lb) <= elementAt(lb+1) <= ... <= elementAt(ub)
*/
public void insertionSort(int lb, int ub) {
// Variables
Object tmp;	// The object that we'll be inserting
// Check the preconditions
Assert.pre(lb <= ub, "nonempty subrange");
Assert.pre(0 <= lb, "reasonable starting point");
Assert.pre(ub < size(), "reasonable ending point");
// Base case: one element, so it's sorted
if (lb == ub) {
return;
}
// Remember the element at the end, and then remove it
tmp = elementAt(ub);
removeElementAt(ub);
// Sort all but that element
insertionSort(lb,ub-1);
// Insert the element at the proper place
insert(findPlace(lb,ub-1,tmp), tmp);
} // insertionSort
```
• What's the running time? There are O(n) insertions and O(n) calls to `findPlace()` (which finds the proper place in the vector to insert the element). Each insertion requires O(n) steps, and each place determination takes O(log_2(n)) steps, so the running time is O(n*(n+log_2(n)) which is O(n).
• What's the extra storage (ignoring recursive calls)? It should be constant.
• What's the extra storage for the recursive method calls? It can be O(n), since this is not a tail-recursive method.

Outlines: prev next

[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 Tue Oct 7 17:13:18 1997.

This page generated on Wed Nov 5 12:38:36 1997 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu