Skip to content

Commit 2ad6265

Browse files
Added Documentation in MergeSortRecursive.java
1 parent e945f16 commit 2ad6265

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

src/main/java/com/thealgorithms/sorts/MergeSortRecursive.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,98 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

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+
*/
611
public class MergeSortRecursive {
712

13+
// The original list to be sorted
814
List<Integer> arr;
915

16+
/**
17+
* Constructor to initialize the list.
18+
*
19+
* @param arr the list of integers to be sorted
20+
*/
1021
public MergeSortRecursive(List<Integer> arr) {
1122
this.arr = arr;
1223
}
1324

25+
/**
26+
* Public method to start the merge sort process.
27+
*
28+
* @return a new sorted list
29+
*/
1430
public List<Integer> mergeSort() {
1531
return merge(arr);
1632
}
1733

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+
*/
1841
private static List<Integer> merge(List<Integer> arr) {
19-
// base condition
42+
// Base condition: a list with 0 or 1 element is already sorted
2043
if (arr.size() <= 1) {
2144
return arr;
2245
}
2346

47+
2448
int arrLength = arr.size();
2549
int half = arrLength / 2;
50+
51+
// Split the list into left and right halves
2652
List<Integer> arrA = arr.subList(0, half);
2753
List<Integer> arrB = arr.subList(half, arr.size());
2854

29-
// recursion
55+
// Recursively sort both halves
3056
arrA = merge(arrA);
3157
arrB = merge(arrB);
3258

59+
// Merge the sorted halves
3360
return sort(arrA, arrB);
3461
}
3562

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+
3671
private static List<Integer> sort(List<Integer> unsortedA, List<Integer> unsortedB) {
72+
// If both lists are empty, return an empty list
3773
if (unsortedA.isEmpty() && unsortedB.isEmpty()) {
3874
return new ArrayList<>();
3975
}
76+
// If one list is empty, return the other list
4077
if (unsortedA.isEmpty()) {
4178
return unsortedB;
4279
}
4380
if (unsortedB.isEmpty()) {
4481
return unsortedA;
4582
}
83+
84+
// Compare first elements of both lists and pick the smaller one
4685
if (unsortedA.get(0) <= unsortedB.get(0)) {
4786
List<Integer> newAl = new ArrayList<Integer>() {
4887
{ add(unsortedA.get(0)); }
4988
};
89+
// Recursively merge the remaining elements
5090
newAl.addAll(sort(unsortedA.subList(1, unsortedA.size()), unsortedB));
5191
return newAl;
5292
} else {
93+
// Create a new list and add the smaller element
5394
List<Integer> newAl = new ArrayList<Integer>() {
5495
{ add(unsortedB.get(0)); }
5596
};
97+
// Recursively merge the remaining elements
5698
newAl.addAll(sort(unsortedA, unsortedB.subList(1, unsortedB.size())));
5799
return newAl;
58100
}

0 commit comments

Comments
 (0)