简体   繁体   English

Java中的多维数组的Mergesort

[英]Mergesort of multi-dimensional array in Java

I have a function which should sort a 2D String array according to a feature index. 我有一个功能,应该根据功能索引对2D字符串数组进行排序。 Here is my 2D array of 4 elements (4 x 10). 这是我的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"}
                };

I want to sort these elements according to lets say their 7th index, which is (2328040,2328006,7917,113992) in each element respectively. 我想根据这些元素的第7个索引对它们进行排序,第7个索引分别是每个元素中的(2328040,2328006,7917,113992)。 Here is the function I wrote for it: 这是我为此编写的函数:

// ************MERGE SORT***************************
public static void mergeSort(String[][] arr,int featureIndex){
        mergeSort(arr,new String [arr.length][10],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);

}

When I run the program it prints out 7917 7917 7917 113992 respectively for each element. 当我运行程序时,它会分别为每个元素打印7917 7917 7917 113992。

This is a great question because it brings up the complicated problem of sorting a table with columns of unlike types. 这是一个很好的问题,因为它带来了一个复杂的问题,即用不同类型的列对表进行排序。 Later versions of Java implement new sorting tools you can use. Java的更高版本实现了可以使用的新排序工具。 Here's my solution to your problem using JDK 1.8 (Java 8). 这是我使用JDK 1.8(Java 8)解决您的问题的解决方案。 It's printing only fields 0,1,7 although you can easily add in the rest of the fields. 尽管您可以轻松添加其余字段,但它仅打印字段0、1、7。

First class file: 头等舱文件:

package intquestions;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System.out;
import intquestions.StaffComparator;

public class SortDataList   {
    public static void main(String[] args) {
        runit();     }    

    public static void runit() {        
        Object[] list1 = {"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"    };
        Object[] list2 = {"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"};
        Object[] list3 = {"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"};
        Object[] list4 = {"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"};

        ArrayList<DataList> mList=new java.util.ArrayList<DataList>(); 
        mList.add(new DataList(list1));
        mList.add(new DataList(list2));
        mList.add(new DataList(list3));
        mList.add(new DataList(list4));
        String sep = "  |   " ;
        out.println("BEFORE SORTING - ONLY PRINTING FIELDS 1,2,8 (0,1,7) ---------------------- :   "  );
        out.println(mList.get(0).s.toString() +sep+ mList.get(0).f.toString() +sep+ mList.get(0).eighth.toString());
        out.println(mList.get(1).s.toString() +sep+ mList.get(1).f.toString() +sep+ mList.get(1).eighth.toString());
        out.println(mList.get(2).s.toString() +sep+ mList.get(2).f.toString() +sep+ mList.get(2).eighth.toString());
        out.println(mList.get(3).s.toString() +sep+ mList.get(3).f.toString() +sep+ mList.get(3).eighth.toString());

        StaffComparator myComparator = new StaffComparator();

        try {   Collections.sort(mList, myComparator);  
        } catch (Exception e) {  out.println(e); }

        out.println("\nDONE SORTING - ONLY PRINTING FIELDS 1,2,8 (0,1,7)  ----------------------- :   "  );
        out.println(mList.get(0).s.toString() +sep+ mList.get(0).f.toString() +sep+ mList.get(0).eighth.toString());
        out.println(mList.get(1).s.toString() +sep+ mList.get(1).f.toString() +sep+ mList.get(1).eighth.toString());
        out.println(mList.get(2).s.toString() +sep+ mList.get(2).f.toString() +sep+ mList.get(2).eighth.toString());
        out.println(mList.get(3).s.toString() +sep+ mList.get(3).f.toString() +sep+ mList.get(3).eighth.toString());
    }
}    

class DataList extends DataListAbstract {
    public DataList(Object[] myObj) {
        super(myObj);     }

    public Integer getEighth(DataList locVarDataList) {
        return  locVarDataList.eighth; }

    @Override
    public int compareTo(DataListAbstract o) {
        return 0;     }
}

abstract class DataListAbstract implements Comparable<DataListAbstract> {
    String f;  // first
    String s; //second
    Integer eighth;
    DataListAbstract(Object[] myo) {
        this.eighth =  (Integer)myo[7]; 
        this.f =  (String)myo[0]; 
        this.s =  (String)myo[1]; 
    }
}

Second class: 第二类:

package intquestions;
import java.util.Comparator;

public class StaffComparator implements Comparator<DataList> {
    public int compare(DataList c1, DataList c2) {
        Integer firstInt = c1.getEighth(c1);
        Integer secondInt = c2.getEighth(c2);
        return firstInt.compareTo(secondInt);
    }
}

Output: 输出:

BEFORE SORTING - ONLY PRINTING FIELDS 1,2,8 (0,1,7) ---------------------- :   
192.168.1.101  |   192.168.1.101-67.212.184.66-2156-80-6  |   2328040
192.168.1.101  |   192.168.1.101-67.212.184.66-2159-80-6  |   2328006
192.168.2.106  |   192.168.2.106-192.168.2.113-3709-139-6  |   7917
192.168.5.122  |   192.168.5.122-64.12.90.98-59707-25-6  |   113992

DONE SORTING - ONLY PRINTING FIELDS 1,2,8 (0,1,7)  ----------------------- :   
192.168.2.106  |   192.168.2.106-192.168.2.113-3709-139-6  |   7917
192.168.5.122  |   192.168.5.122-64.12.90.98-59707-25-6  |   113992
192.168.1.101  |   192.168.1.101-67.212.184.66-2159-80-6  |   2328006
192.168.1.101  |   192.168.1.101-67.212.184.66-2156-80-6  |   2328040

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

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