CSC161 2010F Imperative Problem Solving

Class 40: Sorting, Revisited

Back to Pointers to Functions. On to Pause for Breath.

This outline is also available in PDF.

Held: Wednesday, 10 November 2010

Summary: We continue our exploration of sorting.

Related Pages:

Notes:

Overview:

Templates for Sorting Algorithms

As you may recall, we developed a generic template for sorting algorithms.

K&R take a slightly different perspective. They focus on arrays of pointers (most frequently arrays of strings) and pass in a comparator.

void sort(void *values[], int size, int (*compare)(void *x, void *y));

Is that enough to let us sort arrays of integers or doubles? No.

Can we combine the ideas? Certainly.

void PREFIX(sort)(TYPE values[], int size, int (*compare)(TYPE x, TYPE y));

Is this a good idea? I'll let you reflect on that question.

Weinman suggests an alternative way of combining the two. We skip the macro techinque and pass in the size of objects.

void sort(void *values, int size, int valsize, int (*compare)(void *x, void *y));

Is this a good idea? I'll also let you reflect on that question.

Review: Sorting Algorithms

We've sketched out a variety of sorting algorithms.

There are also many more.

Selecting Sorting Algorithms

Suppose we have a library of correctly implemented sorting routines. How do we decide which one to use?

Lab

Back to Pointers to Functions. On to Pause for Breath.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Dec 8 10:57:11 2010.
The source to the document was last modified on Fri Aug 27 07:12:23 2010.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Outlines/outline.40.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu