如何在没有错误ArrayIndexOutOfBoundsException的情况下实现具有4路分区的合并排序算法?

时间:2019-04-25 02:14:45

标签: java algorithm mergesort

我正在尝试在Java中用4路分区实现合并排序算法,问题是它在算法的第85行中生成了ArrayIndexOutOfBoundsException错误。代码如下,我基于Merge Sort的2路算法(传统算法):

public static void mergeSort3WayRec(Integer[] gArray, int low, int high,
                                    Integer[] destArray) {
    if (high - low < 2) {
        return;
    }

    int mid1 = low + ((high - low) / 4);
    int mid2 = low + 2 * ((high - low) / 4) + 1;
    int mid3 = low + 3 * ((high - low) / 4) + 2;

    mergeSort3WayRec(destArray, low, mid1, gArray);
    mergeSort3WayRec(destArray, mid1, mid2, gArray);
    mergeSort3WayRec(destArray, mid2, mid3, gArray);
    mergeSort3WayRec(destArray, mid3, high, gArray);

    merge(destArray, low, mid1, mid2, mid3, high, gArray);
}

public static void merge(Integer[] gArray, int low, int mid1, int mid2, int mid3, int high,
                         Integer[] destArray) {
    int i = low, j = mid1, k = mid2, l = mid3, m = high;

    while ((i < mid1) && (j < mid2) && (k < mid3) && (l < high)) {
        if (gArray[i].compareTo(gArray[j]) < 0) {
            if (gArray[i].compareTo(gArray[k]) < 0) {
                if (gArray[i].compareTo(gArray[l]) < 0) {
                    destArray[m++] = gArray[i++];
                } else {
                    destArray[m++] = gArray[l++];
                }
            } else {
                destArray[m++] = gArray[k++];
            }
        } else {
            if (gArray[j].compareTo(gArray[k]) < 0) {
                if (gArray[j].compareTo(gArray[l]) < 0) {
                    destArray[m++] = gArray[j++];
                } else {
                    destArray[m++] = gArray[l++];
                }
            } else {
                if (gArray[k].compareTo(gArray[l]) < 0) {
                    destArray[m++] = gArray[k++];
                } else {
                    destArray[m++] = gArray[l++];
                }
            }
        }
    }

    while ((i < mid1) && (j < mid2)) {
        if (gArray[i].compareTo(gArray[j]) < 0) {
            destArray[m++] = gArray[i++];
        } else {
            destArray[m++] = gArray[j++];
        }
    }

    while ((j < mid2) && (k < mid3)) {
        if (gArray[j].compareTo(gArray[k]) < 0) {
            destArray[m++] = gArray[j++];
        } else {
            destArray[m++] = gArray[k++];
        }
    }

    while ((k < mid3) && (l < high)) {
        if (gArray[k].compareTo(gArray[l]) < 0) {
            destArray[m++] = gArray[k++];
        } else {
            destArray[m++] = gArray[l++];
        }
    }

    while ((i < mid1) && (k < mid3)) {
        if (gArray[i].compareTo(gArray[k]) < 0) {
            destArray[m++] = gArray[i++];
        } else {
            destArray[m++] = gArray[k++];
        }
    }

    while ((i < mid1) && (l < high)) {
        if (gArray[i].compareTo(gArray[l]) < 0) {
            destArray[m++] = gArray[i++];
        } else {
            destArray[m++] = gArray[l++];
        }
    }

    while ((j < mid2) && (l < high)) {
        if (gArray[j].compareTo(gArray[l]) < 0) {
            destArray[m++] = gArray[j++];
        } else {
            destArray[m++] = gArray[l++];
        }
    }

    while (i < mid1) {
        destArray[m++] = gArray[i++];
    }

    while (j < mid2) {
        destArray[m++] = gArray[j++];
    }

    while (k < mid3) {
        destArray[m++] = gArray[k++];
    }

    while (l < high) {
        destArray[m++] = gArray[l++];
    }
}

应注意,gArray是在main方法中输入的原始数组的副本,该部分的代码如下:

