File tree Expand file tree Collapse file tree 4 files changed +28
-197
lines changed
src/main/java/algorithms/sorting/mergeSort Expand file tree Collapse file tree 4 files changed +28
-197
lines changed Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 3
3
/**
4
4
* Here, we are implementing MergeSort where we sort the array in increasing (or more precisely, non-decreasing)
5
5
* 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
31
6
*/
32
7
33
8
public class MergeSort {
Original file line number Diff line number Diff line change
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
+
1
8
![ MergeSort Recursive] ( ../../../../../../../docs/assets/images/MergeSortRecursive.png )
2
9
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
You can’t perform that action at this time.
0 commit comments