在Java中以递归方式对数组进行排序,偶数出现在数组前面. [英] Sorting an array recursively in Java with even numbers appearing in front of array.

查看:84
本文介绍了在Java中以递归方式对数组进行排序,偶数出现在数组前面.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一种方法来对数组进行排序,其中偶数排在数组的前面,而奇数排在数组的后面.我的作业要求我使用递归来完成此任务.当我尝试打印排序后的数组时,它只是打印出未排序的数组.我究竟做错了什么?

I'm using a method to sort out an array with even numbers coming out in the front and odd numbers in the back of the array. My assignment dictates that i accomplish this using recursion. When I try to print the sorted out array it just prints out the array unsorted. What am i doing wrong?

left变量始于索引0,而right变量始于索引的末尾.然后将它们都进行比较,如果left是奇数,而right是偶数,则它们交换值.如果left变成偶数,则不会发生交换,它指向数组中的下一个索引.如果right变成奇数,则不会发生交换,它指向数组中的下一个索引.这样做直到所有偶数都在数组的右边.

The left variable starts at index 0 and the right variable starts at the end of the index. They are then both compared, if left is an odd number and right is an even number then they swap values. If left turns out to be an even number then no swap takes place and it points at the next index in the array. If right turns out to be an odd number then no swap takes place and it points at the next index in the array. It does this until all the even numbers are on the right of the array.

我得到

线程"main"中的异常java.lang.StackOverflowError".

"Exception in thread "main" java.lang.StackOverflowError".

import java.util.*;
public class Problem2{

    //i=left 
    //j=right
    //first i tried to shift the whole thing
    //have all even numbers pop to the front of array when even
    public static int[] callme(int[] arry, int left, int right){
        int temp;
        if(left>=right)       //base case, return array
            return arry; 
        else if(arry[left]%2!=0 && arry[right]%2==0){//if match, do the swap
            temp=arry[left];
            arry[left]=arry[right];
            arry[right]=temp;   
            return callme(arry, left++, right--);
        }
        else{
            if(arry[right]%2!=0){//if right side is on odd #, then decrease index
                return callme(arry, left, right--);
            }
            if(arry[left]%2==0){//if left side is on even #, then increase index
                return callme(arry, left++, right);
            }
        } 
        return arry;
    }

    public static void main(String[] args){

        //int index=0;
        int[] arry={3,5,6,8};

        int[] newarry=callme(arry, 0, arry.length-1);
        System.out.print("The new sorted array is: ");
        for(int i=0; i<newarry.length;i++){
            System.out.print(newarry[i]+" ");
        } 
    }
}

推荐答案

left++left + 1不同.如果要使用一个高一个left参数递归地调用该方法,请使用left + 1进行调用.

left++ is not the same as left + 1. If you want to call the method recursively with a left argument that is one higher, then call it with left + 1.

在此声明中:

  return callme(arry, left++, right--);

它的工作方式是这样的:程序保存>的 current 值,将left加1,然后使用之前保存的值作为递归调用的参数增加. right--参数的工作方式相同.因此,当callme调用自身时,它使用与调用时完全相同的参数进行调用.因此,您永远都无法掌握基本情况.

the way it works is this: The program saves the current value of left, adds 1 to left, but then uses the value saved before it was incremented as the argument to the recursive call. The right-- argument works the same way. So when callme calls itself, it's calling itself with the exact same arguments it was called with. So you never get to the base case.

(关于递归,您应该了解的其他事情:递归方法每次调用自身时,都会拥有自己的局部变量副本,包括参数.因此,即使第一个方法增加了left,也没有影响递归调用left会是什么,因为递归方法具有自己的left.)

(Something else you should know about recursion: Each time a recursive method calls itself, it will have its own copy of the local variables, including the parameters. So even though the first method increases left, that has no impact on what left will be when it's called recursively, because the recursive methods have their own left.)

这篇关于在Java中以递归方式对数组进行排序,偶数出现在数组前面.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