-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
70 lines (65 loc) · 1.78 KB
/
MergeSort.java
File metadata and controls
70 lines (65 loc) · 1.78 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.ArrayList;
/**
* Write a description of class MergeSort here.
*
* @author sofie Budman
* @version 4/21
*/
public class MergeSort
{
// instance variables - replace the example below with your own
private int x;
/**
* Constructor for objects of class MergeSort
*/
public MergeSort()
{
// initialise instance variables
x = 0;
}
public ArrayList<Integer> merge(ArrayList<Integer> a, ArrayList<Integer> b)
{
ArrayList<Integer> c = new ArrayList<Integer>();
while(a.size() > 0 && b.size() > 0){
if(a.get(0) > b.get(0)){
c.add(b.get(0));
b.remove(0);
}else{
c.add(a.get(0));
a.remove(0);
}
}
while(a.size() >0){
c.add(a.get(0));
a.remove(0);
}
return c;
}
public ArrayList<Integer> mergeSort(ArrayList<Integer> arr)
{
if (arr.size() == 1) return arr;
ArrayList<Integer> arrayOne = new ArrayList<Integer>();
ArrayList<Integer> arrayTwo = new ArrayList<Integer>();
for(int i = 0; i < arr.size()/2; i ++){
arrayOne.add(arr.get(i));
}
for(int i = arr.size()/2; i < arr.size(); i ++){
arrayTwo.add(arr.get(i));
}
arrayOne = mergeSort(arrayOne);
arrayTwo = mergeSort(arrayTwo);
return merge(arrayOne, arrayTwo);
}
public static void main(String[] args)
{
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(10);
a.add(9);
a.add(8);
a.add(7);
a.add(6);
MergeSort m = new MergeSort();
ArrayList<Integer> c = m.mergeSort(a);
System.out.println(c);
}
}