简体   繁体   English

我如何排序2个并行数组

[英]How do i sort 2 parallel arrays

I have 2 arrays, 1 1dimensional and another 2dimensional that i have to sort. 我有2个数组,分别是1个1维和另一个2维。 The problem is that I don't know how to sort them in parallel. 问题是我不知道如何对它们进行并行排序。 I already sorted the 1d array according to alphabetical order but I want to sort the 2d array accordingly. 我已经按照字母顺序对1d数组进行了排序,但是我想对2d数组进行相应的排序。 So I need to send these two arrays to a sort function that will return the two arrays sorted in ascending order while the elements in the 1d array "arr1" keep their corresponding elements in the 2d array "arr2". 因此,我需要将这两个数组发送到sort函数,该函数将返回按升序排序的两个数组,而1d数组“ arr1”中的元素将其对应的元素保留在2d数组“ arr2”中。

What I have so far: 到目前为止,我有:

import java.io.*;
import java.util.Scanner;
import java.util.Arrays;

public class JAVAhw15 {
public static void main(String args[]) throws IOException {

//required for input    
BufferedReader usrInput = new BufferedReader(
new InputStreamReader(System.in));

Scanner user_input = new Scanner( System.in );

int row = 10;
int col = 3;
int size = 10;

//Declare & Initialize
int arr2[][] = new int[row][];
String arr1[] = new String[size];

//construct 2d arr
for(int i=0; i<row; i++)
{
    arr2[i] = new int[col];
}

//Filling 1d array arr1
System.out.println("Enter 10 names please: ");
for(int i=0; i<size; i++)
{
    arr1[i] = user_input.next();
}


//Filling 2d array arr2
System.out.println("Enter 3 telephone numbers for each name entered");
for(int i=0; i<row; i++)
{
    for(int j=0; j<col; j++)
    {
        System.out.println(arr1[i]+"'s Tel "+j+" is: ");
        arr2[i][j] = Integer.parseInt(usrInput.readLine());
    }
}

System.out.println("After sort :");

    String merged[] = new String[arr1.length];
    for (int i=0; i< arr1.length; i++) {
        merged[i] = arr1[i]+"=="+arr2[i];
    }
    Arrays.sort(merged);
    for(int i = 0; i < merged.length; i++) {
    System.out.println( (i+1) + ". "+
    merged[i].split("==")[0] + "\t\t" +   
    merged[i].split("==")[1]);
    }
}
}

I can see two solutions : 我可以看到两种解决方案:

  • you create a class that merges the data you are storing in two separate lists. 您创建一个类,将存储在两个单独列表中的数据合并。 This way, you will have only one list and it will be easier for you to do waht you want. 这样,您将只有一个列表,并且您可以轻松进行任何操作。

  • you write yourself a classical sort like quick sort, with a small variant : you will not only sort the aray but also return an object representing the permutation that was applied to the array to sort it. 您可以编写自己的经典排序方法,例如快速排序方法,但有一个小的变化:您不仅可以对aray进行排序,还可以返回一个对象,该对象代表应用于数组以对其进行排序的排列。 Then, you just have to write a method to apply the permutation on the other array. 然后,您只需要编写一个方法即可在另一个数组上应用置换。

I would prefer the first solution but since you've shown interest for the second one, here is an example of how you can do it : 我更喜欢第一个解决方案,但是由于您已经对第二个解决方案表现出兴趣,因此,这里有一个如何实现的示例:

import java.util.Arrays;
import java.util.Random;

public class QuickSort {
    private static Random   rd = new Random();

    public static <T extends Comparable<T>> Integer[] sort(T[] arr) {
        Integer[] perm = new Integer[arr.length];
        for (int i=0 ; i<perm.length ; i++)
            perm[i] = i;
        sort(arr,perm,0,arr.length - 1);
        return perm;
    }

    private static <T extends Comparable<T>> void sort(T[] arr, Integer[] perm, int start, int end) {
        if (start < end) {
            int index = pivot(arr,perm,start,end);
            sort(arr,perm,start,index - 1);
            sort(arr,perm,index + 1,end);
        }
    }

    private static <T extends Comparable<T>> int pivot(T[] arr, Integer[] perm, int start, int end) {
        int index  = start + rd.nextInt(end - start + 1);
        T   pivot  = arr[index];
        int result = start;
        swap(arr,perm,index,end);

        for (int i=start ; i<end ; i++) 
            if (arr[i].compareTo(pivot) < 0) 
                swap(arr,perm,i,result++);
        swap(arr,perm,result,end);
        return result;
    }

    private static <T> void swap(T[] arr, Integer[] perm, int i, int j) {
        swap(arr,i,j);
        swap(perm,i,j);
    }

    private static <T> void swap(T[] arr, int i, int j) {
        T tmp  = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;       
    }

    public static void main(String[] args) {
        Integer[] arr = { 1,3,4,6,9,2,8 };
        Integer[] res = sort(arr);
        System.out.println("sorted array : " + Arrays.toString(arr) + ", permutation : " + Arrays.toString(res));
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM