```
//********************************************************************
// SortPhoneList.java Author: Lewis/Loftus/Cocking
//
// Driver for testing an object sort.
//********************************************************************
public class SortPhoneList
{
//-----------------------------------------------------------------
// Creates an array of Contact objects, sorts them, then prints
// them.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Contact[] friends = new Contact[7];
friends[0] = new Contact ("Barika", "Favaken", "610-555-7384");
friends[1] = new Contact ("Lorenz", "Lowski", "215-555-3827");
friends[2] = new Contact ("Ely", "Kassakian", "733-555-2969");
friends[3] = new Contact ("Galen", "Powers", "663-555-3984");
friends[4] = new Contact ("Laura", "Getz", "464-555-3489");
friends[5] = new Contact ("Ching", "Lee", "322-555-2284");
friends[6] = new Contact ("Susan", "Getz", "243-555-2837");
Sorts.insertionSort(friends);
for (Contact contact: friends)
System.out.println (contact);
}
}
```

```
//********************************************************************
// Sorts.java Author: Lewis/Loftus/Cocking
//
// Demonstrates the selection sort and insertion sort algorithms,
// as well as a generic object sort.
//********************************************************************
public class Sorts
{
//-----------------------------------------------------------------
// Sorts the specified array of integers using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (int[] numbers)
{
int min, temp;
for (int index = 0; index < numbers.length-1; index++)
{
min = index;
for (int scan = index+1; scan < numbers.length; scan++)
if (numbers[scan] < numbers[min])
min = scan;
// Swap the values
temp = numbers[min];
numbers[min] = numbers[index];
numbers[index] = temp;
}
}
public static void selectionSort (Comparable[] objects)
{
int min;
Comparable temp;
for (int index = 0; index < objects.length-1; index++)
{
min = index;
for (int scan = index+1; scan < objects.length; scan++)
if (objects[min].compareTo(objects[scan]))
min = scan;
// Swap the values
temp = objects[min];
objects[min] = objects[index];
objects[index] = temp;
}
}
//-----------------------------------------------------------------
// Sorts the specified array of integers using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (int[] numbers)
{
for (int index = 1; index < numbers.length; index++)
{
int key = numbers[index];
int position = index;
// shift larger values to the right
while (position > 0 && numbers[position-1] > key)
{
numbers[position] = numbers[position-1];
position--;
}
numbers[position] = key;
}
}
//-----------------------------------------------------------------
// Sorts the specified array of objects using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (Comparable[] objects)
{
for (int index = 1; index < objects.length; index++)
{
Comparable key = objects[index];
int position = index;
// shift larger values to the right
while (position > 0 && objects[position-1].compareTo(key) > 0)
{
objects[position] = objects[position-1];
position--;
}
objects[position] = key;
}
}
}
```