Java插入排序运行时

时间:2018-03-11 19:58:56

标签: java performance optimization runtime insertion-sort

我有一个基本的插入排序函数,它将2D字符串数组和索引值作为参数,然后根据索引对该数组进行排序,如下所示:

public static void insertionSort(String[][] arr,int featureindex) {
        int j;
        double key;
        int n = arr.length;
        for (int i = 2; i < n; i++){
            key = Double.parseDouble(arr[i][featureindex]);
            String[] keyRow = arr[i];
            j = i - 1;
            while ((j > 0) && (Double.parseDouble(arr[j][featureindex]) > key)){
                arr[j+1] = arr[j];
                j = j - 1;
            }
            arr[j+1] = keyRow;
        }
    }

我知道插入排序比合并排序或快速排序慢,但我必须比较不同的排序算法(确实是mergesort和quicksort)并且认为插入比bubble和选择排序更好,但是,它出来的速度比我期望。它在~140秒内对50 000个阵列进行排序,在700秒内对10万个阵列进行排序。我只是想尽可能地优化它,以便它还必须处理250 000个阵列。我的数据看起来像这样:(例如4个数组):

String[][] testArr = {
                {"Flow ID"," Source IP","Source Port","Destination IP","Destination Port","Protocol","Timestamp","Flow Duration"," Total Fwd Packets","Total Backward Packets"}
                ,{"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","12"}
               ,{"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","12"}
               ,{"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","987"}
               ,{"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","6"}
                };

这里我在每个子阵列中有10个元素但是,在实际的一个中,它可以说是250000 x 84阵列。

有没有办法改善这个?

0 个答案:

没有答案