public static void main(String args[]) {
    Integer[] data = new Integer[]{ 45, -2, -45, 78,
        30, -42, 10, 19, 73, 93, 80, 60, 2, 98, 85, 99 };
    mergeSort3Way(data);

    System.out.println("After 3 way merge sort: ");
    for (int i = 0; i < data.length; i++) {
        System.out.print(data[i] + " ");
    }
}

public static void mergeSort3Way(Integer[] gArray) {

    if (gArray == null) {
        return;
    }

    Integer[] fArray = new Integer[gArray.length];

    for (int i = 0; i < fArray.length; i++) {
        fArray[i] = gArray[i];
    }

    mergeSort3WayRec(fArray, 0, gArray.length, gArray);

    for (int i = 0; i < fArray.length; i++) {
        gArray[i] = fArray[i];
    }
}

我的问题是,如何解决此错误?另外,如果还有其他实现错误,那么我已经是使用这种算法的新手了。 谢谢。

3 个答案:

答案 0 :(得分:1)

问题似乎是...,m =高,随后是destArray [m ++] =...。

在合并中,当4向合并到达4次运行之一的末尾时,它应降为3向合并。为了避免重复代码,您需要将索引移动到low,mid1,mid2,并使用mid3或high作为子数组从mid2开始的结尾。当3种方式的合并到达运行之一的末尾时,应将其降至2种方式的合并,然后降低至1种方式的副本。

在mergesort中,如果高低<4,您可能只想进行冒泡排序比较并交换高-低== 3或高-低== 2。

假设高-低<4是分开处理的,则用于均匀地设置内部索引(较小的行在左侧):

    int mid1 = low +(high+0-low)/4;
    int mid2 = mid1+(high+1-low)/4;
    int mid3 = mid2+(high+2-low)/4;

