// write a code of mergesort using function in java import java.util.ArrayList; import java.util.Scanner; public class MergeSort{ // use notation beginning, mid and end public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array: "); int n = sc.nextInt(); int[] arr = new int[n]; System.out.println("Enter the elements of the array: "); for(int i=0; i temp = new ArrayList<>(); // temporary array int left = low; // starting index of left half of arr int right = mid + 1; // starting index of right half of arr //storing elements in the temporary array in a sorted manner// while (left <= mid && right <= high) { if (arr[left] <= arr[right]) { temp.add(arr[left]); left++; } else { temp.add(arr[right]); right++; } } // if elements on the left half are still left // while (left <= mid) { temp.add(arr[left]); left++; } // if elements on the right half are still left // while (right <= high) { temp.add(arr[right]); right++; } // transfering all elements from temporary to arr // for (int i = low; i <= high; i++) { arr[i] = temp.get(i - low); } } }