CodexBloom - Programming Q&A Platform

Issues with Merge Sort Implementation in Java - Performance Degradation on Large Inputs

👀 Views: 171 💬 Answers: 1 📅 Created: 2025-06-11
algorithm sorting performance Java

I'm stuck on something that should probably be simple. I'm getting frustrated with I'm writing unit tests and I'm optimizing some code but I'm currently working on a merge sort implementation in Java, and I'm facing some performance issues when sorting large arrays, specifically those with over a million integers. While the algorithm works correctly for smaller inputs, I've noticed that it takes significantly longer than expected for larger datasets, and I suspect that I might be missing some optimizations. Here's the implementation I've been using: ```java public class MergeSort { public static void mergeSort(int[] array) { if (array.length < 2) return; int mid = array.length / 2; int[] left = new int[mid]; int[] right = new int[array.length - mid]; System.arraycopy(array, 0, left, 0, mid); System.arraycopy(array, mid, right, 0, array.length - mid); mergeSort(left); mergeSort(right); merge(array, left, right); } private static void merge(int[] array, int[] left, int[] right) { int i = 0, j = 0, k = 0; while (i < left.length && j < right.length) { if (left[i] <= right[j]) { array[k++] = left[i++]; } else { array[k++] = right[j++]; } } while (i < left.length) { array[k++] = left[i++]; } while (j < right.length) { array[k++] = right[j++]; } } } ``` I am invoking the sorting method like this: ```java public static void main(String[] args) { int[] data = new int[1000000]; for (int i = 0; i < data.length; i++) { data[i] = (int)(Math.random() * 1000000); } long startTime = System.currentTimeMillis(); MergeSort.mergeSort(data); long endTime = System.currentTimeMillis(); System.out.println("Time taken: " + (endTime - startTime) + " ms"); } ``` When I run this code, it takes almost 8-10 seconds to complete, which seems excessive for merge sort given its O(n log n) complexity. I've tried changing the merge method to avoid using `System.arraycopy`, but it doesn't seem to make much difference. Are there any specific optimizations I could apply to improve the performance of my merge sort implementation? I’ve read about using insertion sort for smaller arrays, but I’m not sure how to integrate that effectively into my current implementation. For context: I'm using Java on Ubuntu. Is there a better approach? My team is using Java for this application. Any ideas how to fix this? Has anyone dealt with something similar? Has anyone else encountered this? The project is a application built with Java. Any ideas how to fix this?