public class SelSort1
{
public static void main(String[] args)
{
/* -----------------------------------------------------------------
Define the used variables (make sure you have the type correct !)
----------------------------------------------------------------- */
double[] a = { 2.3, 3.4 , 4.5, 5.6, 6.7, 7.8, 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 = i; // 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;
}
}
}
|
Fact:
|
The way the Selection Sort Algorithm is written, we need to do the following to sort 2 arrays a and b:
public class SelSort2
{
public static void main(String[] args)
{
double[] a = { 2.3, 3.4 , 4.5, 5.6, 6.7, 7.8, 8.9 }; // 7 elements
double[] b = { 6.7, 4.5, 9.9}; // Second array
int i, j, min_j; // Array indices
double help; // helper variable for 3-way exchange
/* ---------------------------------------------------
Print array before sorting
--------------------------------------------------- */
for ( i = 0 ; i < a.length ; i++ )
{
System.out.println( a[i] );
}
/* ---------------------------------------------------
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;
}
System.out.println( "Array after Selection Sort:");
/* ---------------------------------------------------
Print array after sorting
--------------------------------------------------- */
for ( i = 0 ; i < a.length ; i++ )
{
System.out.println( a[i] );
}
/* ============= Sort array b ================= */
System.out.println();
/* ---------------------------------------------------
Print array before sorting
--------------------------------------------------- */
for ( i = 0 ; i < b.length ; i ++ )
{
System.out.println( b[i] );
}
/* ---------------------------------------------------
The selection sort algorithm
--------------------------------------------------- */
for ( i = 0 ; i < b.length ; i++ )
{
/* ---------------------------------------------------
Find array element with min. value among
b[i], b[i+1], ..., b[n-1]
--------------------------------------------------- */
min_j = i; // Assume elem i (b[i]) is the minimum
for ( j = min_j+1 ; j < b.length ; j++ )
{
if ( b[j] < b[min_j] )
{
min_j = j; // We found a smaller minimum, update min_j
}
}
System.out.println("i = " + i + ", min-index = " + min_j );
/* ---------------------------------------------------
Swap b[i] and b[min_j]
--------------------------------------------------- */
help = b[i];
b[i] = b[min_j];
b[min_j] = help;
}
System.out.println( "Array after Selection Sort:");
/* ---------------------------------------------------
Print array after sorting
--------------------------------------------------- */
for ( i = 0 ; i < b.length ; i ++ )
{
System.out.println( b[i] );
}
}
}
|
Conclusion:
|
|
|
|
|
|
|
Example:
public class Input1
{
/* -------------------------------------------------------
Pass information to method with parameter variable x
------------------------------------------------------- */
public static double Square( double x )
{
double out;
out = x * x; // x contains input information
return(out);
}
public static void main( String[] args )
{
double a, b;
a = 4.0;
b = Square( a ); // The value in a is passed to the parameter var x
System.out.println( b) ;
}
}
|
Example Program:
(Demo above code)
                       
                       
How to run the program:
|
|
Example:
public class Input2
{
/* -------------------------------------------------------
Pass information to method with class variable x
------------------------------------------------------- */
public static double x;
/* ----------------------------------------------------------
Notice that the method does not have any parameter var !
---------------------------------------------------------- */
public static double Square( )
{
double out;
out = x * x; // x contains input information
return(out);
}
public static void main( String[] args )
{
double a, b;
a = 4.0;
x = a; // The value in a is passed using class var x
b = Square( );
System.out.println( b) ;
}
}
|
Example Program:
(Demo above code)
                       
                       
How to run the program:
|
|
|
Example: (Java)
public class Output1
{
/* -------------------------------------------------------
Pass information out of method with RETURN statement
------------------------------------------------------- */
public static double Square( double x )
{
double out; // out contains some info. inside method
out = x * x;
return(out); // Info. in out is returned (passed outside)
}
public static void main( String[] args )
{
double a, b;
a = 4.0;
b = Square( a ); // The return value is saved in b
System.out.println( b) ;
}
}
|
This mechanism is (trivially) available in Java
How to run the program:
|
|
Example:
public class Output2
{
/* -------------------------------------------------------
Pass information out of method with CLASS variable
------------------------------------------------------- */
public static double out;
public static void Square( double x )
{
out = x * x; // Store the result in CLASS var "out"
}
public static void main( String[] args )
{
double a, b;
a = 4.0;
Square( a ); // The return value is saved in CLASS var out
b = out; // Get the result !
System.out.println( b) ;
}
}
|
Example Program:
(Demo above code)
                       
                       
How to run the program:
|
|
Example: (C++)
void Square( double & out, double x )
{
out = x * x; // Store result in output variable
}
int main(int argc, char ** argv )
{
double a, b;
a = 4.0;
Square( b, a ); // b is passed by reference and can be modified by
// the method so that the result can be returned
cout << "a = " << a << " --- squared = " << b << endl;
}
|
How to run the program:
|
Note:
|
|
|
|
public class SortingTools
{
public static void SelectionSort(double[] a)
{
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;
}
}
}
|
Explanation:
|
Example: if I want to sort the arrays a and b , all I need to do is:
public class SelSort3
{
public static void main(String[] args)
{
double[] a = { 2.3, 3.4 , 4.5, 5.6, 6.7, 7.8, 8.9 }; // 7 elements
double[] b = { 6.7, 4.5, 9.9}; // Second array
int i;
/* ---------------------------------------------------
Print array before sorting
--------------------------------------------------- */
for ( i = 0 ; i < a.length ; i++ )
{
System.out.println( a[i] );
}
SortingTools.SelectionSort(a);
System.out.println( "Array after Selection Sort:");
/* ---------------------------------------------------
Print array after sorting
--------------------------------------------------- */
for ( i = 0 ; i < a.length ; i++ )
{
System.out.println( a[i] );
}
/* ============= Sort array b ================= */
System.out.println();
/* ---------------------------------------------------
Print array before sorting
--------------------------------------------------- */
for ( i = 0 ; i < b.length ; i ++ )
{
System.out.println( b[i] );
}
SortingTools.SelectionSort(b);
System.out.println( "Array after Selection Sort:");
/* ---------------------------------------------------
Print array after sorting
--------------------------------------------------- */
for ( i = 0 ; i < b.length ; i ++ )
{
System.out.println( b[i] );
}
}
}
|
How to run:
|
The SelectionSort method can do this because:
|