Java中多维数组的Mergesort

时间:2018-03-10 23:01:03

标签: java multidimensional-array collections mergesort

我有一个函数应该根据特征索引对2D String数组进行排序。这是我的4个元素的2D数组(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][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);

}

当我运行程序时,它会分别为每个元素打印出7917 7917 7917 113992.

1 个答案:

答案 0 :(得分:2)

这是一个很好的问题,因为它带来了使用不同类型的列对表进行排序的复杂问题。 Java的更高版本实现了您可以使用的新排序工具。这是我使用JDK 1.8(Java 8)解决问题的方法。它只打印字段0,1,7,尽管您可以轻松添加其余字段。

头等档案:

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]; 
    }
}

第二课:

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);
    }
}

输出:

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