Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用递归将数组最小值的伪代码转换为Java代码_Java_Arrays_Recursion - Fatal编程技术网

使用递归将数组最小值的伪代码转换为Java代码

使用递归将数组最小值的伪代码转换为Java代码,java,arrays,recursion,Java,Arrays,Recursion,我没有回答这个复习题,有人能帮我开始吗?在findMinPos中,我被这三个参数弄糊涂了,我如何访问数据数组中的节点?即使是递归方法,我也可以使用循环吗 public class ArraySwapMin { public static void swapMin( int[] data, int cur ) { int min = findMinPos( data, cur, cur ); ////////////////////////

我没有回答这个复习题,有人能帮我开始吗?在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 ] + " ]" );
    }
}
公共类ArraySwapMin
{
公共静态void swapMin(int[]数据,int cur)
{   
int min=findMinPos(数据、电流、电流);
/////////////////////////////////////////////////////////////
//将最小位置值与cur位置值交换
////////////////////////////////////////////////////////////////
} 
/**
*检查“数据”中从位置“开始”到阵列结束的节点。
*查看数组此部分中的任何值是否小于最小值
*到目前为止找到的值(直到“cur”位置)。
*/ 
私有静态int findMinPos(int[]数据、int cur、int minPosSoFar)
{
//////////////////////////////////////////////////////////////
//将此条目的值(如果它是有效条目)与
//条目“minPosSoFar”中的值。如果该值小于
//这个条目现在是“minPosSoFar”。
//对数组的其余部分递归。
///////////////////////////////////////////////////////////////
返回minPosSoFar;
}
/**
*单元测试仪
*/
公共静态void main(字符串[]args)
{
int[]数据={12,3,10,5,1,8};
整数计数=0;
System.out.println(“+ArraySwapMin+;
printArray(“起始数组”,数据);
对于(int i=0;i
他们给了你伪代码。照上面说的做就行了。首先将说明更改为逐步

  • 将此条目的值(如果它是有效条目)与条目“minPosSoFar”中的值进行比较
  • 如果此值小于此值,则此条目现在为“minPosSoFar”
  • 对数组的其余部分递归
  • 因此:

    private static int findMinPos(int[]数据、int cur、int minPosSoFar)
    {
    if(cur
    因为这是为了上学,我不想为你做所有的事情,希望这能给你一个好主意。在
    swapMin()
    中,你必须将当前位置切换到最小的位置

    public static void swapMin( int[] data, int cur )  
    {   
        int min = findMinPos( data, cur, cur );
    
        int minValue = data[min];
        data[min] = data[cur];
        data[cur] = minValue;
    }
    
    最小值将在
    findMinPos()
    中递归确定。递归编程的全部思想是使用内部方法调用的返回值,而不是使用循环。您需要的是一个整体中断条件(在您的例子中是数组的长度)和多个返回语句

    这里的这一个会起作用:

    private static int findMinPos( int[] data, int cur, int minPosSoFar )
    {
        if(cur < data.length)
        {
            if(data[cur] < data[minPosSoFar]) // set new minimum to position cur
            {
                return findMinPos(data, cur + 1, cur);
            }
            else // keep old minimum
            {
                return findMinPos(data, cur + 1, minPosSoFar);
            }
        }
    
        return minPosSoFar;
    }
    
    private static int findMinPos(int[]数据、int cur、int minPosSoFar)
    {
    if(电流<数据长度)
    {
    if(data[cur]
    由于if-else块中的多个return语句使代码冗长而混乱,因此可以这样缩短代码

    private static int findMinPos( int[] data, int cur, int minPosSoFar )
    {
        if(cur < data.length)
        {
            return (data[cur] < data[minPosSoFar]) ? 
                findMinPos(data, cur + 1, cur) :
                findMinPos(data, cur + 1, minPosSoFar);
        }
    
        return minPosSoFar;
    }
    
    private static int findMinPos(int[]数据、int cur、int minPosSoFar)
    {
    if(电流<数据长度)
    {
    返回(数据[cur]
    1)您已经描述了一个问题,但到目前为止还没有提出任何问题(更不用说具体的、可回答的问题了)。你的问题是什么?2) 请参阅以获取最佳提示。抱歉,我现在将进行编辑
    private static int findMinPos( int[] data, int cur, int minPosSoFar )
    {
        if(cur < data.length)
        {
            return (data[cur] < data[minPosSoFar]) ? 
                findMinPos(data, cur + 1, cur) :
                findMinPos(data, cur + 1, minPosSoFar);
        }
    
        return minPosSoFar;
    }