-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitonic.java
74 lines (70 loc) · 2.04 KB
/
bitonic.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
/* *****************************************************************************
* Name: Alan Turing
* NetID: aturing
* Precept: P00
*
* Description: Prints 'Hello, World' to the terminal window.
* By tradition, this is everyone's first program.
* Prof. Brian Kernighan initiated this tradition in 1974.
*
**************************************************************************** */
import edu.princeton.cs.algs4.StdOut;
public class bitonic {
public static int findMax(int[] a, int lo, int hi)
{
int mid = lo + (lo + hi) / 2;
if (lo + 1 == hi || lo == hi)
{
return hi;
}
else if (mid - 1 >= 0 && mid + 1 <= hi &&
a[mid - 1] <= a[mid] && a[mid] >= a[mid + 1])
{
return mid;
}
else if (mid - 1 >= 0 && mid + 1 <= hi &&
a[mid - 1] >= a[mid] && a[mid] >= a[mid + 1])
{
return findMax(a, lo, mid - 1);
}
else if (mid - 1 >= 0 && mid + 1 <= hi &&
a[mid - 1] <= a[mid] && a[mid] <= a[mid + 1])
{
return findMax(a, mid + 1, hi);
}
return -1;
}
public static int binarySearch(int []a, int key, int lo, int hi)
{
while(lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (a[mid] < key) lo = mid + 1;
else if (a[mid] > key) hi = mid - 1;
else return mid;
}
return -1;
}
public static int find(int[] a, int key)
{
int x = findMax(a, 0, a.length);
int ret = binarySearch(a, key, 0, x);
if (ret != -1)
{
return ret;
}
ret = binarySearch(a, key, x, a.length);
if (ret != -1)
{
return ret;
}
return -1;
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5, 0, -1, -2, -3};
int x = findMax(a, 0, a.length);
StdOut.println(a[x]);
x = find(a, 3);
StdOut.println(x);
}
}