The algorithm is called selection sort
Wikipedia has a nice write up on this algorithm: click here
The selection sort algorithm can be used to sort any data set where there is an ordering among the data items.
|
|
Let: a = array containing the values Let: n = # of elements 1. Find the array element with the min. value among a[0], a[1], ..., a[n-1]; 2. Swap this element with a[0] Illustration: |
Observation:
|
Let: the values be store in an array "a" Let: n = a.length for ( i = 0 ; i < n ; i ++ ) { Find the array element with the min. value among a[i], ..., a[n-1]; Swap this element with a[i]; } |
Check for yourself:
|
|
|
// Find array element with min. value among a[0], a[1], ..., a[n-1]
min_j = 0; // Assume elem 0 (a[0]) is the minimum
for ( j = 1 ; j < a.length ; j++ )
{
if ( a[i] < a[min_j] )
{
min_j = j; // We found a smaller minimum, update min_j
}
}
|
See: click here
|
by starting the serach at the array element a[i].
// Find array element with min. value among a[i], a[i+1], ..., a[n-1]
min_j = i; // Assume elem i (a[i]) is the minimum
for ( j = i+1 ; j < a.length ; j++ )
{
if ( a[j] < a[min_j] )
{
min_j = j; // We found a smaller minimum, update min_i
}
}
// Result: min_j = index of array element with min. value among a[i] ... a[n-1]
|
Note:
|
Previously, we have (see: click here )
Let the values be store in an array "a"
Let n = a.length
for ( i = 0 ; i < n ; i ++ )
{
1. Find the array element with the min. value among a[i], ..., a[n-1];
2. Swap this element with a[i];
}
|
We have just develop an algorithm to solve subproblem 1.
Insert the algorithm and we obtain a more refined (more detailed) algorithm:
Let the values be store in an array "a"
Let n = a.length
for ( i = 0 ; i < n ; i ++ )
{
// Find array element with min. value among a[i], a[i+1], ..., a[n-1]
min_j = i; // Assume elem i (a[i]) is the minimum
for ( j = i+1 ; j < a.length ; j++ )
{
if ( a[j] < a[min_j] )
{
min_j = j; // We found a smaller minimum, update min_j
}
}
// Result: min_j = index of array element with min. value among a[i] ... a[n-1]
2. Swap this element (= a[min_j] !) with a[i];
}
|
OK, one subproblem down, one more to go...
|
|
|
Suppose the starting values are: A = 3.4 B = 5.6 3 way exchange algorithm: help = A; ----> A = 3.4 B = 5.6 help = 3.4 A = B; ----> A = 5.6 B = 5.6 help = 3.4 B = help; ----> A = 5.6 B = 3.4 help = 3.4 |
help = a[i]; a[i] = a[min_j]; a[min_j] = help; |
Previously, we have (see: click here )
Let the values be store in an array "a"
Let n = a.length
for ( i = 0 ; i < n ; i ++ )
{
// Find array element with min. value among a[i], a[i+1], ..., a[n-1]
min_j = i; // Assume elem i (a[i]) is the minimum
for ( j = i+1 ; j < a.length ; j++ )
{
if ( a[j] < a[min_j] )
{
min_j = j; // We found a smaller minimum, update min_j
}
}
// Result: min_j = index of array element with min. value among a[i] ... a[n-1]
2. Swap this element (= a[min_j] !) with a[i];
}
|
We have just develop an algorithm to solve subproblem 2.
Insert the algorithm and we obtain a even more refined algorithm:
Let the values be store in an array "a"
Let n = a.length
for ( i = 0 ; i < n ; i ++ )
{
// Find array element with min. value among a[i], a[i+1], ..., a[n-1]
min_j = i; // Assume elem i (a[i]) is the minimum
for ( j = i+1 ; j < a.length ; j++ )
{
if ( a[j] < a[min_j] )
{
min_j = j; // We found a smaller minimum, update min_j
}
}
// Swap a[i] and a[min_j]
help = a[i];
a[i] = a[min_j];
a[min_j] = help;
}
|
Let the values be store in an array "a" Let n = a.length for ( i = 0 ; i < n ; i ++ ) { // Find array element with min. value among a[i], a[i+1], ..., a[n-1] min_j = i; // Assume elem i (a[i]) is the minimum for ( j = i+1 ; j < a.length ; j++ ) { if ( a[j] < a[min_j] ) { min_j = j; // We found a smaller minimum, update min_j } } // Swap a[i] and a[min_j] help = a[i]; a[i] = a[min_j]; a[min_j] = help; } |
You will determine that we have used the following variables inside the algorithm:
a = the array - type: double[] (we use numbers in the example) i = array index - type: integer j = another array index - type: integer min_j = another array index - type: integer help = stores an array value - type: double (same type as an element of array "a") Note: a[i] is not a new variable a[min_j] is not a new variable |
public class SelSort1 { public static void main(String[] args) { /* ----------------------------------------------------------------- Define the used variables (make sure you have the type correct !) ----------------------------------------------------------------- */ double[] a = { 6.7, 2.3, 7.8, 3.4, 5.6, 4.5, 8.9 }; // 7 elements int i, j, min_j; // Array indices double help; // helper variable for 3-way exchange /* --------------------------------------------------- The selection sort algorithm --------------------------------------------------- */ for ( i = 0 ; i < a.length ; i ++ ) { /* --------------------------------------------------- Find array element with min. value among a[i], a[i+1], ..., a[n-1] --------------------------------------------------- */ min_j = i; // Assume elem i (a[i]) is the minimum for ( j = i+1 ; j < a.length ; j++ ) { if ( a[j] < a[min_j] ) { min_j = j; // We found a smaller minimum, update min_j } } /* --------------------------------------------------- Swap a[i] and a[min_j] --------------------------------------------------- */ help = a[i]; a[i] = a[min_j]; a[min_j] = help; } } } |
How to run the program:
|
|
However:
|
|
We will use the "step-wise refinement" technique later in the course when we discuss the topic "Modular programming using the top-down design methodology".