为何我基于维基百科自上而下伪代码实现的归并排序无法运行?
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
splitMergemethod: YoursplitMergedeclaration 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
- Completed
splitMerge: Added the full parameter list (int[] Aas 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. - Added
mergehelper: This method takes two sorted subarrays fromB, compares elements one by one, and writes them in sorted order toA. It handles leftover elements in either subarray to ensure nothing gets missed. - Fixed recursion roles: When recursing, we swap
AandBto 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
相关产品推荐
相关产品推荐

