Skip to content

Commit 9d60203

Browse files
committed
Added mergesort recursive int implementation
1 parent 517a76c commit 9d60203

File tree

8 files changed

+319
-148
lines changed

8 files changed

+319
-148
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
![InsertionSort](../../../../assets/InsertionSort.png)
2+
3+
Image taken from: https://www.hackerrank.com/challenges/correctness-invariant/problem
Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,4 @@
11
package src.algorithms.sorting.mergeSort.iterative;
22

3-
import java.util.*;
4-
5-
/**
6-
* Iterative implementation of Merge sort with O(n) space
7-
*/
83
public class MergeSort {
9-
10-
/**
11-
* Sorts a list from a specified start to end point.
12-
*
13-
* Note: starting with an interval size (call this var <interval>) of 1 and iteratively *2,
14-
* Merge two sub-lists of size <interval> together using merge routine.
15-
* Invariant: Partitioning the main list into sub-lists of size <interval>,
16-
* each of this sub-list is sorted within itself (the last sub-list may not be of
17-
* size <interval> but it is still sorted since the size is necessarily less than <interval>.
18-
*
19-
* @param <T> generic type of object
20-
* @param lst list of objects to be sorted
21-
* @param start sorting starts at this index
22-
* @param end sorting ends (inclusive) at this index
23-
*/
24-
private static <T extends Comparable<T>> void mergeSort(List<T> lst, int start, int end) {
25-
if (start == end) {
26-
return;
27-
}
28-
int size = lst.size();
29-
int interval = 1;
30-
List<T> tmp = new ArrayList<>();
31-
for (T item : lst) {
32-
tmp.add(item);
33-
}
34-
while (interval < lst.size()) {
35-
for (int i = 0; i + interval < size; i += 2*interval) {
36-
int start1 = i;
37-
int start2 = i + interval;
38-
int e = i + 2 * interval - 1;
39-
if (e > size - 1) {
40-
e = size - 1;
41-
}
42-
merge(lst, tmp, start1, start2, e);
43-
}
44-
interval *= 2;
45-
}
46-
47-
}
48-
49-
/**
50-
* Merging algorithm that merges two sorted sub-lists into one final sorted list.
51-
* @param <T> generic type of object
52-
* @param lst at the end, elements from s1 to e (inclusive) of lst are sorted
53-
* @param s1 start index of first sub-list
54-
* @param s2 start index of second sub-list; note that end index of first sub-list is s2-1
55-
* @param e end index of second sub-list
56-
*/
57-
private static <T extends Comparable<T>> void merge(List<T> lst, List<T> tmp, int s1, int s2, int e) {
58-
int startLeft = s1;
59-
int startRight = s2;
60-
int tmpIdx = s1;
61-
while (startLeft < s2 && startRight < e + 1) {
62-
if (lst.get(startLeft).compareTo(lst.get(startRight)) < 0) {
63-
tmp.set(tmpIdx, lst.get(startLeft++));
64-
} else {
65-
tmp.set(tmpIdx, lst.get(startRight++));
66-
}
67-
tmpIdx++;
68-
}
69-
70-
while (startLeft < s2) {
71-
tmp.set(tmpIdx++, lst.get(startLeft++));
72-
}
73-
74-
while (startRight < e + 1) {
75-
tmp.set(tmpIdx++, lst.get(startRight++));
76-
}
77-
78-
for (int i = s1; i < e+1; i++) {
79-
lst.set(i, tmp.get(i));
80-
}
81-
}
82-
83-
/**
84-
* Sorting algorithm that clients calls
85-
* @param <T> generic type
86-
* @param lst list to be sorted
87-
*/
88-
public static <T extends Comparable<T>> void sort(List<T> lst) {
89-
mergeSort(lst, 0, lst.size() - 1);
90-
}
914
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package src.algorithms.sorting.mergeSort.legacy.iterative;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Iterative implementation of Merge sort with O(n) space
7+
*/
8+
public class MergeSort {
9+
10+
/**
11+
* Sorts a list from a specified start to end point.
12+
*
13+
* Note: starting with an interval size (call this var <interval>) of 1 and iteratively *2,
14+
* Merge two sub-lists of size <interval> together using merge routine.
15+
* Invariant: Partitioning the main list into sub-lists of size <interval>,
16+
* each of this sub-list is sorted within itself (the last sub-list may not be of
17+
* size <interval> but it is still sorted since the size is necessarily less than <interval>.
18+
*
19+
* @param <T> generic type of object
20+
* @param lst list of objects to be sorted
21+
* @param start sorting starts at this index
22+
* @param end sorting ends (inclusive) at this index
23+
*/
24+
private static <T extends Comparable<T>> void mergeSort(List<T> lst, int start, int end) {
25+
if (start == end) {
26+
return;
27+
}
28+
int size = lst.size();
29+
int interval = 1;
30+
List<T> tmp = new ArrayList<>();
31+
for (T item : lst) {
32+
tmp.add(item);
33+
}
34+
while (interval < lst.size()) {
35+
for (int i = 0; i + interval < size; i += 2*interval) {
36+
int start1 = i;
37+
int start2 = i + interval;
38+
int e = i + 2 * interval - 1;
39+
if (e > size - 1) {
40+
e = size - 1;
41+
}
42+
merge(lst, tmp, start1, start2, e);
43+
}
44+
interval *= 2;
45+
}
46+
47+
}
48+
49+
/**
50+
* Merging algorithm that merges two sorted sub-lists into one final sorted list.
51+
* @param <T> generic type of object
52+
* @param lst at the end, elements from s1 to e (inclusive) of lst are sorted
53+
* @param s1 start index of first sub-list
54+
* @param s2 start index of second sub-list; note that end index of first sub-list is s2-1
55+
* @param e end index of second sub-list
56+
*/
57+
private static <T extends Comparable<T>> void merge(List<T> lst, List<T> tmp, int s1, int s2, int e) {
58+
int startLeft = s1;
59+
int startRight = s2;
60+
int tmpIdx = s1;
61+
while (startLeft < s2 && startRight < e + 1) {
62+
if (lst.get(startLeft).compareTo(lst.get(startRight)) < 0) {
63+
tmp.set(tmpIdx, lst.get(startLeft++));
64+
} else {
65+
tmp.set(tmpIdx, lst.get(startRight++));
66+
}
67+
tmpIdx++;
68+
}
69+
70+
while (startLeft < s2) {
71+
tmp.set(tmpIdx++, lst.get(startLeft++));
72+
}
73+
74+
while (startRight < e + 1) {
75+
tmp.set(tmpIdx++, lst.get(startRight++));
76+
}
77+
78+
for (int i = s1; i < e+1; i++) {
79+
lst.set(i, tmp.get(i));
80+
}
81+
}
82+
83+
/**
84+
* Sorting algorithm that clients calls
85+
* @param <T> generic type
86+
* @param lst list to be sorted
87+
*/
88+
public static <T extends Comparable<T>> void sort(List<T> lst) {
89+
mergeSort(lst, 0, lst.size() - 1);
90+
}
91+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package src.algorithms.sorting.mergeSort.legacy.recursive;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Recursive implementation of merge sort (in ascending order)
7+
* with only additional O(n) space required.
8+
*/
9+
public class MergeSort {
10+
11+
/**
12+
* Sorts a list from a specified start to end point, making use of
13+
* and additional copy list to store sorted elements.
14+
* @param <T> generic type of object
15+
* @param lst list of objects to be sorted from start to end
16+
* @param start sorting of elements starting from this index
17+
* @param end sorting of elements up till and inclusive of this index
18+
* @param copy copy of the list to store and extract elements in sorted position
19+
*/
20+
private static <T extends Comparable<T>> void mergeSort(List<T> lst, int start, int end, List<T> copy) {
21+
if (start == end) {
22+
return;
23+
}
24+
int mid = start + (end - start) / 2;
25+
mergeSort(copy, start, mid, lst);
26+
mergeSort(copy, mid + 1, end, lst);
27+
merge(lst, start, mid, end, copy);
28+
}
29+
30+
/**
31+
* Merging algorithm that merges two sorted sub-lists into one final sorted list.
32+
* @param <T> generic type of object
33+
* @param arr at the end, elements from s to e (inclusive) of arr are sorted
34+
* @param s start of first sub-list
35+
* @param m end (inclusive) of first sub-list; note start of second sub-list is m+1
36+
* @param e end (inclusive) of second sub-list
37+
* @param cpy copy of the sorted sub-lists to extract and insert sorted final list into arr
38+
*/
39+
private static <T extends Comparable<T>> void merge(List<T> arr, int s, int m, int e, List<T> cpy) {
40+
int start = s;
41+
int startLeft = s;
42+
int startRight = m + 1;
43+
44+
while (startLeft < m + 1 && startRight < e + 1) {
45+
if (cpy.get(startLeft).compareTo(cpy.get(startRight)) < 0) {
46+
arr.set(start++, cpy.get(startLeft++));
47+
} else {
48+
arr.set(start++, cpy.get(startRight++));
49+
}
50+
}
51+
52+
while (startLeft < m + 1) {
53+
arr.set(start++, cpy.get(startLeft++));
54+
}
55+
56+
while (startRight < e + 1) {
57+
arr.set(start++, cpy.get(startRight++));
58+
}
59+
}
60+
61+
/**
62+
* Sorting algorithm to be called by client.
63+
* @param <T> generic type
64+
* @param lst list of elements to be sorted.
65+
*/
66+
public static <T extends Comparable<T>> void sort(List<T> lst) {
67+
List<T> copy = new ArrayList<>();
68+
for (int i = 0; i < lst.size(); i++) {
69+
copy.add(lst.get(i));
70+
}
71+
mergeSort(lst, 0, lst.size() - 1, copy);
72+
}
73+
}

0 commit comments

Comments
 (0)