简体   繁体   English

保持两个数组String []和int []与合并排序并行

[英]Keeping two arrays, String[] and int[], parallel with merge sort

I'm having trouble with keeping two arrays in parallel within my Merge Sorting algorithm. 我在合并排序算法中将两个数组保持并行时遇到麻烦。 Suppose I have array defMergeSort and intMergeSort2. 假设我有数组defMergeSort和intMergeSort2。

I would like to lexicographically order String defMergeSort, 我想按字典顺序对String defMergeSort进行排序,

String[] defMergeSort = {"Echo", "Alpha", "Charlie", "Beta", "Alpha", "Echo"};

Array intMergeSort2 represents the element position in parallel to defMergeSort. 数组intMergeSort2表示与defMergeSort平行的元素位置。 (Ex: defMergeSort[0] = Echo contingent to intMergeSort2[0] = 0, defMergeSort[3] = Beta contingent to intMergeSort2[3] = 3) intMergeSort2 is to be rearranged in parallel to defMergeSort, although not numerically sorted, (例如:defMergeSort [0] =对应于intMergeSort2 [0] = 0,defMergeSort [3] = Beta对应于intMergeSort2 [3] = 3)intMergeSort2与defMergeSort并行排列,尽管未进行数字排序,

int[] intMergeSort2 = {0,1,2,3,4,5};

The end result should look similar to this (Not sure if parallel ordering for intMergeSort2[] in my example is correct for duplicate Strings in defMergeSort[]): 最终结果应与此类似(不确定在我的示例中对intMergeSort2 []的并行排序对于defMergeSort []中的重复字符串是否正确):

  • defMergeSort[0] Alpha = intMergeSort2[1] 1 defMergeSort [0] Alpha = intMergeSort2 [1] 1
  • defMergeSort[1] Alpha = intMergeSort2[4] 4 defMergeSort [1] Alpha = intMergeSort2 [4] 4
  • defMergeSort[2] Beta = intMergeSort2[3] 3 defMergeSort [2] Beta = intMergeSort2 [3] 3
  • defMergeSort[3] Charlie = intMergeSort2[2] 2 defMergeSort [3]查理= intMergeSort2 [2] 2
  • defMergeSort[4] Echo = intMergeSort2[0] 0 defMergeSort [4]回声= intMergeSort2 [0] 0
  • defMergeSort[5] Echo = intMergeSort2[5] 5 defMergeSort [5]回声= intMergeSort2 [5] 5

The following merge sort algorithm can lexicographically order defMergeSort, although I cannot figure out how to keep defMergeSort in parallel as stipulated above: 以下合并排序算法可以按字典顺序对defMergeSort进行排序,尽管我无法弄清楚如何使defMergeSort保持如上所述的并行状态:

//mergeSort code found at:
//http://www.buildingjavaprograms.com/code-files/2ed/ch13/MergeSort.java
public static void mergeSort(String[] defMergeSort, int[] intMergeSort2) {
    if (defMergeSort.length > 1) {
        // split array into two halves
        String[] left = leftHalf(defMergeSort);
        String[] right = rightHalf(defMergeSort);
        // recursively sort the two halves
        mergeSort(left, intMergeSort2);
        mergeSort(right, intMergeSort2);
        // merge the sorted halves into a sorted whole
        merge(defMergeSort, intMergeSort2, left, right);
    }
}

// Returns the first half of the given array.
public static String[] leftHalf(String[] defMergeSort) {
    int size1 = defMergeSort.length / 2;
    String[] left = new String[size1];
    for (int i = 0; i < size1; i++) {
        left[i] = defMergeSort[i];
    }
    return left;
}

// Returns the second half of the given array.
public static String[] rightHalf(String[] defMergeSort) {
    int size1 = defMergeSort.length / 2;
    int size2 = defMergeSort.length - size1;
    String[] right = new String[size2];
    for (int i = 0; i < size2; i++) {
        right[i] = defMergeSort[i + size1];
    }
    return right;
}

// Merges the given left and right arrays into the given
// result array.  Second, working version.
// pre : result is empty; left/right are sorted
// post: result contains result of merging sorted lists;
public static void merge(String[] defMergeSort, int[] intMergeSort2,
String[] left, String[] right){
    int i1 = 0;   // index into left array
    int i2 = 0;   // index into right array
    for(int i = 0; i < defMergeSort.length; i++){
        if (i2 >= right.length || (i1 < left.length &&
        left[i1].compareTo(right[i2]) <= 0)) {
            defMergeSort[i] = left[i1];
            i1++;
        } else {
            defMergeSort[i] = right[i2];
            i2++;
        }
    }
}

Define an object containing your integer and string values, have a single array of that object, sort that array. 定义一个包含整数和字符串值的对象,对该对象具有单个数组,然后对该数组进行排序。

ie

class DataElement {
    final String str;
    final int i;

    //Add constructor here
}

DataElement[] array; // sort this array

Note that you will either need to implement Comparable in the DataElement or specify a Comparator to the sort method in order to control the sorting. 请注意,您将需要在DataElement中实现Comparable或为排序方法指定Comparator以便控制排序。

Tim B's answer is probably the best approach, I would just like to mention that since the class is just an int and a string, you can use the predefined datastructure std::pair. Tim B的答案可能是最好的方法,我想提及一下,由于该类只是一个整数和一个字符串,因此可以使用预定义的数据结构std :: pair。 Just have an array of these pairs and sort by the string. 只需拥有这些对的数组并按字符串排序即可。 To do this you can use std::sort() defined in , if I'm not wrong std::sort is a MergeSort. 为此,您可以使用中定义的std :: sort(),如果我没错,std :: sort是MergeSort。 You can override the compare function defined with your own. 您可以覆盖自己定义的比较功能。

I find it extremely convenient to use pair in such situations as yours. 我发现在您这样的情况下使用配对非常方便。 Make sure you include . 确保包括在内。

Look into http://www.cplusplus.com/reference/utility/pair/ for reference. 查看http://www.cplusplus.com/reference/utility/pair/以获取参考。

Look into http://www.cplusplus.com/reference/algorithm/sort/ for reference. 查看http://www.cplusplus.com/reference/algorithm/sort/以获取参考。

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

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