使用一对相互递归函数避免回写和“展开”合并逻辑的自上而下4方式合并排序的示例代码。此方法比执行许多条件操作要快,但是我认为主要的性能改进是由于对小批量运行使用了插入排序。在这种情况下,Java中没有“ goto”是一个问题,因为避免重复代码的方法是在合并例程中设置并测试“最小运行”变量。

    static final int MINSIZE = 32;          // must be >= 3

    static void InsertionSort(Integer a[], int ll, int rr)
    {
    int i = ll+1;
    int j;
    Integer t;
        while(i < rr){
            t = a[i];
            j = i;
            while((j > ll) && a[j-1].compareTo(t)> 0){
                a[j] = a[j-1];
                j -= 1;}
        a[j] = t;
        i += 1;}
    }

    public static void MergeSort(Integer[] a)  // entry function
    {
        if(a.length < 2)                    // if size < 2 return
            return;
        Integer[] b = new Integer[a.length];
        MergeSortAtoA(a, b, 0, a.length);

    }

    static void MergeSortAtoA(Integer[] a, Integer[] b, int ll, int rr)
    {
        if(rr - ll <= MINSIZE){
            InsertionSort(a, ll, rr);
            return;}
        int m1 = ll+(rr+0-ll)/4;
        int m2 = m1+(rr+1-ll)/4;
        int m3 = m2+(rr+2-ll)/4;
        MergeSortAtoB(a, b, ll, m1);
        MergeSortAtoB(a, b, m1, m2);
        MergeSortAtoB(a, b, m2, m3);
        MergeSortAtoB(a, b, m3, rr);
        Merge(b, a, ll, m1, m2, m3, rr);
    }

    static void MergeSortAtoB(Integer[] a, Integer[] b, int ll, int rr)
    {
        if(rr - ll <= MINSIZE){
            System.arraycopy(a, ll, b, ll, rr-ll);
            InsertionSort(b, ll, rr);
            return;}
        int m1 = ll+(rr+0-ll)/4;
        int m2 = m1+(rr+1-ll)/4;
        int m3 = m2+(rr+2-ll)/4;
        MergeSortAtoA(a, b, ll, m1);
        MergeSortAtoA(a, b, m1, m2);
        MergeSortAtoA(a, b, m2, m3);
        MergeSortAtoA(a, b, m3, rr);
        Merge(a, b, ll, m1, m2, m3, rr);
    }

    static void Merge(Integer[] a, Integer[] b, int ll, int m1, int m2, int m3, int rr) {
        int bb = ll;                        // b[] index
        int a0 = ll;                        // a[] indexes
        int a1 = m1;
        int a2 = m2;
        int a3 = m3;
        while(true){                        // 4 way merge
            int sr;                         // smallest run
            if(a[a0].compareTo(a[a1]) <= 0){
                if(a[a2].compareTo(a[a3]) <= 0){
                    if(a[a0].compareTo(a[a2]) <= 0){
                        sr = 0;}
                    else{
                        sr = 2;}}
                else{
                    if(a[a0].compareTo(a[a3]) <= 0){
                        sr = 0;}
                    else{
                        sr = 3;}}}
            else{
                if(a[a2].compareTo(a[a3]) <= 0){
                    if(a[a1].compareTo(a[a2]) <= 0){
                        sr = 1;}
                    else{
                        sr = 2;}}
                else{
                    if(a[a1].compareTo(a[a3]) <= 0){
                        sr = 1;}
                    else{
                        sr = 3;}}}
            if(sr == 0){
                b[bb] = a[a0];
                bb++;
                a0++;
                if(a0 < m1)
                    continue;
                a0 = a1;
                a1 = a2;
                a2 = a3;
                m1 = m2;
                m2 = m3;
                m3 = rr;
                break;}
            if(sr == 1){
                b[bb] = a[a1];
                bb++;
                a1++;
                if(a1 < m2)
                    continue;
                a1 = a2;
                a2 = a3;
                m2 = m3;
                m3 = rr;
                break;}
            if(sr == 2){
                b[bb] = a[a2];
                bb++;
                a2++;
                if(a2 < m3)
                    continue;
                a2 = a3;
                m3 = rr;
                break;}
            else{  // sr == 3
                b[bb] = a[a3];
                bb++;
                a3++;
                if(a3 < rr)
                    continue;
                break;}
        }
        while(true){                        // 3 way merge
            int sr;                         // smallest run
            if(a[a0].compareTo(a[a1]) <= 0){
                if(a[a0].compareTo(a[a2]) <= 0){
                    sr = 0;}
                else{
                    sr = 2;}}
            else{
                if(a[a1].compareTo(a[a2]) <= 0){
                    sr = 1;}
                else{
                    sr = 2;}}
            if(sr == 0){
                b[bb] = a[a0];
                bb++;
                a0++;
                if(a0 < m1)
                    continue;
                a0 = a1;
                a1 = a2;
                m1 = m2;
                m2 = m3;
                break;}
            if(sr == 1){
                b[bb] = a[a1];
                bb++;
                a1++;
                if(a1 < m2)
                    continue;
                a1 = a2;
                m2 = m3;
                break;}
            else{ // sr == 2
                b[bb] = a[a2];
                bb++;
                a2++;
                if(a2 < m3)
                    continue;
                break;}
        }
        while(true){                        // 2 way merge
            if(a[a0].compareTo(a[a1]) <= 0){
                b[bb] = a[a0];
                bb++;
                a0++;
                if(a0 < m1)
                    continue;
                a0 = a1;
                m1 = m2;
                break;}
            else{
                b[bb] = a[a1];
                bb++;
                a1++;
                if(a1 < m2)
                    continue;
                break;}
        }
        System.arraycopy(a, a0, b, bb, m1-a0);  // 1 way copy
    }

