-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
38 lines (34 loc) · 969 Bytes
/
Copy pathMergeSort.java
File metadata and controls
38 lines (34 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//An implementation of mergesort
import java.util.List;
import java.util.Iterator;
public class mergeSort{
public List<Integer> mergeSorted(List<Integer> list1, List<Integer> list2)
{
List<Integer> newList;
if (list1.isEmpty){return list2;}
if (list2.isEmpty){return list1;}
Iterator<Integer> pointer1 = list1.listIterator();
Iterator<Integer> pointer2 = list2.listIterator();
value1 = pointer1.next();
value2 = pointer2.next();
while (pointer1.hasNext() & pointer2.hasNext())
{
if (value1<=value2 | !pointer2.hasNext())
{
newList.add(value1);
value1 = pointer1.next;
}
else
{
newList.add(value2);
value1 = pointer2.next;
}
}
return newList;
public List<int> mergeSort(List<Integer> inputList)
{
if (inputList.size() == 1)
{
return inputList
}
else{return mergeSorted(mergeSort(inputList.subList(0,inputList.size()/2)),mergeSort(inputList.subList(inputList.size()/2,inputList.size())));}