DEV Community

Cover image for In-place Merge Sort in Java
Lydia Yuan
Lydia Yuan

Posted on

In-place Merge Sort in Java

Plain In-place Merge Sort

Time complexity: O(N2)O(N^2)


class PlainInPlaceMergeSort {
    private void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

    private void inPlaceMerge(int[] nums, int start, int mid, int end) {
        int leftPointer = start;
        int rightPointer = mid + 1;
        while (leftPointer <= mid && rightPointer <= end) {
            if (nums[leftPointer] <= nums[rightPointer]) {
                leftPointer++;
            } else {
                int currentRightElement = nums[rightPointer];
                int currentRightPointer = rightPointer;
                while (currentRightPointer > leftPointer) {
                    nums[currentRightPointer] = nums[currentRightPointer - 1];
                    currentRightPointer--;
                }
                nums[leftPointer] = currentRightElement;
                leftPointer++;
                rightPointer++;
                mid++;
            }
        }
    }

    private int getMid(int start, int end) {
        return start + (end - start) / 2;
    }

    private void inPlaceMergeSortHelper(int[] nums, int start, int end) {
        if(end - start < 2) {
            return;
        }
        int mid = getMid(start, end);
        inPlaceMergeSortHelper(nums, start, mid);
        inPlaceMergeSortHelper(nums, mid + 1, end);
        inPlaceMerge(nums, start, mid, end);
    }

    public void inPlaceMergeSort(int[] nums) {
        inPlaceMergeSortHelper(nums, 0, nums.length - 1);
    }
}
Enter fullscreen mode Exit fullscreen mode

Shell Sort Mixed In-place Merge Sort

Time Complexity: O(N(logN)2)O(N*(logN)^2)


class ShellSortMixedInplaceMergeSort {
    private void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

    private void inPlaceMerge(int[] nums, int start, int end) {
        int length = end - start + 1;
        int comparisonRange = getComparisonRange(length);
        while (comparisonRange >= 1) {
            int pointer = start;
            while (pointer + comparisonRange <= end) {
                if (nums[pointer] > nums[pointer + comparisonRange]) {
                    swap(nums, pointer, pointer + comparisonRange);
                }
                pointer++;
            }
            if(comparisonRange == 1) {
                break;
            }
            comparisonRange = getComparisonRange(comparisonRange);
        }
    }

    private int getComparisonRange(int previousLength) {
        return (int) Math.ceil((double) previousLength / 2);
    }

    private int getMid(int start, int end) {
        return start + (end - start) / 2;
    }

    private void inPlaceMergeSortHelper(int[] nums, int start, int end) {
        if (end - start < 1) {
            return;
        }
        int mid = getMid(start, end);
        inPlaceMergeSortHelper(nums, start, mid);
        inPlaceMergeSortHelper(nums, mid + 1, end);
        inPlaceMerge(nums, start, end);
    }

    public void inPlaceMergeSort(int[] nums) {
        inPlaceMergeSortHelper(nums, 0, nums.length - 1);
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)