-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollectDataNew.java
224 lines (159 loc) · 6.48 KB
/
CollectDataNew.java
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
224
import java.util.Arrays;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
/** Class that calculates runtime prints them to file.
*Authors: Greg Marx and Zamir Thind
*Programming Assignment 1
*CPSC-3273-AO1
*/
public class CollectDataNew {
/** Main driver for class.
*@param args not used.
*@throws IOException for print.
*/
public static void main(String[] args)
throws IOException {
// Declare, allocate array of 100000 ints, a[0]...a[999999]
int[] huge = new int[100000];
//Creates new file and printwriter.
FileWriter fw = new FileWriter("output.txt");
PrintWriter pw = new PrintWriter(fw);
pw.println("N " + "," + " InsertSort Time " + ","
+ " MergeSort Time " + ","
+ " QuickSort Time ");
//Assigns random values to each element.
for (int i = 0; i < huge.length; i++) {
huge[i] = (int) (Math.random() * 100000); // Random number 0-999999
}
// Run and plot sortArray in increments of 10
for (int n = 10; n <= huge.length; n += 1000) {
// Declare, allocate array of n ints
int[] insertArray = new int[n];
int[] mergeArray = new int[n];
int[] quickArray = new int[n];
// Copy n values from array huge into new array
insertArray = Arrays.copyOf(huge, n);
mergeArray = Arrays.copyOf(huge, n);
quickArray = Arrays.copyOf(huge, n);
//+++++++++++++++++++++++++++++++++++++++++//
//****** CALCULATE INSERT SORT ************//
//+++++++++++++++++++++++++++++++++++++++++//
// Get time prior to sorting
long startTimeInsertSort = System.currentTimeMillis();
// Call sort method to sort new array
insertSort(insertArray);
// Get time after sorting
long endTimeInsertSort = System.currentTimeMillis();
// Subtract endTime - startTime for elapsed time
long elapsedTimeInsertSort = (endTimeInsertSort - startTimeInsertSort);
//+++++++++++++++++++++++++++++++++++++++++//
//****** CALCULATE MERGE SORT *************//
//+++++++++++++++++++++++++++++++++++++++++//
long startTimeMergeSort = System.currentTimeMillis();
// Call sort method to sort new array
mergeSort(mergeArray, 0, mergeArray.length - 1);
// Get time after sorting
long endTimeMergeSort = System.currentTimeMillis();
// Subtract endTime - startTime for elapsed time
long elapsedTimeMergeSort = (endTimeMergeSort - startTimeMergeSort);
//+++++++++++++++++++++++++++++++++++++++++//
//****** CALCULATE QUICK SORT *************//
//+++++++++++++++++++++++++++++++++++++++++//
long startTimeQuickSort = System.currentTimeMillis();
// Call sort method to sort new array
quickSort(quickArray, 0, quickArray.length - 1);
// Get time after sorting
long endTimeQuickSort = System.currentTimeMillis();
// Subtract endTime - startTime for elapsed time
long elapsedTimeQuickSort = (endTimeQuickSort - startTimeQuickSort);
// Prints length of new arrays
System.out.println("For n ints: " + insertArray.length);
// Prints time elapsed for InsertSort
System.out.println("InsertSort elapsed time: " + elapsedTimeInsertSort);
// Prints time elapsed for MergeSort
System.out.println("MergeSort elapsed time: " + elapsedTimeMergeSort);
// Prints time elapsed for QuickSort
System.out.println("QuickSort elapsed time: " + elapsedTimeQuickSort);
// System.out.println("InsertSort ET: " + elapsedTime);
// Write n and the T(n)/n*n to output.txt
pw.println(n + ", " + elapsedTimeInsertSort + ", " + elapsedTimeMergeSort
+ ", " + elapsedTimeQuickSort);
}
pw.close();
}
public static void merge(int A[], int p, int q, int r) {
// Find sizes of two subarrays to merge
int n1 = q - p + 1;
int n2 = r - q;
// Create temp arrays
int L[] = new int[n1 + 1];
int R[] = new int[n2 + 1];
for (int i = 0; i < n1; i++)
L[i] = A[p + i];
for (int j = 0; j < n2; j++)
R[j] = A[q + 1 + j];
L[n1] = Integer.MAX_VALUE;
R[n2] = Integer.MAX_VALUE;
// Initialize indexes of first and second subarrays
int i = 0;
int j = 0;
// Initial index of merged subarray
for (int k = p; k <= r; k++) {
if (L[i] <= R[j]) {
A[k] = L[i];
i = i + 1;
}
else {
A[k] = R[j];
j = j + 1;
}
}
}
public static void mergeSort(int A[], int p, int r) {
if (p < r) {
int q = ((p + r) / 2);
mergeSort(A, p, q);
mergeSort(A, q + 1, r);
merge(A, p, q, r);
}
}
public static void insertSort(int A[]){
int n = A.length;
for(int i = 1; i < n; ++i) {
int key = A[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while(j >= 0 && A[j] > key) {
A[j + 1] = A[j];
j = j -1;
}
A[j + 1] = key;
}
}
public static int partition(int A[], int low, int high) {
int pivot = A[high];
int i = low - 1;
for ( int j = low; j < high; j++) {
if( A[j] < pivot) {
i++;
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
int temp = A[i+1];
A[i+1] = A[high];
A[high] = temp;
return i + 1;
}
public static void quickSort(int A[], int low, int high){
if (low < high) {
int partitionIndex = partition(A, low, high);
quickSort(A, low, partitionIndex - 1);
quickSort(A, partitionIndex + 1, high);
}
}
}