2017-06-22 41 views
-2

如何將數組排序爲偶數和賠率之間的數字出現順序,例如[2 1 4 6 3 9 8]使用遞歸來提供這個[1 3 9 2 4 6 8]?一些幫助或使用Java的例子將不勝感激。即使奇數排序,首先按出現順序保持賠率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; 
    } 
+2

向我們展示你的努力。 –

+1

歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [在主題](http://stackoverflow.com/help/on-topic)和[如何提問](http://stackoverflow.com/help/how-to-ask)適用於此處。 StackOverflow不是一個設計,編碼,研究或教程服務。 – Prune

+1

[「有人能幫我嗎?」是不是一個有效的SO問題](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question)。這通常表明,你需要的是半個小時的時間與當地的導師,或通過一個教程,而不是堆棧溢出。 – Prune

回答

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 
+0

我會試一試。我可能已經走向複雜與我的。 – charonodaemon

0

這裏是可能的方法之一:

@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); 
    } 
+0

如果我理解正確,這看起來是使用鏈表。我沒有用到這些,但遞歸調用應該是一個巨大的幫助!謝謝! – charonodaemon

+0

糾正這一點,它看起來像你使用增強的for循環。 – charonodaemon

+0

本例使用ArrayList實現,而不是LinkedList。在'Java 8'中,這種方法將使用'List.sort(Comparator c)'接口方法的默認實現。複雜性:'lg(n)'和算法稱爲'合併排序' –

相關問題