Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
237 lines (212 loc) · 6.33 KB
/
Copy pathMain.java
File metadata and controls
237 lines (212 loc) · 6.33 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
import java.util.Arrays;
import java.util.Scanner;
import org.apache.commons.cli.*;
import static java.lang.System.*;
public class Main
{
public static void main(String[] args)
{
Options options = new Options();
Option option1 = Option.builder().hasArg().longOpt("type").build();
Option option2 = Option.builder().hasArg().longOpt("key").build();
Option option3 = Option.builder().hasArgs().longOpt("list").build();
options.addOption(option1);
options.addOption(option2);
options.addOption(option3);
String type="";
String tempKey="";
String[] tempList=null;
int size = 0;
CommandLineParser parser = new DefaultParser();
try {
CommandLine cmd = parser.parse(options, args);
type = cmd.getOptionValue("type");
tempKey = cmd.getOptionValue("key");
tempList = cmd.getOptionValues("list");
if(type == null)
throw new ParseException("Argument --type not given");
if(tempKey == null)
throw new ParseException("Argument --key not given");
if(tempList == null)
throw new ParseException("Argument --list not given");
for(String x : tempList)
size++;
}
catch(ParseException e)
{
out.println("There was an error reading arguments");
e.printStackTrace();
exit(0);
}
if(!((type.compareTo("i") != 0 && type.compareTo("s") == 0) || (type.compareTo("i") == 0 && type.compareTo("s") != 0)))
{
out.println("Unrecognized 'type' argument given. Try 'i' for integer type or 's' for String type.");
exit(0);
}
Comparable[] list = new Comparable[size];
int[] indexList = new int[size];
try
{
for(int x=0; x<size; x++) {
list[x] = tempList[x];
indexList[x] = x;
}
}
catch(Exception e)
{
out.println("-1");
System.exit(0);
}
Comparable key = tempKey;
//mergeSort(list, indexList,0, size-1);
int returnValue = binSearch(list, indexList, key);
// if(returnValue != -1)
// returnValue = findDuplicates(list, indexList, returnValue);
//String result = "The key was not found!";
// if(-1<returnValue)
// result = String.valueOf(indexList[returnValue]);
out.println(returnValue);
}
private static int binSearch(Comparable[] aList, int[] indexList, Comparable key)
{
int max = aList.length - 1;
int min = 0;
int mid;
while(min <= max)
{
mid = (min + max) /2;
if(/*Double.compare(aList[mid], key)*/aList[mid].compareTo(key) == 0)
{
return 1;
}
else if(/*aList[mid] > key*/ aList[mid].compareTo(key) > 0)
{
max = mid - 1;
}
else if(/*aList[mid] < key*/ aList[mid].compareTo(key) < 0)
{
min = mid + 1;
}
}
return 0;
}
// private static void mergeSort(Comparable[] list, int[] indexList, int l, int h)
// {
// if(l < h) {
// int mid = ((h - l) / 2) + l;
//
// mergeSort(list, indexList, l, mid);
// mergeSort(list,indexList,mid+1, h);
//
// merge(list, indexList,l, mid, h);
// }
// }
//
// private static void merge(Comparable[] list, int[] indexList, int l, int mid, int h)
// {
// int n1 = mid - l +1;
// int n2 = h - mid;
//
// Comparable[] listA = new Comparable[n1];
// Comparable[] listB = new Comparable[n2];
//
// int[] indexA = new int[n1];
// int[] indexB = new int[n2];
//
// for (int i=0; i < n1; i++)
// {
// listA[i] = list[i+l];
// indexA[i] = indexList[i+l];
// }
// for(int j=0; j < n2; j++)
// {
// listB[j] = list[mid+1+j];
// indexB[j] = indexList[mid+1+j];
// }
//
// int i=0, j=0, k=l;
//
// while(i<n1 && j<n2)
// {
// if(/*listA[i] <= listB[j]*/ listA[i].compareTo(listB[j]) <= 0)
// {
// list[k] = listA[i];
// indexList[k] = indexA[i];
// i++; k++;
// }
// else
// {
// list[k] = listB[j];
// indexList[k] = indexB[j];
// j++; k++;
// }
// }
//
// while (i < n1)
// {
// list[k] = listA[i];
// indexList[k] = indexA[i];
// i++; k++;
// }
//
// while(j < n2)
// {
// list[k] = listB[j];
// indexList[k] = indexB[j];
// j++; k++;
// }
// }
//
// private static int findDuplicates(Comparable[] list, int[] indexList, int ogIndex)
// {
// boolean rightDuplicate = true;
// boolean leftDuplicate = true;
//
// int pointerIndex = ogIndex;
// int finalIndex = ogIndex;
//
// do
// {
// ++pointerIndex;
// if(pointerIndex >= list.length)
// break;
//
// if(/*Double.compare(list[pointerIndex], list[finalIndex])*/ list[pointerIndex].compareTo(list[finalIndex]) == 0)
// {
// if(indexList[pointerIndex] < indexList[finalIndex])
// {
// finalIndex = pointerIndex;
// }
// }
// else
// {
// rightDuplicate = false;
// }
//
// }while(rightDuplicate);
//
// pointerIndex = ogIndex;
//
// do {
//
// --pointerIndex;
// if(pointerIndex <= -1)
// break;
//
// if(/*Double.compare(list[pointerIndex], list[finalIndex])*/ list[pointerIndex].compareTo(list[finalIndex]) == 0)
// {
// if(indexList[pointerIndex] < indexList[finalIndex])
// {
// finalIndex = pointerIndex;
// }
// }
// else
// {
// leftDuplicate = false;
// }
//
// }while(leftDuplicate);
//
// return finalIndex;
// }
}