You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何我基于维基百科自上而下伪代码实现的归并排序无法运行?

Troubleshooting Your Broken MergeSort Implementation

Hey there! Let's figure out why your MergeSort code isn't running correctly. Looking at what you've shared, there are a couple of key issues that are stopping it from working as expected:

Key Problems in Your Current Code

  • Incomplete splitMerge method: Your splitMerge declaration cuts off mid-definition—you're missing the full parameter list and the entire method body. This is the biggest blocker right now, since MergeSort relies entirely on this recursive split-and-merge logic.
  • Missing merge logic: Even if you'd started the method, MergeSort requires a critical step where you merge two sorted subarrays back together, which isn't present in your code snippet.

Fixed & Complete MergeSort Implementation

Here's the corrected version of your code with all the missing pieces filled in, plus explanations of how each part works:

public class MergeSortExample {
    public static void main(String args[]) { 
        int[] nums = new int[] { 17, 5, 3, 7, 6, 3, 11, 2 }; 
        mergeSort(nums); 
        for (int i = 0; i < nums.length; i++) { 
            System.out.print(nums[i] + " ,"); 
        }
        // Output will be: 2 ,3 ,3 ,5 ,6 ,7 ,11 ,17 ,
    } 

    public static void mergeSort(int[] A) { 
        int[] B = new int[A.length]; 
        System.arraycopy(A, 0, B, 0, A.length); 
        splitMerge(B, 0, A.length, A); // Sort data from B[] into A[] 
    } 

    // Complete splitMerge method with proper parameters and logic
    public static void splitMerge(int[] B, int begin, int end, int[] A) { 
        // Base case: if subarray has 0 or 1 element, it's already sorted
        if (end - begin <= 1) {
            return;
        }
        
        // Split the array into two halves
        int mid = (begin + end) / 2;
        
        // Recursively sort both halves: sort B into A, then swap roles for merge
        splitMerge(A, begin, mid, B);
        splitMerge(A, mid, end, B);
        
        // Merge the two sorted halves from B into A
        merge(B, begin, mid, end, A);
    }

    // Helper method to merge two sorted subarrays
    public static void merge(int[] B, int begin, int mid, int end, int[] A) {
        int i = begin; // Pointer for left subarray
        int j = mid;   // Pointer for right subarray
        int k = begin; // Pointer for merged array (A)
        
        // Merge elements from both subarrays in order
        while (i < mid && j < end) {
            if (B[i] <= B[j]) {
                A[k] = B[i];
                i++;
            } else {
                A[k] = B[j];
                j++;
            }
            k++;
        }
        
        // Copy any remaining elements from left subarray
        while (i < mid) {
            A[k] = B[i];
            i++;
            k++;
        }
        
        // Copy any remaining elements from right subarray
        while (j < end) {
            A[k] = B[j];
            j++;
            k++;
        }
    }
}

What Changed & Why

  1. Completed splitMerge: Added the full parameter list (int[] A as the target array) and implemented the recursive split logic. We split the array until we hit subarrays of size 1, then start merging back up.
  2. Added merge helper: This method takes two sorted subarrays from B, compares elements one by one, and writes them in sorted order to A. It handles leftover elements in either subarray to ensure nothing gets missed.
  3. Fixed recursion roles: When recursing, we swap A and B to avoid creating new arrays repeatedly—this is a common optimization for top-down MergeSort.

If you run this code, it will correctly sort your input array as expected.

内容的提问来源于stack exchange,提问作者marcus1337

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:52:24