Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
218 lines (169 loc) · 6.92 KB
/
Copy pathMain.java
File metadata and controls
218 lines (169 loc) · 6.92 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
/*********************************************************
*
* Place your name / section number in the 'HW1.java' file
* (NOT this file). This main routine is a driver routine for
* testing the methods in the file 'HW1.java'.
*
*********************************************************/
import java.util.Stack;
public class Main {
public static void main(String[] args) {
int assignmentScore = 0;
boolean errorFlag = false;
System.out.println("\nProgram testing driver starting ...");
/**************************************
*
* Testing of the LinkedList code.
*
***************************************/
HW1.LinkedList list1 = new HW1.LinkedList();
// Insert the values
list1.sortedInsert(1);
list1.sortedInsert(2);
list1.sortedInsert(3);
list1.sortedInsert(4);
list1.sortedInsert(1);
list1.sortedInsert(2);
list1.sortedInsert(3);
list1.sortedInsert(4);
list1.sortedInsert(9);
list1.sortedInsert(100);
list1.sortedInsert(35);
// System.out.println("Starting Linked-Lists testing ...");
// System.out.println("After initial insertions: " + list1);
if ( ! list1.toString().equals("[1 1 2 2 3 3 4 4 9 35 100]") ) {
System.out.println("ERROR 1: Linked-list test failed sorted insertions");
errorFlag = true;
}
list1.removeElementsLT(8); // remove items with a value less than 8
if ( ! errorFlag && ! list1.toString().equals("[9 35 100]") ) {
System.out.println("ERROR 2: Linked-list test failed removing elements LT");
errorFlag = true;
}
list1.removeElement(35); // remove items with a value of 35
if ( ! errorFlag && ! list1.toString().equals("[9 100]") ) {
System.out.println("ERROR 3: Linked-list test failed removing elements equal");
errorFlag = true;
}
// Additional link-list tests
HW1.LinkedList list2 = new HW1.LinkedList();
list2.sortedInsert(1003);
list2.sortedInsert(4004);
list2.sortedInsert(05);
list2.sortedInsert(5);
list2.sortedInsert(59);
list2.sortedInsert(995);
list2.sortedInsert(5);
if ( ! errorFlag && ! list2.toString().equals("[5 5 5 59 995 1003 4004]") ) {
System.out.println("ERROR 4: Linked-list test failed sorted insertions");
errorFlag = true;
}
list2.removeElement(35); // remove items with a value of 35
if ( ! errorFlag && ! list2.toString().equals("[5 5 5 59 995 1003 4004]") ) {
System.out.println("ERROR 5: Linked-list test failed removing elements equal");
errorFlag = true;
}
list2.removeElement(995); // remove items with a value of 35
if ( ! errorFlag && ! list2.toString().equals("[5 5 5 59 1003 4004]") ) {
System.out.println("ERROR 6: Linked-list test failed removing elements equal");
errorFlag = true;
}
list2.removeElementsLT(35); // remove items with a value of 35
if ( ! errorFlag && ! list2.toString().equals("[59 1003 4004]") ) {
System.out.println("ERROR 7: Linked-list test failed removing elements LT");
errorFlag = true;
}
/*
* Score the Link-list portion (max points 25)
*/
if ( ! errorFlag )
assignmentScore += 25;
else
errorFlag = false;
/**************************************
*
* Testing of isPalindrone code.
*
***************************************/
// System.out.println("Starting isPalindrone testing ...");
if ( ! HW1.Stacks.isPalindrome("Was it a car or a cat I saw")) {
System.out.println("ERROR 8: ispalindrome test failed");
errorFlag = true;
}
if ( ! errorFlag && ! HW1.Stacks.isPalindrome("Race Car")) {
System.out.println("ERROR 9: ispalindrome test failed");
errorFlag = true;
}
if ( ! errorFlag && ! HW1.Stacks.isPalindrome("Never odd or even")) {
System.out.println("ERROR 10: ispalindrome test failed");
errorFlag = true;
}
if ( ! errorFlag && HW1.Stacks.isPalindrome("civic2")) {
System.out.println("ERROR 11: ispalindrome test failed");
errorFlag = true;
}
/*
* Score the isPalindrome portion (max points 25)
*/
if ( ! errorFlag )
assignmentScore += 25;
else
errorFlag = false;
/**************************************
*
* Testing of findLargestK code.
*
***************************************/
// System.out.println("Starting findLargestK testing ...");
Stack<Integer> myStack = new Stack<>();
myStack.push(5);
myStack.push(3);
myStack.push(1);
myStack.push(4);
myStack.push(1);
// System.out.println(myStack);
if ( HW1.Stacks.findLargestK(myStack, 1) != 4) {
System.out.println("ERROR 12: findLargestk test failed");
errorFlag = true;
}
if ( ! errorFlag && HW1.Stacks.findLargestK(myStack, 5) != 0) {
System.out.println("ERROR 13: findLargestk test failed");
errorFlag = true;
}
if ( ! errorFlag && HW1.Stacks.findLargestK(myStack, 4) != 3) {
System.out.println("ERROR 14: findLargestk test failed");
errorFlag = true;
}
/*
* Score the findLargestK portion (max points 26)
*/
if ( ! errorFlag )
assignmentScore += 26;
else
errorFlag = false;
/**************************************
*
* Testing of the algorithm analysis.
*
***************************************/
System.out.println("\nThe algorithm analysis testing skipped (12 points each, total 24 points) ...");
// Test code removed
System.out.println("\nTesting complete. Assignment 1 Score is: " + assignmentScore );
}
}
// Explanation: AlgorithmAnalysis1
// The first loop is O(N) and the second loop is O(M).
// Since N and M are independent variables, so we can’t say which one is
// the leading term. Therefore Time complexity of the given problem will
// be O(N+M). Since variables size does not depend on the size of the input,
// therefore Space Complexity will be constant or O(1)
// Explanation: AlgorithmAnalysis2
// If you notice, j keeps doubling till it is less than or equal
// to n. Several times, we can double a number till it is less than n would
// be log(n).
// Let’s take the examples here.
// for n = 16, j = 2, 4, 8, 16
// for n = 32, j = 2, 4, 8, 16, 32
// So, j would run for O(log n) steps.
// i runs for n/2 steps.
// So, total steps = O(n/ 2 * log (n)) = O(n*logn)