修复了chqrlie版本的代码。

    public static void merge(Integer[] gArray, int low, int mid1, int mid2, int mid3, int high,
                             Integer[] destArray)
    {
        int i = low, j = mid1, k = mid2, l = mid3, m = low;

        while (m < high) {
            if (i < mid1 && (j >= mid2 || gArray[i].compareTo(gArray[j]) <= 0)) {
                if (k >= mid3 || gArray[i].compareTo(gArray[k]) <= 0) {
                    if (l >= high || gArray[i].compareTo(gArray[l]) <= 0) {
                        destArray[m++] = gArray[i++];
                    } else {
                        destArray[m++] = gArray[l++];
                    }
                } else {
                    if (k < mid3 && (l >= high || gArray[k].compareTo(gArray[l]) <= 0)) {
                        destArray[m++] = gArray[k++];
                    } else {
                        destArray[m++] = gArray[l++];
                    }
                }
            } else {
                if (j < mid2 && (k >= mid3 || gArray[j].compareTo(gArray[k]) < 0)) {
                    if (l >= high || gArray[j].compareTo(gArray[l]) < 0) {
                        destArray[m++] = gArray[j++];
                    } else {
                        destArray[m++] = gArray[l++];
                    }
                } else {
                    if (k < mid3 && (l >= high || gArray[k].compareTo(gArray[l]) < 0)) {
                        destArray[m++] = gArray[k++];
                    } else {
                        destArray[m++] = gArray[l++];
                    }
                }
            }
        }
    }

    public static void mergeSort4WayRec(Integer[] gArray, int low, int high,
                                        Integer[] tempArray) {
        if (high - low < 2) {
            return;
        }

        int mid1 = low  + (high + 0 - low) / 4;
        int mid2 = mid1 + (high + 1 - low) / 4;
        int mid3 = mid2 + (high + 2 - low) / 4;

        mergeSort4WayRec(tempArray, low,  mid1, gArray);
        mergeSort4WayRec(tempArray, mid1, mid2, gArray);
        mergeSort4WayRec(tempArray, mid2, mid3, gArray);
        mergeSort4WayRec(tempArray, mid3, high, gArray);

        merge(tempArray, low, mid1, mid2, mid3, high, gArray);
    }

    public static void mergeSort4Way(Integer[] gArray) {

        if (gArray != null) {
            Integer[] tempArray = new Integer[gArray.length];

            for (int i = 0; i < gArray.length; i++) {
                tempArray[i] = gArray[i];
            }
            mergeSort4WayRec(gArray, 0, gArray.length, tempArray);
        }
    }

    public static void main(String[] args) {
        Integer[] a = new Integer[1024*1024];
        Random r = new Random();
        for(int i = 0; i < a.length; i++)
            a[i] = r.nextInt();
        long bgn, end;
        bgn = System.currentTimeMillis();
        mergeSort4Way(a);
        end = System.currentTimeMillis();
        for(int i = 1; i < a.length; i++){
            if(a[i-1] > a[i]){
                System.out.println("failed");
                break;
            }
        }
        System.out.println("milliseconds " + (end-bgn));
    }

答案 1 :(得分:0)

ArrayIndexOutOfBoundsException应归因于为mid3计算(high - low)/4 < 2的2。 (这背后的想法是什么?(调用函数mergeSort3WayRec()是无用的,将1加到计算mid2上也是没有帮助的。))
计算 P = 1,2,…,n-1 splitP,方差为1而不是 n-1
count = high - low并设置splitP = low + (P * count) / n

答案 2 :(得分:0)

您的代码中存在多个问题:

  • 对于小跨度,分区点的计算不正确:low + 3 * ((high - low) / 4) + 2的{​​{1}}大于high。您应该只使用rcgldr建议的更正方法:

    high - low == 4
  • 对小型数组执行4种方式的合并是过大的,尤其是在大小小于4的情况下。您应该对int mid1 = low + (high - low + 0) / 4; int mid2 = mid1 + (high - low + 1) / 4; int mid3 = mid2 + (high - low + 2) / 4; 使用插入排序,否则可能需要谨慎地确定一些更高的数目基准测试。

  • 名称high - low < 4对于4向合并排序实现是一种误导:)

  • mergeSort3WayRec必须初始化为m,而不是low

  • 四路合并阶段缺少测试。

  • 当其中一个数组用完时,您应该退回到三路合并阶段,这在您的代码中是完全缺失的。根据您的方法,您将需要4个不同的3路合并循环。

  • 然后,如果要保持稳定性,则执行其余两路合并阶段的顺序不正确。实际上,您应该使用high进行测试以实现稳定的排序。

  • <=的参数列表中的名称destArray具有误导性,它是一个临时数组,而不是目标数组。

  • mergeSort3WayRec中的复制循环不正确。 mergeSort3Way()计算就位排序,不需要复制循环。

这是结合边界测试的更简单方法:

mergeSort2WayRec