|
3 | 3 | import java.util.ArrayList; |
4 | 4 | import java.util.List; |
5 | 5 |
|
| 6 | +/** |
| 7 | + * Implementation of Merge Sort using recursion. |
| 8 | + * This class sorts a list of integers by repeatedly dividing it |
| 9 | + * into smaller sublist and merging them in sorted order. |
| 10 | + */ |
6 | 11 | public class MergeSortRecursive { |
7 | 12 |
|
| 13 | + // The original list to be sorted |
8 | 14 | List<Integer> arr; |
9 | 15 |
|
| 16 | + /** |
| 17 | + * Constructor to initialize the list. |
| 18 | + * |
| 19 | + * @param arr the list of integers to be sorted |
| 20 | + */ |
10 | 21 | public MergeSortRecursive(List<Integer> arr) { |
11 | 22 | this.arr = arr; |
12 | 23 | } |
13 | 24 |
|
| 25 | + /** |
| 26 | + * Public method to start the merge sort process. |
| 27 | + * |
| 28 | + * @return a new sorted list |
| 29 | + */ |
14 | 30 | public List<Integer> mergeSort() { |
15 | 31 | return merge(arr); |
16 | 32 | } |
17 | 33 |
|
| 34 | + /** |
| 35 | + * Recursively divides the list into two halves |
| 36 | + * until sublist of size 1 are obtained. |
| 37 | + * |
| 38 | + * @param arr the list to be divided |
| 39 | + * @return a sorted list |
| 40 | + */ |
18 | 41 | private static List<Integer> merge(List<Integer> arr) { |
19 | | - // base condition |
| 42 | + // Base condition: a list with 0 or 1 element is already sorted |
20 | 43 | if (arr.size() <= 1) { |
21 | 44 | return arr; |
22 | 45 | } |
23 | 46 |
|
| 47 | + |
24 | 48 | int arrLength = arr.size(); |
25 | 49 | int half = arrLength / 2; |
| 50 | + |
| 51 | + // Split the list into left and right halves |
26 | 52 | List<Integer> arrA = arr.subList(0, half); |
27 | 53 | List<Integer> arrB = arr.subList(half, arr.size()); |
28 | 54 |
|
29 | | - // recursion |
| 55 | + // Recursively sort both halves |
30 | 56 | arrA = merge(arrA); |
31 | 57 | arrB = merge(arrB); |
32 | 58 |
|
| 59 | + // Merge the sorted halves |
33 | 60 | return sort(arrA, arrB); |
34 | 61 | } |
35 | 62 |
|
| 63 | + /** |
| 64 | + * Merges two already sorted lists into a single sorted list. |
| 65 | + * |
| 66 | + * @param unsortedA first sorted list |
| 67 | + * @param unsortedB second sorted list |
| 68 | + * @return merged and sorted list |
| 69 | + */ |
| 70 | + |
36 | 71 | private static List<Integer> sort(List<Integer> unsortedA, List<Integer> unsortedB) { |
| 72 | + // If both lists are empty, return an empty list |
37 | 73 | if (unsortedA.isEmpty() && unsortedB.isEmpty()) { |
38 | 74 | return new ArrayList<>(); |
39 | 75 | } |
| 76 | + // If one list is empty, return the other list |
40 | 77 | if (unsortedA.isEmpty()) { |
41 | 78 | return unsortedB; |
42 | 79 | } |
43 | 80 | if (unsortedB.isEmpty()) { |
44 | 81 | return unsortedA; |
45 | 82 | } |
| 83 | + |
| 84 | + // Compare first elements of both lists and pick the smaller one |
46 | 85 | if (unsortedA.get(0) <= unsortedB.get(0)) { |
47 | 86 | List<Integer> newAl = new ArrayList<Integer>() { |
48 | 87 | { add(unsortedA.get(0)); } |
49 | 88 | }; |
| 89 | + // Recursively merge the remaining elements |
50 | 90 | newAl.addAll(sort(unsortedA.subList(1, unsortedA.size()), unsortedB)); |
51 | 91 | return newAl; |
52 | 92 | } else { |
| 93 | + // Create a new list and add the smaller element |
53 | 94 | List<Integer> newAl = new ArrayList<Integer>() { |
54 | 95 | { add(unsortedB.get(0)); } |
55 | 96 | }; |
| 97 | + // Recursively merge the remaining elements |
56 | 98 | newAl.addAll(sort(unsortedA, unsortedB.subList(1, unsortedB.size()))); |
57 | 99 | return newAl; |
58 | 100 | } |
|
0 commit comments