Skip to content

Commit 7ba04f8

Browse files
committed
docs: refactor mergesort recursive readme
1 parent 7b93fc0 commit 7ba04f8

File tree

4 files changed

+28
-197
lines changed

4 files changed

+28
-197
lines changed

src/main/java/algorithms/sorting/mergeSort/legacy/iterative/MergeSort.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/main/java/algorithms/sorting/mergeSort/legacy/recursive/MergeSort.java

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/main/java/algorithms/sorting/mergeSort/recursive/MergeSort.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,6 @@
33
/**
44
* Here, we are implementing MergeSort where we sort the array in increasing (or more precisely, non-decreasing)
55
* order recursively.
6-
* <p>
7-
* Brief Description:
8-
* MergeSort is a divide-and-conquer sorting algorithm. The recursive implementation takes a top-down approach by
9-
* recursively dividing the array into two halves, sorting each half separately, and then merging the sorted halves
10-
* to produce the final sorted output.
11-
* <p>
12-
* Implementation Invariant (for the merging subroutine):
13-
* The sub-array temp[start, (k-1)] consists of the (𝑘−start) smallest elements of arr[start, mid] and
14-
* arr[mid + 1, end], in sorted order.
15-
* <p>
16-
* Complexity Analysis:
17-
* Time:
18-
* - Worst case: O(nlogn)
19-
* - Average case: O(nlogn)
20-
* - Best case: O(nlogn)
21-
* Merging two sorted sub-arrays of size (n/2) requires O(n) time as we need to iterate through every element in both
22-
* sub-arrays in order to merge the two sorted sub-arrays into one sorted array.
23-
* <p>
24-
* Recursion expression: T(n) = 2T(n/2) + O(n) => O(nlogn)
25-
* <p>
26-
* Regardless of how sorted the input array is, MergeSort carries out the same divide-and-conquer strategy, so the
27-
* time complexity of MergeSort is O(nlogn) for all cases.
28-
* <p>
29-
* Space:
30-
* - O(n) since we require a temporary array to temporarily store the merged elements in sorted order
316
*/
327

338
public class MergeSort {
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
# Merge Sort
2+
3+
### Brief Description:
4+
MergeSort is a divide-and-conquer sorting algorithm. The recursive implementation takes a top-down approach by
5+
recursively dividing the array into two halves, sorting each half separately, and then merging the sorted halves
6+
to produce the final sorted output.
7+
18
![MergeSort Recursive](../../../../../../../docs/assets/images/MergeSortRecursive.png)
29

3-
Image Source: https://www.101computing.net/merge-sort-algorithm/
10+
Image Source: https://www.101computing.net/merge-sort-algorithm/
11+
12+
### Implementation Invariant (for the merging subroutine):
13+
The sub-array temp[start, (k-1)] consists of the (𝑘−start) smallest elements of arr[start, mid] and
14+
arr[mid + 1, end], in sorted order.
15+
16+
### Complexity Analysis:
17+
Time:
18+
- Worst case: O(nlogn)
19+
- Average case: O(nlogn)
20+
- Best case: O(nlogn)
21+
Merging two sorted sub-arrays of size (n/2) requires O(n) time as we need to iterate through every element in both
22+
sub-arrays in order to merge the two sorted sub-arrays into one sorted array.
23+
24+
Recursion expression: T(n) = 2T(n/2) + O(n) => O(nlogn)
25+
26+
Regardless of how sorted the input array is, MergeSort carries out the same divide-and-conquer strategy, so the
27+
time complexity of MergeSort is O(nlogn) for all cases.
28+
29+
Space:
30+
- O(n) since we require a temporary array to temporarily store the merged elements in sorted order

0 commit comments

Comments
 (0)