Merge Sort

Merge Sort is a divide-and-conquer algorithm that splits the array into halves, recursively sorts each half, and then merges the sorted halves to produce the sorted array. It is known for its efficiency in handling large data sets, with a time complexity of O(n log n) in all cases.


Algorithm:

Bubble Sort Visualization
Complexity
Best Time Complexity O(n log n)
Average Time Complexity O(n log n)
Worst Time Complexity O(n log n)
Space Complexity O(n)

Merge Sort Visualizer

Merge Sort Code

public void mergeSort(int[] arr, int left, int right) {
                        if (left < right) {
                            int mid = (left + right) / 2;
                            mergeSort(arr, left, mid);
                            mergeSort(arr, mid + 1, right);
                            merge(arr, left, mid, right);
                        }
                    }
                    
                    public void merge(int[] arr, int left, int mid, int right) {
                        int n1 = mid - left + 1;
                        int n2 = right - mid;
                        int[] leftArr = new int[n1];
                        int[] rightArr = new int[n2];
                        System.arraycopy(arr, left, leftArr, 0, n1);
                        System.arraycopy(arr, mid + 1, rightArr, 0, n2);
                        int i = 0, j = 0, k = left;
                        while (i < n1 && j < n2) {
                            if (leftArr[i] <= rightArr[j]) {
                                arr[k++] = leftArr[i++];
                            } else {
                                arr[k++] = rightArr[j++];
                            }
                        }
                        while (i < n1) {
                            arr[k++] = leftArr[i++];
                        }
                        while (j < n2) {
                            arr[k++] = rightArr[j++];
                        }
                    }
void mergeSort(int arr[], int left, int right) {
                        if (left < right) {
                            int mid = left + (right - left) / 2;
                            mergeSort(arr, left, mid);
                            mergeSort(arr, mid + 1, right);
                            merge(arr, left, mid, right);
                        }
                    }
                    
                    void merge(int arr[], int left, int mid, int right) {
                        int n1 = mid - left + 1;
                        int n2 = right - mid;
                        int L[n1], R[n2];
                        for (int i = 0; i < n1; i++)
                            L[i] = arr[left + i];
                        for (int j = 0; j < n2; j++)
                            R[j] = arr[mid + 1 + j];
                        int i = 0, j = 0, k = left;
                        while (i < n1 && j < n2) {
                            if (L[i] <= R[j]) {
                                arr[k] = L[i];
                                i++;
                            } else {
                                arr[k] = R[j];
                                j++;
                            }
                            k++;
                        }
                        while (i < n1) {
                            arr[k] = L[i];
                            i++;
                            k++;
                        }
                        while (j < n2) {
                            arr[k] = R[j];
                            j++;
                            k++;
                        }
                    }
void mergeSort(vector<int>& arr, int left, int right) {
                        if (left < right) {
                            int mid = left + (right - left) / 2;
                            mergeSort(arr, left, mid);
                            mergeSort(arr, mid + 1, right);
                            merge(arr, left, mid, right);
                        }
                    }
                    
                    void merge(vector<int>& arr, int left, int mid, int right) {
                        int n1 = mid - left + 1;
                        int n2 = right - mid;
                        vector<int> L(n1), R(n2);
                        for (int i = 0; i < n1; i++)
                            L[i] = arr[left + i];
                        for (int j = 0; j < n2; j++)
                            R[j] = arr[mid + 1 + j];
                        int i = 0, j = 0, k = left;
                        while (i < n1 && j < n2) {
                            if (L[i] <= R[j]) {
                                arr[k] = L[i];
                                i++;
                            } else {
                                arr[k] = R[j];
                                j++;
                            }
                            k++;
                        }
                        while (i < n1) {
                            arr[k] = L[i];
                            i++;
                            k++;
                        }
                        while (j < n2) {
                            arr[k] = R[j];
                            j++;
                            k++;
                        }
                    }
def merge_sort(arr):
                        if len(arr) > 1:
                            mid = len(arr) // 2
                            left_half = arr[:mid]
                            right_half = arr[mid:]
                            merge_sort(left_half)
                            merge_sort(right_half)
                            i = j = k = 0
                            while i < len(left_half) and j < len(right_half):
                                if left_half[i] < right_half[j]:
                                    arr[k] = left_half[i]
                                    i += 1
                                else:
                                    arr[k] = right_half[j]
                                    j += 1
                                k += 1
                            while i < len(left_half):
                                arr[k] = left_half[i]
                                i += 1
                                k += 1
                            while j < len(right_half):
                                arr[k] = right_half[j]
                                j += 1
                                k += 1

Practice Questions

Question Number Question Title Level Link
1 Sort an Array Easy Link
2 Sort List Medium Link
3 Merge k Sorted Lists Hard Link