2016-01-04 89 views
0

問題的新的位置是我有2個陣列int input2[]={5,1,9,3,8};int input3[]={2,0,3,6,1};
已排序陣列輸入2使用Arrays.sort(input2);現在我要放置在陣列inputs3元素作爲每個元素的新位置陣列的input2排序第一陣列和佈置第二陣列元件按照第一

輸入數組預排序 - 5,1,9,3,8 輸入數組排序後 - 1,3,5,8,9 現在陣列輸入3的元件也應該改變爲每位置陣列輸入2 preSort 2,0,3,6,1 Post sort 0,6,2,1,3

雖然我寫的代碼,而是尋找一個最佳的解決方案enter code here

private static int[] swap(int[] arr, int i, int j,int [] arr2) { 
    arr2[i]=arr[j]; 
    return arr2; 
} 

public static void main(String[] args) { 
    int input2[]={5,1,9,3,8}; 
    int input3[]={2,0,3,6,1}; 
    int []temp=input2.clone(); 
    int []input4=input3.clone(); 
    Arrays.sort(input2); 

    for(int i=0;i<=input2.length-1;i++){ 
     for(int j=0;j<=input2.length;j++){ 
      if(input2[i]==temp[j]){ 
       input4= swap(input3,i,j,input4); 
       break; 
      } 
     } 
    }   
} 
+0

是否有一個原因,你不會使用一個地圖,而不是兩個數組? – TangledUpInBlue

+0

如果一個數組有重複的元素會發生什麼? –

+0

不要這樣做。創建一個包含兩個整數的類,並且在第一個類中具有可比性。創建該類的數組。對它進行排序,並且您還將排序第二個整數。 – RealSkeptic

回答

0

您可以更改您的交換方式進行切換,你想在同一時間一下子掉所有列表的元素。

private static int[] swap(int i, int j,List<int[]> listOfArrays) { 
     for(int[] array : listOfArrays) { 
      int tmp = array[i]; 
      array[i]=array[j]; 
      array[j]=tmp; 
     } 
     return arr2; 
} 

但是你將不得不寫你自己的排序。

另一種選擇是使用函數接口並交換第三個數組進行比較。與

Arrays.sort(T[] a, Comparator<? super T> c) 

方法。

換句話說,排序後它已經晚了,你必須在排序時做。或者初始數組需要重構。

3

推測這些值(input2input3)彼此之間有某種關係?就像,它們是某些點的x,y座標,或者類似的東西?如果是這樣,你應該把它們放到一些對象中,然後對這些對象進行排序。

public class Point { 
    private final int x; 
    private final int y; 

    public Point(int x, int y) { 
     this.x = x; 
     this.y = y; 
    } 

    public int getX() { 
     return x; 
    } 

    public int getY() { 
     return y; 
    } 

    @Override 
    public String toString() { 
     return x + ", " + y; 
    } 
} 

public void sortPoints() { 
    int xs[] = { 5, 1, 9, 3, 8 }; 
    int ys[] = { 2, 0, 3, 6, 1 }; 
    List<Point> points = new ArrayList<>(); 
    for (int i = 0; i < xs.length; ++i) { 
     points.add(new Point(xs[i], ys[i])); 
    } 

    Collections.sort(points, (a, b) -> a.getX() - b.getX()); 

    points.forEach(p -> System.out.println(p.getY())); 
} 
+0

不使用Map的原因是找到使用數組的唯一方法,那就是這個問題的要求 – GSK

+0

@GSK這個評論是針對Sazzad的回答嗎?我的解決方案不使用'Map'。 –

+0

@ TangledUpInBlue – GSK

1

除了使一類的其他解決方案,替代方案的樣子,

public class SortTesting { 
    public static void main(String[] args) { 
     HashMap map = new HashMap(); 
     TreeMap sortedMap = new TreeMap(); 

     map.put(5, 2); 
     map.put(1, 0); 
     map.put(9, 3); 
     map.put(3, 6); 
     map.put(8, 1); 

     sortedMap.putAll(map); 
     System.out.println("results: " + sortedMap); 
    } 
} 
0

假設你有Java的8,你可以(因爲拉姆達比較沒有按使用整數」產生整數索引的數組t與原語一起工作),根據input2對索引數組進行排序,然後根據索引數組重新排序input2和input3。

package x; 
import java.util.Arrays; 
public class x { 
    public static void main(String[] args) { 
     int input2[]={5,1,9,3,8}; 
     int input3[]={2,0,3,6,1}; 
     // generate array of indices 
     Integer[] I = new Integer [input2.length]; 
     for(int i = 0; i < I.length; i++) 
      I[i] = i; 
     // sort array of indices according to input2 
     Arrays.sort(I, (i, j) -> input2[i]-input2[j]); 
     // reorder input2 and input3 in place using sorted indices 
     // also reorder indices back to 0 to length-1 
     // time complexity is O(n) 
     for(int i = 0; i < I.length; i++){ 
      if(i != I[i]){ 
       int t2 = input2[i]; 
       int t3 = input3[i]; 
       int j; 
       int k = i; 
       while(i != (j = I[k])){ 
        input2[k] = input2[j]; 
        input3[k] = input3[j]; 
        I[k] = k; 
        k = j; 
       } 
       input2[k] = t2; 
       input3[k] = t3; 
       I[k] = k; 
      } 
     } 
     // display result 
     for (int i = 0; i < input2.length; i++) { 
      System.out.println("input2 " + input2[i] + " input3 " + input3[i]); 
     } 
    } 
}