排序,根据一个数组去排序另一个数组(根据arr2排序arr1)

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int []arr1={-1,-1,-4,1,3,2,2,4,5,6,5,9,7,9,0};
int []arr2={-1,2,0,5,5};
int []count=new int[9-(-4)+1];
int []result=new int[arr1.length];
for (int i = 0; i < arr1.length; i++) {
count[arr1[i]-(-4)]++;
}
int k=0;
for (int i = 0; i < arr2.length; i++) {
while(count[arr2[i]+4]>0)
{
result[k]=arr2[i];
k++;
count[arr2[i]+4]--;
}
}
for (int i = 0; i < count.length; i++) {
while(count[i]>0)
{
result[k]=i-4;
k++;
count[i]--;
}
}
System.out.println(Arrays.toString(result));
}
}

 

posted @ 2024-02-13 21:56  赵千万  阅读(37)  评论(0编辑  收藏  举报