in-place merge sort

How to do in-place merge sort instead of normal merge sort ?

This sneppet will guide you on how to do in-place merge sort instead of normal merge sort in Java. To perform an in-place merge sort in Java, you can implement the merge sort algorithm with a constant space complexity of O(1). Here are the steps how you can achieve this:

Steps to perform an in-place merge sort in Java

in-place merge sort

1. Implement the merge method to merge two sorted subarrays in-place.
2. Implement the mergeSort method to recursively divide the array into subarrays and merge them in-place.

Here is an example of how you can do this:

public class MergeSort {

    public void merge(int[] arr, int start, int mid, int end) {

        // merge logic to merge two sorted subarrays in-place

    }

    public void mergeSort(int[] intArray, int start, int end) {
        if (start < end) {
            int mid = start + (end - l) / 2;

            mergeSort(intArray, start, mid);
            mergeSort(intArray, mid + start, end);

            merge(intArray, start, mid , end);
        }
    }

    public static void main(String[] args) {

        int[] inputArray= {12, 11, 13, 5, 6, 7};

        MergeSort sorter = new MergeSort();
        sorter.mergeSort(inputArray, 0, inputArray.length - 1);

        // Print the sorted array
        for (int num : inputArray) {
            System.out.print(num + " ");
        }
    }
}

merge logic to merge two sorted subarrays in-place

This section explains you how to implement merge logic to merge two sorted subarrays in-place. To merge two sorted subarrays in-place, you can follow these steps:

1. Create a method to merge two sorted subarrays in-place.
2. Use two pointers to track the current position in each subarray.
3. Compare elements at the current positions in both subarrays and place them in the correct position in the original array.

Here is an example implementation of the merge logic:

public class MergeSort {

    public void merge(int[] intArray, int start, int mid, int end) {

        int n1 = mid - start + 1;
        int n2 = end - mid;

        int[] leftArr = new int[n1];
        int[] rightArr = new int[n2];

        for (int i = 0; i < n1; i++) {
            leftArr[i] = intArray[start + i];
        }
        for (int j = 0; j < n2; j++) {
            rightArr[j] = intArray[mid + 1 + j];
        }

        int i = 0, j = 0;
        int k = start;

        while (i < n1 && j < n2) {
            if (leftArr[i] <= rightArr[j]) {
                intArray[k] = leftArr[i];
                i++;

            } else {
                intArray[k] = rightArr[j];
                j++;
            }
            k++;

        }

        while (i < n1) {
            intArray[k] = leftArr[i];
            i++;
            k++;
        }

        while (j < n2) {
            intArray[k] = rightArr[j];
            j++;
            k++;
        }
    }

    // Other methods like mergeSort() are the same
}

Conclusion

In this implementation, the merge method merges two sorted subarrays intArray[start…mid] and intArray[mid+1…end] in-place. It creates temporary arrays leftArr and rightArr to hold the elements of the subarrays, then compares and merges the elements back into the original array.

You’ll also like:

References

 

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments