-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmss.java
More file actions
241 lines (223 loc) · 8.59 KB
/
Copy pathmss.java
File metadata and controls
241 lines (223 loc) · 8.59 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/** This program has 4 different types of Maximum Subsequence Sums, also known
* as MSS. These MSS will be structurally different, and have different run
* times, which we will calculate and graph on our project writeup.
*
* Names: Matthew Gloriani and Kevin Nhu
*
* DLM: March 21, 2021
*
*/
import java.util.*;
import java.math.*;
import java.io.*;
public class mss {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
while (true) {
// this gets the array
int[] array = FileReader();
// finds the smallest number and sets the number as the maxSum
int maxSum = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] < maxSum) {
maxSum = array[i];
}
}
// instructions for running program
int userInt = 0;
while (userInt < 1 || userInt > 5) {
System.out.println("Please enter a number 1-5, where 1-4 prints out individual "
+ "mss methods and 5 prints out all 5 mss methods." + '\n'
+ "For example, '1' prints out mss1 information.");
userInt = kb.nextInt();
}
RunMethods(array, maxSum, userInt);
// asks user if want to run again
System.out.println("Do you want to run the program again? (y for yes and n for no)");
String decision = kb.next();
System.out.println();
if (decision.charAt(0) == 'n') {
System.exit(1);
}
}
}
public static void RunMethods(int[] array, int maxSum, int userInt) {
// Finds sums and finds runtime
long startTime, endTime;
long mss1Time, mss2Time, mss3Time, mss4Time;
int sum1, sum2, sum3, sum4;
// MSS1
if (userInt == 1 || userInt == 5) {
startTime = System.nanoTime();
sum1 = mss1(array, maxSum);
endTime = System.nanoTime();
System.out.println("MSS1 sum is " + sum1 + " and the time elapsed is " + (endTime - startTime));
}
// MSS2
if (userInt == 2 || userInt == 5) {
startTime = System.nanoTime();
sum2 = mss2(array, maxSum);
endTime = System.nanoTime();
System.out.println("MSS2 sum is " + sum2 + " and the time elapsed is " + (endTime - startTime));
}
// MSS3
if (userInt == 3 || userInt == 5) {
startTime = System.nanoTime();
sum3 = mss3(array, 0, array.length-1, maxSum);
endTime = System.nanoTime();
System.out.println("MSS3 sum is " + sum3 + " and the time elapsed is " + (endTime - startTime));
}
// MSS4
if (userInt == 4 || userInt == 5) {
startTime = System.nanoTime();
sum4 = mss4(array, maxSum);
endTime = System.nanoTime();
System.out.println("MSS4 sum is " + sum4 + " and the time elapsed is " + (endTime - startTime));
}
}
/** This method returns the array from the file being read
* @return - the array
*/
public static int[] FileReader() {
while (true) {
System.out.println("Please enter the name of an ASCII file that "
+ "contains a sorted list of integer numbers "
+ "all in one line, separated by a comma: ");
Scanner scan = new Scanner(System.in);
String userFile = scan.nextLine();
File infile;
BufferedReader input;
StringTokenizer tokens;
String inputLine;
try {
infile = new File(userFile);
input = new BufferedReader(new FileReader(infile));
inputLine = input.readLine();
tokens = new StringTokenizer(inputLine, ",");
while ((inputLine = input.readLine()) != null) {
tokens = new StringTokenizer(inputLine, ",");
}
input.close();
int[] array = new int[tokens.countTokens()];
int counter = 0;
while (tokens.hasMoreTokens()) {
array[counter] = Integer.parseInt(tokens.nextToken());
counter++;
}
return array;
}
catch (IOException e) {
System.out.println("Please enter an existing file name.");
}
}
}
/**This is for O(n^3), derived from notes
* @param array - the array given to us
* @param maxSum - starts with the smallest number in the array
* @return - the maximum subsequence sum
*/
public static int mss1(int[] array, int maxSum) {
for (int i = 0; i < array.length; i++) {
for (int j = i; j < array.length; j++) {
int sum = 0;
for (int k = i; k <= j; k++) {
sum += array[k];
}
if (sum > maxSum) {
maxSum = sum;
}
}
}
return maxSum;
}
/**This is for O(n^2), derived from notes
* @param array - the array given to us
* @param maxSum - starts with the smallest number in the array
* @return - the maximum subsequence sum
*/
public static int mss2(int[] array, int maxSum) {
for (int i = 0; i < array.length; i++) {
int sum = 0;
for (int j = i; j < array.length; j++) {
sum += array[j];
if (sum > maxSum) {
maxSum = sum;
}
}
}
return maxSum;
}
/**This is for O(nlogn) - based off of a binary search, but isn't a binary
* serach
* @param array - the array given to us
* @param start - the starting point of the recursiveness and starter
* @param end - the ending point of the recursiveness and starter
* @param maxSum - starts with the smallest number in the array
* @return - the maximum subsequence sum
*/
public static int mss3(int[] array, int start, int end, int maxSum) {
// base case, basically if there's only one element left
if (start == end) {
return array[start]; // returns the value if only one element in array
}
// Now finding the middle of the partial arrays
int mid = (start + end) / 2;
// returns the max value of the arrays
return Math.max(Math.max(mss3(array, start, mid, maxSum), mss3(array, mid+1, end, maxSum)),
mss3Helper(array, start, mid, end, maxSum));
}
/**This is the helper fuction for doing mss3, calculates left and right
* sums of each side; the for loops are based off of mss4, so that we can
* find the max sum in the left and right sides.
* @param array - the array given to us
* @param start - the starting point of the recursiveness
* @param mid - the middle point of the recursiveness
* @param end - the end point of the recursiveness
* @param maxSum - starts with the smallest number in the array
* @return - the max value of the left and right sums
*/
public static int mss3Helper(int[] array, int start, int mid, int end, int maxSum) {
// calculates left sum
int sum = 0;
int leftSum = maxSum;
for (int i = mid; i >= start; i--) {
sum += array[i];
if (sum >= leftSum) {
leftSum = sum;
}
}
// calculates right sum
sum = 0;
int rightSum = maxSum;
for (int i = mid + 1; i <= end; i++) {
sum += array[i];
if (sum >= rightSum) {
rightSum = sum;
}
}
// got these returns value from lecture notes:
// "Return max (maxLeftSum, maxRightSum,
// maxLeftBoundSum + maxRightBoundSum)
return Math.max(Math.max(leftSum, rightSum), leftSum + rightSum);
}
/** This is for O(n), derived from notes
* @param array - the array given to us
* @param maxSum - starts with the smallest number in the array
* @return - the maximum subsequence sum
*/
public static int mss4(int[] array, int maxSum) {
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
if (sum > maxSum) {
maxSum = sum;
}
else {
if (sum < 0) {
sum = 0;
}
}
}
return maxSum;
}
}