简体   繁体   English

合并排序比较计数器

[英]Merge Sort Comparison Counter

I cant seem to figure out where to put my comparison counter in the Merge class. 我似乎无法弄清楚在Merge类中放置比较计数器的位置。 Although it sorts the array perfectly, it will not count the number of swaps (or comparisons) it made. 尽管它对数组进行了完美排序,但不会计算其交换(或比较)的次数。 Please help this is apart of my final project 请帮助这是我最后一个项目的一部分

      public static int mergeSort(int[] intArray, int first, int last) {
        if(first < last) {
          int mid = (first + last) / 2;
          mergeSort(intArray, first, mid);
          mergeSort(intArray, mid + 1, last);
          Merge(intArray, first, mid, last);

     }

     return comparisons;

  }

  public static int Merge(int[] intArray, int first, int mid, int last) {

     //int count = 0;
     comparisons = 0;
     int first1 = first, last1 = mid;
     int first2 = mid + 1, last2 = last;
     int temp[] = new int[intArray.length];
     int index = first1;

     while(first1 <= last1 && first2 <= last2) {
        if(intArray[first1] < intArray[first2]) {
           temp[index] = intArray[first1++];
           comparisons++;
        }
        else
            temp[index] = intArray[first2++];
            index++;
            comparisons++;
     }

     while(first1 <= last1)
        temp[index++] = intArray[first1++];

     while(first2 <= last2)
        temp[index++] = intArray[first2++];

     for(index = first; index <= last; index++)
        intArray[index] = temp[index];


     return comparisons;
  }

In the code you included here, you didn't show the definition of the variable comparisons , but since you're using it without defining it, I'm assuming it's a field in your class. 在这里您将包含的代码,你没有表现出变量的定义comparisons ,但因为你使用它没有定义它,我假设它在你的类中的字段。 If that's the case, I think the issue is this line in Merge : 如果是这样,我认为问题是Merge这一行:

comparisons = 0;

Since you have one global counter of the number of comparisons made, the presence of this line means that whenever you call Merge , you're resetting the total number of comparisons back to zero, even if in the course of executing mergesort you already have made a bunch of comparisons. 既然你有做比较的数量的一个全局计数器,这条线的存在意味着,只要你打电话Merge ,你重新比较总数回零,即使在执行合并排序的过程中,你已经做一堆比较。

As a quick fix, just delete this line. 作为速战速决,只要删除此行。 However, I think you'd be better off fixing this by just not having this as a field at all and using return values to communicate back the number of comparisons made. 但是,我认为最好不要完全将其作为一个字段并使用返回值来传达进行比较的次数,从而解决此问题。 Here's one way to do this: 下面是做到这一点的一种方法:

  public static int mergeSort(int[] intArray, int first, int last) {
    int compares = 0;
    if(first < last) {
      int mid = (first + last) / 2;
      compares += mergeSort(intArray, first, mid);
      compares += mergeSort(intArray, mid + 1, last);
      compares += Merge(intArray, first, mid, last);

 }

 return compares;

} }

public static int Merge(int[] intArray, int first, int mid, int last) { int comparisons = 0; 公共静态int Merge(int [] intArray,int first,int mid,int last){int comparisons = 0; int first1 = first, last1 = mid; int first1 =首,last1 =中; int first2 = mid + 1, last2 = last; INT first2 =中间+ 1,last2 =最后; int temp[] = new int[intArray.length]; int temp [] =新的int [intArray.length]; int index = first1; int索引= first1;

 while(first1 <= last1 && first2 <= last2) {
    if(intArray[first1] < intArray[first2]) {
       temp[index] = intArray[first1++];
       comparisons++;
    }
    else
        temp[index] = intArray[first2++];
        index++;
        comparisons++;
 }

 while(first1 <= last1)
    temp[index++] = intArray[first1++];

 while(first2 <= last2)
    temp[index++] = intArray[first2++];

 for(index = first; index <= last; index++)
    intArray[index] = temp[index];


 return comparisons;

} }

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

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