-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWarmup.java
More file actions
55 lines (51 loc) · 1.6 KB
/
Warmup.java
File metadata and controls
55 lines (51 loc) · 1.6 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
public class Warmup {
public static int backtrackingSearch(int[] arr, int x, int forward, int back, Stack myStack) {
int output = -1;
int trackBack = 0;
boolean found = false;
for (int i = 0; i<arr.length & !found; i++){
myStack.push(i);
trackBack++;
if (arr[i] == x){
output = i;
found = true;
} else if (trackBack % forward == 0){ //should erase else?
for (int b = 0; b<back; b++){
i = (int)myStack.pop();
}
i--;
}
}
return output;
}
public static int consistentBinSearch(int[] arr, int x, Stack myStack) {
int low = 0;
int high = arr.length-1;
boolean found = false;
int output = -1;
while (!found & high >= low){
int middle = (high + low)/2;
myStack.push(high);
myStack.push(low);
int temp = arr[middle];
if (temp == x){
output = middle;
found = true;
} else {
if (temp < x){
low = middle + 1;
}
else {
high = middle -1;
}
}
int inconsistencies = Consistency.isConsistent(arr);
while (inconsistencies > 0){ //should add output = -1, found = false?
inconsistencies--;
low = (int)myStack.pop();
high = (int)myStack.pop();
}
}
return output;
}
}