简体   繁体   English

合并排序-使用整数数组对字符串数组进行排序

[英]Merge Sort - Sorting String Array with Int Array

For this project, I'm given an array of strings and an array of ints. 对于这个项目,我得到了一个字符串数组和一个整数数组。 int[1] is the ranking for string[1]. int [1]是字符串[1]的排名。 I need to sort the int array in order from 1 to n using mergesort, which I've done below. 我需要使用mergesort对int数组从1到n进行排序,这是我在下面完成的。 But I also need to switch the positions of the string array when the int array gets moved so they are both sorted, if that makes sense? 但是我还需要在移动int数组时切换字符串数组的位置,以便对它们都进行排序,如果这有意义吗? I can't figure out what's wrong with my coding or even if my idea will actually work, but I keep getting an array index out of bounds error on stringSorted[k] = stringRight[j] and I can't figure out if there's a way to fix this. 我无法弄清楚编码有什么问题,即使我的想法确实可以解决,但我仍在stringSorted [k] = stringRight [j]上不断获取数组索引错误,并且我无法弄清楚是否存在解决此问题的方法。 Essentially, when an int was added to the sortedInt array, I also added that element to the sorted String array. 本质上,将int添加到sortedInt数组时,我还将该元素添加到了Sorted String数组中。 Thank you for any help, and let me know if something doesn't make sense 谢谢您的帮助,如果有任何问题,请告诉我

private static int sortAndCount(int intToSort[]){

    int inversionsLeft;
    int inversionsRight;
    int inversionsMerged;

    if(intToSort.length == 1){
        return 0;
    }

    int m = intToSort.length/2;

    int[] intLeft = new int[m];
    stringLeft = new String[m];

    int[] intRight = new int[intToSort.length-m];
    stringRight = new String[intToSort.length-m];


    for (int i=0; i < m; i++){
        intLeft[i] = intToSort[i];
        stringLeft[i] = stringToSort[i];
    }

    for (int i = 0;i < intRight.length; i++){
            intRight[i] = intToSort[m+i];
            stringRight[i] = stringToSort[m+i];
    }

    inversionsLeft = sortAndCount(intLeft);
    inversionsRight = sortAndCount(intRight);

    intSorted = new int[intToSort.length];
    stringSorted = new String[stringToSort.length];

    inversionsMerged = mergeAndCount(intLeft, intRight);

    return(inversionsLeft + inversionsRight + inversionsMerged);

}

private static int mergeAndCount(int[] intLeft, int[] intRight){

    int count = 0;
    int i = 0;
    int j = 0;
    int k = 0;

    while(i < intLeft.length && j < intRight.length){

        if(intLeft[i] < intRight[j]){
            intSorted[k] = intLeft[i];
            stringSorted[k] = stringLeft[i];
            i++;
        }

        else{
            intSorted[k] = intRight[j];
            stringSorted[k] = stringRight[j];
            count += intLeft.length - i + 1;
            j++;
        }

        k++;

    }

     while (i < intLeft.length)
        {
            intSorted[k] = intLeft[i];
            stringSorted[k] = stringLeft[i];
            k++;
            i++;

        }

     while (j < intRight.length)
        {
            intSorted[k] = intRight[j];
            stringSorted[k] = stringRight[j];
            j++;
            k++;

        }

     return count;

}

}
int[] intLeft = new int[m];
stringLeft = new String[m];

int[] intRight = new int[intToSort.length-m];
stringRight = new String[intToSort.length-m];

You'll notice here that for the int arrays you are creating new variables, for the string you are replacing the outer. 您会在这里注意到,对于int数组,您正在创建新变量,对于字符串,您正在替换外部。 This is making your string arrays smaller with each recursive call whereas your int arrays are passed to each method. 这将使您的string数组在每次递归调用时都变小,而将int数组传递给每个方法。

By the time you get to calling mergeAndCount , stringLeft and stringRight are very small whereas the appropriately sized intLeft and intRight are passed as arguments. 在您开始调用mergeAndCountstringLeftstringRight很小,而适当大小的intLeftintRight作为参数传递。

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

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