gpt4 book ai didi

java - 二维数组 Java 中的合并排序

转载 作者:行者123 更新时间:2023-11-29 08:29:54 24 4
gpt4 key购买 nike

我有一个函数,它应该根据一些特征索引对二维字符串数组进行排序。这是我的 4 个元素 (4 x 10) 的二维数组。

String[][] testArr = {
{"192.168.1.101-67.212.184.66-2156-80-6","192.168.1.101","2156","67.212.184.66","80","6","13/06/2010 06:01:11","2328040","2","0"}
,{"192.168.1.101-67.212.184.66-2159-80-6","192.168.1.101","2159","67.212.184.66","80","6","13/06/2010 06:01:11","2328006","2","0"}
,{"192.168.2.106-192.168.2.113-3709-139-6","192.168.2.106","3709","192.168.2.113","139","6","13/06/2010 06:01:16","7917","10","9"}
,{"192.168.5.122-64.12.90.98-59707-25-6","192.168.5.122","59707","64.12.90.98","25","6","13/06/2010 06:01:25","113992","6","3"}
};

我想根据它们的第 7 个索引对这些元素进行排序,每个元素分别为 (2328040,2328006,7917,113992)。这是我为它编写的函数:

// ************MERGE SORT***************************
public static void mergeSort(String[][] arr,int featureIndex){
mergeSort(arr,new String [arr.length][84],0,arr.length-1,featureIndex);

}
// MERGE SORT HELPER FUNCTION
public static void mergeSort(String[][] arr,String [][] temp,int leftStart,int rightEnd,int featureIndex){
if(leftStart >= rightEnd){
return;
}
int mid = (leftStart + rightEnd)/2;
mergeSort(arr,temp,leftStart, mid,featureIndex);
mergeSort(arr,temp,mid + 1, rightEnd,featureIndex);
mergeHalves(arr,temp,leftStart,rightEnd,featureIndex);
}
// Merge 2 Halves
public static void mergeHalves(String[][] arr,String[][] temp,int leftStart,int rightEnd,int featureIndex){
int leftEnd = (rightEnd + leftStart)/2;
int rightStart = leftEnd + 1;
int size = rightEnd - leftStart + 1;

int left = leftStart;
int right = rightStart;
int index = leftStart;


while(left <= leftEnd && right <= rightEnd){
if(Double.parseDouble(arr[left][featureIndex]) <= Double.parseDouble(arr[right][featureIndex])){
temp[index][featureIndex] = arr[left][featureIndex];
left++;
}else{
temp[index][featureIndex] = arr[right][featureIndex];
right++;
}
index++;
}
// Copy the arrays
System.arraycopy(arr, left, temp, index, leftEnd - left + 1);
System.arraycopy(arr, right, temp, index, rightEnd - right + 1);
System.arraycopy(temp, leftStart, arr, leftStart, size);

}

当我运行程序时,它在每个元素中分别打印出 7917 7917 7917 113992。我该如何解决这个问题?

最佳答案

你的合并函数有一些问题,你可以将函数简化为

public static void mergeHalves(String[][] array,
String[][] aux,
int start, int middle,
int end,
int index) {

if (start >= end) return;

int ls = start, le = middle, rs = middle + 1, re = end, size = end - start + 1;

for (int i = 0; i < size; i++) {
if (rs > re) {
aux[i] = array[ls++];
} else if (ls > le) {
aux[i] = array[rs++];
} else if (Double.parseDouble(array[ls][index])
<= Double.parseDouble(array[rs][index])) {
aux[i] = array[ls++];
} else {
aux[i] = array[rs++];
}
}

System.arraycopy(aux, 0, array, start, size);

}

使用这种方法(合并数组的数组),您需要处理数组中的类型,因为在这种情况下您不能使用 parseDouble 列 0、1、3 和 6。您可以使用Comparator字符串 Comparator<String>作为 mergeHalves() 中的变量用 comparator.compare(array[ls][index], array[rs][index]) <= 0 替换 else if 中的比较.然后根据数据类型定义并传递比较器。

关于java - 二维数组 Java 中的合并排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49213714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com