Page 429 - Introduction to Programming with Java: A Problem Solving Approach
P. 429

                10.8 Sorting an Array 395
 /*******************************************************************
*
Sort.java
Dean & Dean
This class uses a selection sort to sort a single array.
*******************************************************************/
*
*
*
public class Sort
{
}
// end Sort
public static void sort(int[] list)
{
}
// end sort
int j;
// index of smallest value
for (int i=0; i<list.length-1; i++)
{
}
j = indexOfNextSmallest(list, i);
swap(list, i, j);
//****************************************************************
private static int indexOfNextSmallest(
       Apago PDF Enhancer
int minIndex = startIndex; // index of smallest value
{
}
// end indexOfNextSmallest
{
}
int[] list, int startIndex)
{
for (int i=startIndex+1; i<list.length; i++)
if (list[i] < list[minIndex])
{
}
minIndex = i;
} // end for
return minIndex;
//****************************************************************
private static void swap(int[] list, int i, int j)
int temp;
temp = list[i];
list[i] = list[j];
list[j] = temp;
// end swap
// temporary holder for number
Figure 10.15
Sort class containing a method that sorts an array of integers in ascending order














































   427   428   429   430   431