尝试理解递归合并排序

发布于 2025-01-10 05:58:48 字数 3705 浏览 0 评论 0原文

我正在使用java,我有一个由讲师给出的代码,我正在尝试遵循该代码,但我似乎无法使该代码工作。

这是给我们的代码:

public class MergeSort   {  
    
    @SuppressWarnings("unchecked") // Generic array allocation
    public static <E extends Comparable<? super E>> void sort(E[] A) {
      E[] temp = (E[])new Comparable[A.length];
      mergesort(A, temp, 0, A.length-1);
    }
    
    
    public static <E extends Comparable<? super E>> void mergesort(E[] A, E[] temp, int l, int r) {
        
        int mid = (l+r)/2; // Select midpoint
        if (l == r) {       
            return; // List has one element
        }
        
        mergesort(A, temp, l, mid); // Mergesort first half     
        mergesort(A, temp, mid+1, r); // Mergesort second half
        
        for (int i=l; i<=r; i++) // Copy subarray to temp
            temp[i] = A[i];
        
        // Do the merge operation back to A
        int i1 = l; int i2 = mid + 1;
        for (int curr=l; curr<=r; curr++) {         
            if ((i1< mid+1) && (i2<=r)) {
                 if (temp[i1].compareTo(temp[i2])<0) // Get smaller
                        A[curr] = temp[i1++];
                 else 
                     A[curr] = temp[i2++];              
            }
            else if ((i1==mid+1) && (i2 <= r) ){ // Left sublist exhausted
                A[curr] = temp[i2++];
            }
            else if (i2 > r) { // Right sublist exhausted
                A[curr] = temp[i1++];
            }
        }  
    }
}

所以我在类 Mergesort 中添加了一个 main 函数,这样我就可以使用断点跟踪代码并了解合并排序如何使用递归:

public static void main(String[] args) {
        int Array []= {4,6,2,1,7,9,10};
        MergeSort m=new MergeSort();
        m.sort(Array);
    }

但它不起作用:我收到此错误

 Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method sort(E[]) in the type MergeSort is not applicable for the arguments (int[])

,这基本上是由 < 引起的代码>m.sort(数组);

这是我的整个代码:

public class MergeSort  {
    public static void main(String[] args) {
        int Array []= {4,6,2,1,7,9,10};
        MergeSort m=new MergeSort();
        m.sort(Array);
    }
    
    @SuppressWarnings("unchecked") // Generic array allocation
    public static <E extends Comparable<? super E>> void sort(E[] A) {
      E[] temp = (E[])new Comparable[A.length];
      mergesort(A, temp, 0, A.length-1);
    }
    
    
    public static <E extends Comparable<? super E>> void mergesort(E[] A, E[] temp, int l, int r) {     
        int mid = (l+r)/2; // Select midpoint
        if (l == r) {       
            return; // List has one element
        }
        
        mergesort(A, temp, l, mid); // Mergesort first half
        
        mergesort(A, temp, mid+1, r); // Mergesort second half
        
        for (int i=l; i<=r; i++) // Copy subarray to temp
            temp[i] = A[i];
        
        // Do the merge operation back to A
        int i1 = l; int i2 = mid + 1;
        for (int curr=l; curr<=r; curr++) {         
            if ((i1< mid+1) && (i2<=r)) {
                 if (temp[i1].compareTo(temp[i2])<0) // Get smaller
                        A[curr] = temp[i1++];
                 else 
                     A[curr] = temp[i2++];              
            }
            else if ((i1==mid+1) && (i2 <= r) ){ // Left sublist exhausted
                A[curr] = temp[i2++];
            }
            else if (i2 > r) { // Right sublist exhausted
                A[curr] = temp[i1++];
            }
        }     
    }
}

I'm using java, I have a code given by an instructor and I am trying ti follow the code but I can't seem to make the code work.

This is code given to us :

public class MergeSort   {  
    
    @SuppressWarnings("unchecked") // Generic array allocation
    public static <E extends Comparable<? super E>> void sort(E[] A) {
      E[] temp = (E[])new Comparable[A.length];
      mergesort(A, temp, 0, A.length-1);
    }
    
    
    public static <E extends Comparable<? super E>> void mergesort(E[] A, E[] temp, int l, int r) {
        
        int mid = (l+r)/2; // Select midpoint
        if (l == r) {       
            return; // List has one element
        }
        
        mergesort(A, temp, l, mid); // Mergesort first half     
        mergesort(A, temp, mid+1, r); // Mergesort second half
        
        for (int i=l; i<=r; i++) // Copy subarray to temp
            temp[i] = A[i];
        
        // Do the merge operation back to A
        int i1 = l; int i2 = mid + 1;
        for (int curr=l; curr<=r; curr++) {         
            if ((i1< mid+1) && (i2<=r)) {
                 if (temp[i1].compareTo(temp[i2])<0) // Get smaller
                        A[curr] = temp[i1++];
                 else 
                     A[curr] = temp[i2++];              
            }
            else if ((i1==mid+1) && (i2 <= r) ){ // Left sublist exhausted
                A[curr] = temp[i2++];
            }
            else if (i2 > r) { // Right sublist exhausted
                A[curr] = temp[i1++];
            }
        }  
    }
}

so I added a main function inside the class Mergesort so I can follow the code using breakpoint and understand how merge sort uses recursion:

public static void main(String[] args) {
        int Array []= {4,6,2,1,7,9,10};
        MergeSort m=new MergeSort();
        m.sort(Array);
    }

but it does not work: I am getting this error

 Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method sort(E[]) in the type MergeSort is not applicable for the arguments (int[])

which is basically caused by m.sort(Array);

This is my entire code:

public class MergeSort  {
    public static void main(String[] args) {
        int Array []= {4,6,2,1,7,9,10};
        MergeSort m=new MergeSort();
        m.sort(Array);
    }
    
    @SuppressWarnings("unchecked") // Generic array allocation
    public static <E extends Comparable<? super E>> void sort(E[] A) {
      E[] temp = (E[])new Comparable[A.length];
      mergesort(A, temp, 0, A.length-1);
    }
    
    
    public static <E extends Comparable<? super E>> void mergesort(E[] A, E[] temp, int l, int r) {     
        int mid = (l+r)/2; // Select midpoint
        if (l == r) {       
            return; // List has one element
        }
        
        mergesort(A, temp, l, mid); // Mergesort first half
        
        mergesort(A, temp, mid+1, r); // Mergesort second half
        
        for (int i=l; i<=r; i++) // Copy subarray to temp
            temp[i] = A[i];
        
        // Do the merge operation back to A
        int i1 = l; int i2 = mid + 1;
        for (int curr=l; curr<=r; curr++) {         
            if ((i1< mid+1) && (i2<=r)) {
                 if (temp[i1].compareTo(temp[i2])<0) // Get smaller
                        A[curr] = temp[i1++];
                 else 
                     A[curr] = temp[i2++];              
            }
            else if ((i1==mid+1) && (i2 <= r) ){ // Left sublist exhausted
                A[curr] = temp[i2++];
            }
            else if (i2 > r) { // Right sublist exhausted
                A[curr] = temp[i1++];
            }
        }     
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文