Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Arrays 使用递归分离整数数组中的奇偶数_Arrays_Algorithm_Recursion - Fatal编程技术网

Arrays 使用递归分离整数数组中的奇偶数

Arrays 使用递归分离整数数组中的奇偶数,arrays,algorithm,recursion,Arrays,Algorithm,Recursion,我正在做一个算法练习,要求重新排列整数数组,将所有偶数值元素放在奇数值元素之前 我想了一会儿,想出了以下伪代码: int[] Rearrange(int [] arr) { if arr.length=1 return arr; if arr[0] is even return arr[0] followed by Rearrange(arr.subarray(1,arr.length)) else return Rearrange(arr.sub

我正在做一个算法练习,要求重新排列整数数组,将所有偶数值元素放在奇数值元素之前

我想了一会儿,想出了以下伪代码:

int[] Rearrange(int [] arr)
{

   if arr.length=1
     return arr;

   if arr[0] is even
    return arr[0] followed by Rearrange(arr.subarray(1,arr.length))
   else
    return Rearrange(arr.subarray(1,arr.length)) followed by arr[0]

}

我有点担心我上面提出的解决方案,因为我需要在每个递归周期中执行一个复制操作,这很昂贵。敬请专家指教,谢谢

递归非常昂贵,而且您的方法将创建大量额外副本。有时递归会产生一个优雅的解决方案,而另一些时候它绝对是一个错误的工具。对于工作案例来说,这是一个错误的工具

相反,编写一个方法来保存头索引和尾索引。初始化指向数组开头的头指针和指向结尾的尾索引

在每次传递时,循环遍历列表顶部的项目,查找奇数值。当您找到一个时,停止,然后从末尾开始寻找一个偶数值,向后看。找到一个后,切换两个值(使用第三个int作为临时存储)。永远重复。当头部和尾部索引满足时,就完成了

大概是这样的:

int head_index = 0;
int tail_index = array.count;
int temp;
while (true)
{
  //Find the next odd number at the front of the array.
  while (array[head_index] %2==0) && head_index < tail_index)
    head_index++;

  //Find the next even number at the end of the array.
  while (array[tail_index]%2==1 && head_index < tail_index)
    tail_index--;

  //If the pointers meet, we're done
  if (head_index <= tail_index)
    break;

  //Swap the items at the current indexes
  temp = array[head_index];
  array[head_index] = array[tail_index];
  array[tail_index] = temp;
}
int head\u index=0;
int tail_index=array.count;
内部温度;
while(true)
{
//在数组前面找到下一个奇数。
while(数组[head\u index]%2==0)和&head\u index如果(head_index即使这个问题已经得到了回答,我还是把解决这个问题的递归版本放在这里,让那些想知道为什么递归是解决这个问题的糟糕方法的人知道

public static int[] segregate(int[] array, int left) {

    int leftIndex = left;

    if(left == array.length) {
        return array;
    }

    for(int i = leftIndex + 1; i < array.length; i++) {
        if(array[leftIndex] % 2 == 1) {
            if(array[i] % 2 == 0) {
                int temp = array[leftIndex];
                array[leftIndex] = array[i];
                array[i] = temp;
            }
        }
    }

    return segregate(array, leftIndex + 1);
}
公共静态int[]分隔(int[]数组,int左){
int leftIndex=左;
if(left==array.length){
返回数组;
}
for(int i=leftIndex+1;i

从代码中可以看出,该方法将自称为n次。当考虑到方法中的for循环的复杂性为O(n)时,递归的总复杂度将为O(n×2)。这比非递归解决方案更糟糕。

您的解决方案从不递归。@BobJarvis这是伪代码,所以我在做假设,但它看起来确实是递归的。重排基金在第二个if语句的两个分支中调用自己:First if arr[0]是偶数,在else的情况下也是如此。这是递归tho的一个糟糕用法。@duncac-自从我发表了我的原始评论以来,这个问题已经被编辑以显示对
重新排列的调用。