【问题标题】:Even odd sort, keep odds out first in order of appearance recursion Java偶数排序,按出现递归Java的顺序先排除赔率
【发布时间】:2017-06-22 16:03:32
【问题描述】:

我如何将数组排序为偶数和奇数之间的数字出现顺序相同,例如 [2 1 4 6 3 9 8] 使用递归来提供此 [1 3 9 2 4 6 8]?非常感谢一些使用 Java 的帮助或示例。

这段代码做了类似的工作,只是很难解决手头的问题。

Sorting an array recursively in Java with even numbers appearing in front of array.

这是关于如何处理并排的偶数问题的部分。不得使用循环。

public static int[] SplitOdds(int input[], int left, int next){     
    int[] temp = new int[1];
    int[] temp2 = new int[1];
    if (input.length == 1)
        {return input;}
    if (input.length ==2){
        temp[0] = input[left];
        input[left] = input[next];
        input[next] = temp[0];
        return input;}
    if (input[left]%2!=0 && input[next]%2==0)
        {return SplitOdds(input, left+1, next+1);}
    if(input[left]%2==0 && input[next]%2!=0){
        temp[0] = input[left];
        input[left] = input[next];
        input[next] = temp[0];
        return  SplitOdds(input, left+1, next+1);}
    if(input[left]%2!=0&&input[next]%2!=0)
        return SplitOdds(input, left+1, next+1);
    if(input[left]%2==0&&input[next]%2==0){
        int j = next;
        temp[0]=input[left];
        return SplitOdds(input, left, next+1);}
        else if(input[next]%2!=0){
            //temp2[0]=input[j];
            //input[left]=input[next];
            input[left+1] = temp2[0];       
        return SplitOdds(input, left, next+1);}
    else if(next == input.length)
    {left = 1; 
    next = 2;}
    return input;
    }

【问题讨论】:

  • 向我们展示你的努力。
  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 on topichow to ask 在这里申请。 StackOverflow 不是设计、编码、研究或教程服务。
  • "Can Someone Help Me?" is not a valid SO question。这通常表明您需要的是与当地导师一起半小时或完成教程,而不是 Stack Overflow。

标签: java arrays sorting recursion


【解决方案1】:

我会尝试这样的:

Integer[] array = new Integer[]{2, 1, 4, 6, 3, 9, 8}; // what to sort?

// The sorting logic is the same for anything that is Comparable. Only the criteria (ie. what makes a value bigger than another?) changes
Arrays.sort(array, new Comparator<Integer>() {
    public int compare(Integer i1, Integer i2) {
        if(i1%2 != i2%2) { // comparing an odd number with an even?
            return i1%2==0? 1:-1; // then odd comes before even
        }

        return i1 - i2; // else biggest number comes after smaller ones
    }
});

System.out.println(Arrays.toString(array)); // basic display

【讨论】:

  • 我会试一试的。我可能已经对我的事情变得复杂了。
【解决方案2】:

这是一种可能的方法:

  @Test
  public void test() {
    List<Integer> testSource = Arrays.asList(1, 8, 2, 4, 6, 9, 5, 3);

    Collections.sort(testSource, new Comparator<Integer>() {
      @Override
      public int compare(Integer o1, Integer o2) {
        if (o1 % 2 == o2 % 2) {
            return o1.compareTo(o2);
        } else {
          return o1 % 2 == 1 ? -1 : 1;
        }
      }
    });
    assertEquals(Arrays.asList(1, 3, 5, 9, 2, 4, 6, 8), testSource);
  }

【讨论】:

  • 如果我理解正确,这看起来是使用链表。我还没有到使用这些的地步,但是递归调用应该是一个巨大的帮助!谢谢!
  • 更正一下,看起来您正在使用增强的 for 循环。
  • 此示例使用 ArrayList 实现,而不是 LinkedList。在Java 8 中,此方法将使用List.sort(Comparator&lt;? super E&gt; c) 接口方法的默认实现。复杂度:lg(n) 和称为Merge Sort的算法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
  • 2011-03-25
  • 1970-01-01
  • 2019-05-14
  • 2014-02-27
  • 2020-09-03
相关资源
最近更新 更多