-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSortCode.java
More file actions
223 lines (185 loc) · 6.8 KB
/
mergeSortCode.java
File metadata and controls
223 lines (185 loc) · 6.8 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import java.util.Arrays;
import java.util.Random;
/**
* COMP 215: Design and Analysis of Algorithms: Programming Assignment<br>
*
* The <code>MergeSort</code> class provides an algorithm that will sort a given
* array into increasing order using the MergeSort algorithm.
*
* This is made for a program that validates the run-time
* complexity and asymptotic run-time complexity for different kinds of sort methods.
*
* This code will be used to compare the worst case, best case, and average case run times
* for Merge Sort
* <br> <br>
* Created: <br>
* [02,01,2016], [Melany Diaz] [assistance from http://algs4.cs.princeton.edu/22mergesort/Merge.java.html]<br>
* Modifications: <br>
*
* @author [Melany Diaz]
*/
public class MergeSort
{
// instance variables
Comparable[] startArray;
//Variables used to determine how long each method takes to execute
private long startTime;
private long endTime;
private long duration;
boolean debug = true;
//the average, best, and worst case arrays
static Comparable[] average;
static Comparable[] increasing;
static Comparable[] decreasing;
static InsertionSortDecreasing insertionSortDecreasing = new InsertionSortDecreasing();
/**
* Constructs a new object of this class.
*/
public MergeSort()
{
// initializing instance variables
this.startArray = startArray;
}
// Methods
/**
* This method uses merge sort to sort an array in increasing order
*
* @param startArray
* @param lowerIndex
* @param higherIndex
*/
@SuppressWarnings("rawtypes")
public void SortMerge( Comparable[] startArray, int lowerIndex, int higherIndex) {
//precondition
if (debug){
assert startArray[0] != null;
}
if (lowerIndex < higherIndex){
int middle = lowerIndex + (higherIndex -lowerIndex)/2;
SortMerge (startArray, lowerIndex, middle);
SortMerge (startArray,middle + 1, higherIndex);
Merge(startArray, lowerIndex, middle, higherIndex);
}
//postcondition
if(debug){
assert isSorted(startArray, lowerIndex, higherIndex);
}
}
/**
* This method uses Merge Sort to sort in increasing order
* @param a, array to be sorted
* @param lo, lower index of the array
* @param mid, middle index of the array
* @param hi, higher index of the array
*/
public void Merge(Comparable[] a, int lo, int mid, int hi) {
// precondition: a[lo .. mid] and a[mid+1 .. hi] are sorted subarrays
if(debug){
assert isSorted(a, lo, mid);
assert isSorted(a, mid+1, hi);
}
Comparable[] aux = new Comparable[a.length];
// copy to aux[]
for (int k = lo; k <= hi; k++) {
aux[k] = a[k];
}
// merge back to a[]
int i = lo, j = mid+1;
for (int k = lo; k <= hi; k++) {
//invariant: a[low.. hi] is sorted
if(debug){
assert(isSorted(a, lo, k));
}
if (i > mid) a[k] = aux[j++];
else if (j > hi) a[k] = aux[i++];
else if (less(aux[j], aux[i])) a[k] = aux[j++];
else a[k] = aux[i++];
}
// postcondition: a[lo .. hi] is sorted
if(debug){
assert isSorted(a, lo, hi);
}
}
/**
* This method times how long it takes to run merge sort
* for an array passed as a parameter
*
* @param Array Array, the array who is to be sorted
* @return long time, the amount it takes to sort that array
*/
public long TimeToSort(Comparable[] Array){
int lowIndex = 0;
int highIndex = Array.length -1;
long start= System.nanoTime();
this.SortMerge(Array, lowIndex, highIndex);
long end = System.nanoTime();
long duration = end - start;
return duration;
}
/***************************************
* Helper sorting functions.
*/
// is v < w ?
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
// exchange a[i] and a[j]
private static void exch(Object[] a, int i, int j) {
Object swap = a[i];
a[i] = a[j];
a[j] = swap;
}
/**
* confirms that an array is in sorted order.
*
* @param Array Array, the array who is to be confirmed
* @return boolean sorted. True if the array is sorted, false if not
*/
private static boolean isSorted(Comparable[] a) {
return isSorted(a, 0, a.length - 1);
}
private static boolean isSorted(Comparable[] a, int lo, int hi) {
for (int i = lo + 1; i <= hi; i++)
if (less(a[i], a[i-1])) return false;
return true;
}
/********************************************
* runs the comparison analysis and prints results to the console
*/
public void run(){
int n = 0;
while (n <= 27400){
//makes an arrays of size n, used for each of the three cases
average = new Comparable[n];
increasing = new Comparable[n];
decreasing = new Comparable[n];
/**
* The following three blocks will make the best, worst, and random cases
*/
/*
* Make the array used for random case and fill the average array
* with n random objects.
*/
Random generator = new Random();
for(int i = 0; i < n; i++)
average[i] = generator.nextInt(n);
/*
* The following code finds the best case scenario.
*/
InsertionSort i = new InsertionSort();
increasing = Arrays.copyOf(average,n);
increasing = i.SortInsertion(increasing);
/*
* The following code finds the worst case scenario.
*/
InsertionSortDecreasing j = new InsertionSortDecreasing();
decreasing = Arrays.copyOf(average,n);
decreasing = j.SortDecretion(decreasing);
/**
* Will print out the time as csv of the average, best, and worst cases
*/
System.out.println(n + ";" + this.TimeToSort(Arrays.copyOf(average,n)) + ";" + this.TimeToSort(Arrays.copyOf(increasing,n)) + ";" + this.TimeToSort(Arrays.copyOf(decreasing,n)));
n = n+50;
}
}
}