【问题标题】:Sorting three arrays using third array as pivot使用第三个数组作为枢轴对三个数组进行排序
【发布时间】:2016-06-05 03:59:07
【问题描述】:

我有 3 个数组

a[n] , b[n] ,c[n]

a 和 b 数组由用户输入,c 计算为

c[i] = b[i] - a[i] + 1

对 c 数组进行排序很容易。我把它排序了

Arrays.sort(c) 方法

现在我必须按照 c 对数组 a 和 b 进行排序,如下所示

例如-

a[5] ={1 , 3 , 5 , 7 , 9 }

b[5] ={5 , 4 , 7 , 8 , 10 }

那么c[]将被计算为

c[i] = b[i] - a[i] +1。

c[5] ={5 , 2 , 3 , 2 , 2}

现在对 c 数组进行排序会得到

c[5] = { 2 , 2 , 2 , 3 , 5 }

现在我也想要 a[i] 和 b[i] 作为

a[5]={ 3 , 7 , 9 , 5 , 1 }

b[5]={ 4 , 8 , 10 , 7 , 5 }

这样现在数组之间的关系将保持不变 对于每个元素“i”

c[i] = b[i] - a[i] + 1

【问题讨论】:

标签: java arrays sorting


【解决方案1】:

使用数组会太乱。您可以将它们放在一个类中:

public class Triplets {
    public int a;
    public int b;
    public int c;
}

然后我们可以在这个类中获取输入而不是单独的数组(例如):

int [] a ={1 , 3 , 5 , 7 , 9 };
int [] b ={5 , 4 , 7 , 8 , 10 };


Triplets [] tripletses = new Triplets[5];

for (int i = 0 ; i < tripletses.length ; i++){
    tripletses[i] = new Triplets();
    tripletses[i].a = a[i];
    tripletses[i].b = b[i];
    tripletses[i].c = tripletses[i].b - tripletses[i].a + 1;
}

当然,您需要在那里使用自己的输入逻辑。现在我们可以使用自定义比较器对这个数组进行排序

Arrays.sort(tripletses, new TripletsComp());

当然还有比较器:

 public class TripletsComp implements Comparator<Triplets> {
        @Override public int compare(Triplets e1, Triplets e2) {
            if(e1.c > e2.c){
                return 1;
            }else if (e1.c == e2.c){
                return 0;
            }
            else { return -1; }
        }
    }

如果您不熟悉 Comarator,请阅读它们 :)

【讨论】:

    【解决方案2】:

    这个问题有两种解决方案:

    1. 创建一个类AbcHolder,其中包含对应于a[i]b[i]c[i] 的三个元素,使用自定义排序器在c 字段上对其进行排序,然后写出as、@ 987654327@s 和cs 回到它们对应的数组,或者
    2. 创建一个索引数组idx[],用从零到c.length-1(含)的数字填充它,并对其进行排序而不是对c[]进行排序。使用自定义比较器在相应索引处比较cs。之后,您可以对匹配的索引重新排序 ba

    【讨论】:

      【解决方案3】:

      当你对 c[5] 进行排序时。您可以按值对其进行排序,您将获得相应键值对的数组。

      它会返回一个这样的数组

      array(5) { [1]=> int(2) [3]=> int(2) [4]=> int(2) [2]=> int(3) [0]=> int(5) } 
      

      通过上面排序后的c[5]数组的key可以得到a[5]和b[5]的对应值;

      我希望这对您有所帮助。

      【讨论】:

        【解决方案4】:

        如果您在不使用任何预定义函数的情况下对它们进行排序,那么您可以进行任何稳定排序,例如插入、合并或冒泡排序,并在第二个数组中执行交换时交换第一个数组中的数字。

        【讨论】:

          【解决方案5】:

          我发现最好的方法是使用Swapper,而不创建新数组。允许您在两个索引之间的排序交换时执行其他操作。我使用了it.unimi.dsi.fastutil.Arrays.quickSort(int, int, IntComparator, Swapper),来自FastUtil。这是我的代码:

          public static void main(String[] args) {
              final int SIZE = 25;
              Random rand = new Random();
              int[] a = new int[SIZE];
              int[] b = new int[SIZE];
              int[] c = new int[SIZE];
          
              for (int i = 0; i < a.length; i++)
                  a[i] = rand.nextInt(100);
              for (int i = 0; i < b.length; i++)
                  b[i] = rand.nextInt(100);
              for (int i = 0; i < c.length; i++)
                  c[i] = b[i] - a[i] + 1;
          
              Arrays.quickSort(0, SIZE, new AbstractIntComparator() {
          
                  @Override
                  public int compare(int k1, int k2) {
                      return Integer.compare(c[k1], c[k2]);
                  }
              }, new Swapper() {
          
                  @Override
                  public void swap(int i, int j) {
                      // Swap in c
                      int temp = c[i];
                      c[i] = c[j];
                      c[j] = temp;
          
                      // Swap in a and b
                      temp = a[i];
                      a[i] = a[j];
                      a[j] = temp;
                      temp = b[i];
                      b[i] = b[j];
                      b[j] = temp;
                  }
              });
          }
          

          【讨论】:

          • 当我试图编译你的代码时,它给我的错误是 QuickComp.java:21: error: cannot find symbol Arrays.quickSort(0, SIZE, new AbstractIntComparator() { ^ symbol: class AbstractIntComparator location : class QuickComp QuickComp.java:27: error: cannot find symbol }, new Swapper() { ^ symbol: class Swapper location: class QuickComp 2 错误。抱歉格式化不正确我是新手,无法弄清楚如何格式化
          • @BrijRajKishore 可能是因为你导入了java.util.Arrays,我用了it.unimi.dsi.fastutil.Arrays。要导入此类,您需要下载 FastUtil 库。
          猜你喜欢
          • 2022-11-15
          • 2023-04-10
          • 2014-04-06
          • 2011-01-22
          • 1970-01-01
          • 2012-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多