Java 将煎饼排序的循环实现转换为递归实现

Java 将煎饼排序的循环实现转换为递归实现,java,recursion,Java,Recursion,我正在学习递归,想把我的循环转换成递归函数?这段代码的正确答案应该是什么(假设我已经编写了翻转方法来反转数组中的元素)?谢谢 /** * Sorts an array of integers by repeatedly reversing * subranges within the array. Prints the flip sequence. */ public static void sort( int[] array) { int size = array.

我正在学习递归,想把我的循环转换成递归函数?这段代码的正确答案应该是什么(假设我已经编写了翻转方法来反转数组中的元素)?谢谢

/**
 * Sorts an array of integers by repeatedly reversing 
 * subranges within the array. Prints the flip sequence. 
 */ 
public static void  sort( int[] array)
{   
    int size = array.length;
    if (!Ordered(array, size)){
        for(int i = size-1; i > 0; i--)
        {
            int j = findMax(array, 0, i );
            int flipPosition;

            if( j != i )
            {
                if( j != 0 ) {
                    flip( array, 0, j );
                    flipPosition = size-j;
                    System.out.print( flipPosition + " " );
                }

                flip( array, 0, i );
                flipPosition = size-i;
                System.out.print( flipPosition + " " );
            }   
        }
    }
    System.out.println( 0 );
}

如果这是家庭作业,我不想写你的程序,如果这是私人的,我也不想破坏你的乐趣,所以我在Ruby中实现了一个有大量注释的递归解决方案。它基本上归结为进行必要的翻转,将最大元素移动到数组的末尾,然后将相同的逻辑应用于通过排除最大值创建的子数组。递归调用返回已排序的子数组,因此只需追加最大值并将结果返回行。递归的基本情况是,当您开始处理单个元素时,只需返回它

# Function to find the index of the max element in an array.
# In case of ties, returns the lowest index
def max_index(a)
  a.each_index.inject { |memo, i| a[i] > a[memo] ? i : memo }
end

def pancake_sort(a)
  # An array with 0 or 1 elements is sorted (trivially),
  # just return it.  This is the base case for the recursion.
  return a if a.length < 2

  # Find location of the max, express it as the n'th element
  n = max_index(a) + 1

  # Flip the stack of the first n elements (max is last)
  # to put the max at the front, concatenate it with the
  # rest of the array, then flip the entire result.  This
  # will put max at the end. However, don't bother with all
  # that flipping if max was already at the end.
  a = (a.take(n).reverse + a.drop(n)).reverse if n < a.length

  # Recursively apply the logic to the subarray that excludes
  # the last (max) element.  When you get back the sorted
  # subarray, tack the max back onto the end
  return pancake_sort(a.take(a.length - 1)) << a[-1]
end

# Create an array of 20 random numbers between 0 and 99    
ary = Array.new(20) { rand(100) }
# Display the result
p ary
# Display the result after sorting
p pancake_sort(ary)

# Sample output:
# [70, 19, 95, 47, 87, 49, 53, 8, 89, 33, 22, 85, 91, 87, 99, 56, 15, 27, 75, 70]
# [8, 15, 19, 22, 27, 33, 47, 49, 53, 56, 70, 70, 75, 85, 87, 87, 89, 91, 95, 99]
函数查找数组中max元素的索引。 #如果是平局,则返回最低索引 def max_索引(a) a、 每个| index.inject{| memo,i | a[i]>a[memo]?i:memo} 结束 def煎饼分类(a) #对包含0或1个元素的数组进行排序(一般), #把它还给我就行了。这是递归的基本情况。 如果a.length<2,则返回a #找到最大值的位置,将其表示为第n个元素 n=最大指数(a)+1 #翻转前n个元素的堆栈(最大值为最后一个) #要将最大值放在前面,请将其与 #数组的其余部分,然后翻转整个结果。这 #会把max放在最后。然而,不要为所有的事情烦恼 #如果max已经到了终点的话。 a=(a.take(n)。反转+a.drop(n))。如果nreturn pancake_sort(a.take(a.length-1))您尝试使用什么排序算法?@Logan我的方法使用pancake sort,它在翻转数组之前识别数组中的最大数,并按从左到右的升序排序。我不知道如何把它修改成递归函数。递归是不可能的。我主要用于排序的是一种称为快速排序的算法。我仔细研究并找出我的“基本情况”是什么,然后从那里开始工作。在我学习递归时,有一件事对我很有帮助:隔离基本情况和操作。必须将定义作为基础(“基础案例”)。您的定义允许您“退出”递归。在其他情况下,您执行一个操作并再次调用自己继续执行一个操作,直到达到基本情况。您想递归地解决一个问题吗?是的,我明白您的意思,写下了我的代码,但它在某个地方仍然有问题,您能帮我看看吗?如果您希望人们帮助您找出代码不起作用的原因,您需要在问题中包含代码。这意味着所有的代码,包括您正在调用的小实用程序函数。只是觉得我的代码太长了。给你:。当我试着运行测试时,它得到的是StackFlow错误还是Java IndexOutOf Bound。我不想在这里大惊小怪,但我希望你意识到你在试图转移这个问题。如前所述,您询问了如何使用递归实现您发布的迭代算法。现在,您要问的是,您没有提供的实现有什么问题,并且仍然不是问题的一部分。你有几个问题。首先,对于基本情况
if(i),您缺少
return
语句