我在這個考試複習題上是空白的,任何人都可以幫助我入門嗎?在findMinPos中,我對這三個參數感到困惑,我如何訪問數據數組中的節點?即使它是遞歸方法,我可以使用循環嗎?使用遞歸將最小數組的僞代碼轉換爲Java代碼
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 ] + " ]");
}
}
1 )你已經描述了一個問題,但至今沒有提出問題(更不用說具體的可回答的問題)。你的問題是什麼? 2)請參閱[開始編寫程序](http://home.earthlink.net/~patricia_shanahan/beginner.html)以獲取重要提示。 – 2013-05-08 13:25:24
對不起,我現在編輯 – codeAligned 2013-05-08 13:32:27