Skip to content

Commit e40487a

Browse files
authored
Java class used for mergeSorting any object lists
Object must implement Comparable<> and MaxObject<>
1 parent 92e3cf6 commit e40487a

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

MergeSort/Java/MergeSortAny.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import java.lang.reflect.Array;
2+
import java.util.ArrayList;
3+
4+
/**
5+
* @author Casper Rysgaard
6+
*/
7+
public class MergeSortAny<T extends MaxValue<T> & Comparable<T>>
8+
{
9+
/*
10+
* java class used for sorting any type of list
11+
*/
12+
13+
public static <T extends MaxValue<T> & Comparable<T>> void sort(ArrayList<T> arrayList)
14+
{
15+
mergeSortSplit(arrayList, 0, arrayList.size()-1);
16+
}
17+
18+
private static <T extends MaxValue<T> & Comparable<T>> void mergeSortSplit(ArrayList<T> listToSort, int start, int end)
19+
{
20+
if (start < end)
21+
{
22+
int middle = (start + end) / 2;
23+
mergeSortSplit(listToSort, start, middle);
24+
mergeSortSplit(listToSort, middle+1, end);
25+
merge(listToSort, start, middle, end);
26+
}
27+
}
28+
29+
private static <T extends MaxValue<T> & Comparable<T>> void merge(ArrayList<T> listToSort, int start, int middle, int end)
30+
{
31+
ArrayList<T> A = new ArrayList<T>(listToSort.subList(start, middle+1));
32+
ArrayList<T> B = new ArrayList<T>(listToSort.subList(middle+1, end+1));
33+
A.add(A.get(0).getMaxObject());
34+
B.add(B.get(0).getMaxObject());
35+
36+
int i = 0;
37+
int j = 0;
38+
39+
for (int k = start; k <= end; k++)
40+
{
41+
if (A.get(i).compareTo(B.get(j)) <= 0)
42+
{
43+
listToSort.set(k, A.get(i));
44+
i++;
45+
}
46+
else
47+
{
48+
listToSort.set(k, B.get(j));
49+
j++;
50+
}
51+
}
52+
}
53+
54+
55+
public static <T extends MaxValue<T> & Comparable<T>> void sort(T[] array)
56+
{
57+
mergeSortSplitArray(array, 0, array.length-1);
58+
}
59+
60+
private static <T extends MaxValue<T> & Comparable<T>> void mergeSortSplitArray(T[] listToSort, int start, int end)
61+
{
62+
if (start < end)
63+
{
64+
int middle = (start + end) / 2;
65+
mergeSortSplitArray(listToSort, start, middle);
66+
mergeSortSplitArray(listToSort, middle+1, end);
67+
mergeArray(listToSort, start, middle, end);
68+
}
69+
}
70+
71+
private static <T extends MaxValue<T> & Comparable<T>> void mergeArray(T[] listToSort, int start, int middle, int end)
72+
{
73+
T[] A = (T[]) Array.newInstance(listToSort[0].getClass(),middle-start +2);
74+
T[] B = (T[]) Array.newInstance(listToSort[0].getClass(),end - middle +1);
75+
cloneArray(listToSort, A, start);
76+
cloneArray(listToSort, B, middle+1);
77+
78+
int i = 0;
79+
int j = 0;
80+
81+
for (int k = start; k <= end; k++)
82+
{
83+
if (A[i].compareTo(B[j]) <= 0)
84+
{
85+
listToSort[k] = A[i];
86+
i++;
87+
}
88+
else
89+
{
90+
listToSort[k] = B[j];
91+
j++;
92+
}
93+
}
94+
}
95+
96+
private static <T extends MaxValue<T> & Comparable<T>> void cloneArray(T[] listIn, T[] cloneInto, int start)
97+
{
98+
for (int i = start; i < start+cloneInto.length-1; i++)
99+
{
100+
cloneInto[i - start] = listIn[i];
101+
}
102+
103+
cloneInto[cloneInto.length-1] = listIn[0].getMaxObject();
104+
}
105+
}

0 commit comments

Comments
 (0)