我在这个考试复习问题上空白,有人可以帮我开始吗?在 findMinPos 中,我被三个参数弄糊涂了,我如何访问数据数组中的节点?即使是递归方法,我也可以使用循环吗?
public class ArraySwapMin
{
public static void swapMin( int[] data, int cur )
{
int min = findMinPos( data, cur, cur );
/////////////////////////////////////////////////////////////
// swap the min position value with the one in the cur position
////////////////////////////////////////////////////////////////
}
/**
* Check the nodes in "data" from position "start" to the end of the array.
* to see if any value in this part of the array is less than the min
* value found so far (up to the "cur" position).
*/
private static int findMinPos( int[] data, int cur, int minPosSoFar )
{
//////////////////////////////////////////////////////////////
// Compare this entry's value (if it is a valid entry) with the
// value in the entry "minPosSoFar". If this value is less, then
// this entry is now the "minPosSoFar".
// Recurse for the rest of the array.
///////////////////////////////////////////////////////////////
return minPosSoFar;
}
/**
* unit tester
*/
public static void main( String[] args )
{
int[] data = { 12, 3, 10, 5, 1, 8 };
int count = 0;
System.out.println( "++++++++++++++++ ArraySwapMin ++++++++++++++++" );
printArray( "starting array ", data );
for ( int i = 0; i < data.length - 1; i++ )
{
swapMin( data, i );
printArray( "swap Min with " + i, data );
}
}
public static void printArray( String label, int[] data )
{
System.out.print( label + ": [ " );
for ( int i = 0; i < data.length - 1; i++ )
System.out.print( data[ i ] + ", " );
System.out.println( data[ data.length - 1 ] + " ]" );
}
}