public class MergeSort {
public static void mergeSort(int[] arr) {
if (arr.length <= 1) {
return; // If the array has 0 or 1 element, it's already sorted, so we're done.
}
// Find the middle of the array.
int mid = arr.length / 2;
// Create two subarrays, one for the left half and one for the right half.
int[] left = new int[mid];
int[] right = new int[arr.length - mid];
// Copy elements from the original array into the left and right subarrays.
System.arraycopy(arr, 0, left, 0, mid);
System.arraycopy(arr, mid, right, 0, arr.length - mid);
// Recursively sort the left and right subarrays.
mergeSort(left);
mergeSort(right);
// Merge the sorted left and right subarrays to produce the final sorted array.
merge(arr, left, right);
}
private static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
// Compare elements from the left and right subarrays and merge them into the original array.
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
// Copy any remaining elements from the left subarray (if any).
while (i < left.length) {
arr[k++] = left[i++];
}
// Copy any remaining elements from the right subarray (if any).
while (j < right.length) {
arr[k++] = right[j++];
}
}
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
// Call the mergeSort function to sort the array.
mergeSort(arr);
// Print the sorted array.
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}
public class MergeSort {
public static void mergeSort(int[] arr) {
if (arr.length <= 1) {
return;
}
int mid = arr.length / 2;
// Divide the array into two halves.
int[] left = new int[mid];
int[] right = new int[arr.length - mid];
System.arraycopy(arr, 0, left, 0, mid);
System.arraycopy(arr, mid, right, 0, arr.length - mid);
// Sort the two halves recursively.
mergeSort(left);
mergeSort(right);
// Merge the two sorted halves back together.
merge(arr, left, right);
}
private static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
while (i < left.length) {
arr[k++] = left[i++];
}
while (j < right.length) {
arr[k++] = right[j++];
}
}
}
public class MergeSort {
public static void mergeSort(int[] arr) {
if (arr.length <= 1) {
return;
}
int mid = arr.length / 2;
// Divide the array into two halves.
int[] left = new int[mid];
int[] right = new int[arr.length - mid];
System.arraycopy(arr, 0, left, 0, mid);
System.arraycopy(arr, mid, right, 0, arr.length - mid);
// Sort the two halves recursively.
mergeSort(left);
mergeSort(right);
// Merge the two sorted halves back together.
merge(arr, left, right);
}
private static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
while (i < left.length) {
arr[k++] = left[i++];
}
while (j < right.length) {
arr[k++] = right[j++];
}
}
public static void main(String[] args) {
int[] arr = {5, 3, 2, 1, 4};
mergeSort(arr);
System.out.println("Sorted array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
WITH TIME COMPLEXITY
public class MergeSort {
public static void mergeSort(int[] arr) {
if (arr.length <= 1) {
return;
}
int mid = arr.length / 2;
// Divide the array into two halves.
int[] left = new int[mid];
int[] right = new int[arr.length - mid];
System.arraycopy(arr, 0, left, 0, mid);
System.arraycopy(arr, mid, right, 0, arr.length - mid);
// Sort the two halves recursively.
mergeSort(left);
mergeSort(right);
// Merge the two sorted halves back together.
merge(arr, left, right);
}
private static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
while (i < left.length) {
arr[k++] = left[i++];
}
while (j < right.length) {
arr[k++] = right[j++];
}
}
public static long timeComplexity(int[] arr) {
long startTime = System.nanoTime();
mergeSort(arr);
long endTime = System.nanoTime();
return endTime - startTime;
}
public static void main(String[] args) {
int[] arr = {5, 3, 2, 1, 4};
long timeComplexity = timeComplexity(arr);
System.out.println("Sorted array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("\nTime complexity: " + timeComplexity + " nanoseconds");
}
}
GUYS IF YOU HAVE ANY DOUBT. PLEASE LET ME KNOW