|
|
|
|
|
Let's write the Selection Sort algorithm:
public static void selectionSort(int[] list)
{
for (int i = 0; i < list.length - 1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
int min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k] < min ) // Found a smaller element
{
min = list[k]; // Update min value
minIndex = k; // Update index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary;
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
int help = list[minIndex];
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
Repeatedly do: (1) find the smallest element in the sub list list[i] .. list[N]
public static void selectionSort(int[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
<---- find min in sublist ---->
+---+---+---+---+---+---+---+---+---+---+---+
| 1 | 2 | 3 |...|...|...| 4 |...|...|...|...|
+---+---+---+---+---+---+---+---+---+---+---+
^ ^
| |
i minIndex
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary;
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
int help = list[minIndex];
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
Repeatedly do: (2) if list[minIndex] is out of place: swap list[minIndex] <--> list[i]
public static void selectionSort(int[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
<---- find min in sublist ---->
+---+---+---+---+---+---+---+---+---+---+---+
| 1 | 2 | 3 |...|...|...| 4 |...|...|...|...|
+---+---+---+---+---+---+---+---+---+---+---+
^ ^
| |
i minIndex
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
+---+---+---+---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 |...|...|...|...|...|...|...|
+---+---+---+---+---+---+---+---+---+---+---+
}
}
}
|
Let's flesh out the selection sort algorithm:
public static void selectionSort(int[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
int min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k] < min ) // Found a smaller element
{
min = list[k]; // Update min value
minIndex = k; // Update index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
int help = list[minIndex];
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
The standard "find min" algorithm assumes that the first element in the array is the minimum:
public static void selectionSort(int[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
int min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k] < min ) // Found a smaller element
{
min = list[k]; // Update min value
minIndex = k; // Update index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
int help = list[minIndex];
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
We look for a better minimum in the remaining elements:
public static void selectionSort(int[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
int min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k] < min ) // Found a smaller element
{
min = list[k]; // Update min value
minIndex = k; // Update index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
int help = list[minIndex];
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
If we find a better minimum, we remember its value and index:
public static void selectionSort(int[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
int min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k] < min ) // Found a smaller element
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
int help = list[minIndex];
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
If the smallest value in the sublist is out of place, swap list[minIndex] <--> list[i] :
public static void selectionSort(int[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
int min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k] < min ) // Found a smaller element
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
int help = list[minIndex]; // Standard exchange alg
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
DEMO: demo/04-inheritance/19-selsort :
public static void main(String[] args)
{
int[] myList = {8, 4, 9, 7, 3, 5, 6, 1, 2};
// Print list[] before sorting
for ( int i = 0; i < myList.length; i++)
System.out.print(myList[i]+ " ");
System.out.println();
selectionSort(myList);
// Print list[] after sorting
for ( int i = 0; i < myList.length; i++)
System.out.print(myList[i]+ " ");
System.out.println();
}
|
I will use the selection sort method to show you an application of polymorphism next