2016-01-04 90 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]); 
     } 
    } 
}