package Sorts; import java.util.Arrays; import java.util.ArrayList; import java.util.List; public class MergeSortRecursive { List arr; public MergeSortRecursive(List arr) { this.arr = arr; } public void mergeSort() { List arrSorted = merge(arr); System.out.println(arrSorted); } private static List merge(List arr) { // base condition if (arr.size() <= 1) { return arr; } int arrLength = arr.size(); int half = arrLength / 2; List arrA = arr.subList(0, half); List arrB = arr.subList(half, arr.size()); // recursion arrA = merge(arrA); arrB = merge(arrB); return sort(arrA, arrB); } private static List sort(List unsortedA, List unsortedB) { if (unsortedA.size() <= 0 && unsortedB.size() <= 0) { return new ArrayList<>(); } if (unsortedA.size() <= 0) { return unsortedB; } if (unsortedB.size() <= 0) { return unsortedA; } if (unsortedA.get(0) <= unsortedB.get(0)) { List newAl = new ArrayList() { { add(unsortedA.get(0)); } }; newAl.addAll(sort(unsortedA.subList(1, unsortedA.size()), unsortedB)); return newAl; } else { List newAl = new ArrayList() { { add(unsortedB.get(0)); } };newAl.addAll(sort(unsortedA, unsortedB.subList(1, unsortedB.size()))); return newAl; } } } class App { public static void main(String[] args) { MergeSortRecursive sort = new MergeSortRecursive(new ArrayList<>(Arrays.asList(4, 3, 1, 8, 5, 10, 0, 1, 4, 11, 8, 9))); sort.mergeSort(); } }