# Class 25: Introduction to Sorting

Back to Project Discussion: Specifications. On to Some Sorting Algorithms.

Held Monday, March 6, 2000

Overview

Today we begin our investigations of a key problem in computing: that of sorting. Sorting algorithms turn a collection (typically an array) of elements into a collection that is ordered (most typically, from smallest to largest).

Notes

• Not to stifle your creativity, but I get a lot of email, and when I'm looking for email from my students, I look under their names. If possible, please set your name appropriately when you send email to me. Thanks.
• Are there questions on assignment 3?
• I will be out of town from Wednesday noon until late Sunday evening. Make sure to talk to me about questions on the assignment before then.
• Vivek will have copies of an answer key to hand out on Friday.
• Assigned:

Contents

Summary

• Project discussion, continued
• The problem of sorting
• In-place vs. out-of-place sorting
• Stable sorts
• Due:
• Java Plus Data Structures, Chapter 11: Sorting

## Project Discussion

• Let's make sure that we've resolved any confusion we may have about the expectations and capabilities of the various interfaces.

## The Problem of Sorting

• Typically, computer scientists look at collections of problems and attempt to find appropriate generalizations of these problems (or their subproblems).
• By solving the generalized problem, you solve a number of related problems.
• One problem that seems to crop up a lot is that of sorting. Given a list, array, vector, sequence, or file of comparable elements, put the elements in order.
• in order typically 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 also need to ensure that all elements in the original list are in the sorted list.
• you also need to ensure that no other elements are in the list.
• We'll look at sorting arrays.
• You'll find that these sorting methods look somewhat different from those in the book. That's because we're working in a different context.
• In the book, the methods sort external arrays which are passed as parameters.
• In these notes, the methods sort arrays that are part of the same class.
• 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.
• Stable sorts preserve the order of equal elements. Stable sorting is necessary if you want to sort the same list multiple times, on different keys.
• For large enough data sets, not all of the elements can be stored in memory. Often, variant algorithms must be used in order to get more efficient operation.
• You may learn about such sorting algorithms in CSC302.
• Most often, in-memory sorting is accomplished by repeatedly swapping elements. However, this is not the only way in which sorting can be done.

### Sorting Examples

• In my experience, most of us know some sorting algorithms, but have difficulty generalizing them.
• I'll bring in some things to sort (most frequently, a stack of CDs) and we'll look at different ways to sort them.

### Insertion Sort

• One simple sorting technique is insertion sort.
• Insertion sort operates by segmenting the list into unsorted and sorted portions, and repeatedly removing the first element from the unsorted portion and inserting it into the correct place in the sorted portion.
• This may be likened to the way typical card players sort their hands.
• What's the running time?
• We do the find-place and insert steps O(n) times.
• Each insertion takes O(n) in an array.
• Each findPlace takes O(log2n) in a sorted array. (Using binary search.)
• Hence, the running time is O(n*(n+log2(n)).
• Simplifying, we get O(n2).
• What's the extra storage? It should be constant.
• How might we code this recursively?

## A Sortable Class

• One of the difficulties of object-oriented design is deciding what belongs where.
• Are the sorting methods methods of some kind of collection class (which seems reasonable: ``You there! Sort yourself!'')
• Are hey applied to some kind of collection (which also seams reasonable: ``Carl and Carla, please sort this stuff'').
• We'll choose the first and build objects that know how to sort themselves. It should be a natural step to the second.
• Here's a class which we might extend by adding a sort method.
```
/**
* A objects of values which can be indexed by
* integers from 0 to size().  Basically, a wrapper
* class for Java's built-in arrays.
*
* @author Samuel A. Rebelsky
* @version 1.0 of September 1999
*/
public class Array {
// +--------+--------------------------------------------------
// | Fields |
// +--------+

/** The elements of the objects. */
public Object[] objects;

// +--------------+--------------------------------------------
// | Constructors |
// +--------------+

/**
* Build a new array which holds up to n elements.
* Initially, each element is null.
*/
public Array(int n) {
objects = new Object[n];
} // Array(int)

/**
* Build a new array which holds the specified
* set of elements.
*/
public Array(Object[] elements) {
objects = new Object[elements.length];
for (int i = 0; i < elements.length; ++i) {
objects[i] = elements[i];
}
} // Array(Object[])

// +-----------+-----------------------------------------------
// | Accessors |
// +-----------+

/**
* Get the ith element of the array.
*
* @exception ArrayIndexOutOfBoundsException
*   For the obvious reasons.
*/
public Object get(int i) {
return objects[i];
} // get(int)

/**
* Get the number of elements in the array.
*/
public int size() {
return objects.length;
} // size()

// +-----------+-----------------------------------------------
// | Modifiers |
// +-----------+

/**
* Set the ith element of the array.
*
* @exception ArrayIndexOutOfBoundsException
*   For the obvious reasons.
*/
public void set(int i, Object value) {
objects[i] = value;
} // set(int, Object)

/**
* Swap the ith and jth elements of the array.
* Included because it's commonly used during sorting.
*/
public void swap(int i, int j) {
Object temp = objects[i];
objects[i] = objects[j];
objects[j] = temp;
} // swap(int,int)

} // class Array

```
• Here's the corresponding interface.
```
import Comparator;
import IncomparableException;

/**
* Things that you can tell to sort themselves.
*/
public interface Sortable {
/**
* Sort the contents of the thing using a comparator
* to compare elements.
* Pre: The elements can be compared.
* Post: The elements are sorted.  Each element is no greater
*   the the next element.
* Post: No elements are added or deleted.
*
* @exception IncomparableException
*   if there are pairs of elements that cannot be compared.
*/
public void sort(Comparator compare)
throws IncomparableException;
} // Sortable()

```

## History

Tuesday, 18 January 2000

• Created as a blank outline.

Monday, 6 March 2000

Tuesday, 7 March 2000

• Added insertion sort (covered in class, but not originally in the outline).

Back to Project Discussion: Specifications. On to Some Sorting Algorithms.

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.