|
7 | 7 | *
|
8 | 8 | * @author rampatra
|
9 | 9 | * @since 5/31/15
|
10 |
| - * @time: 10:44 PM |
11 | 10 | */
|
12 | 11 | public class PivotedBinarySearch {
|
13 | 12 |
|
14 | 13 | /**
|
15 |
| - * Search an element in a sorted pivoted array {@param a}. |
| 14 | + * Search an element in a sorted pivoted array {@code arr}. |
16 | 15 | * <p/>
|
17 | 16 | * Example,
|
18 | 17 | * 1) For array [3,4,5,1,2] pivot is 5
|
19 | 18 | * 2) For array [6,7,8,5,4] pivot is 8
|
20 | 19 | *
|
21 |
| - * @param a |
| 20 | + * @param arr |
22 | 21 | * @param n
|
23 | 22 | * @return
|
24 | 23 | */
|
25 |
| - public static int pivotedBinarySearch(int a[], int n) { |
26 |
| - int pivot = findPivot(a, 0, a.length - 1); |
| 24 | + public static int pivotedBinarySearch(int[] arr, int n) { |
| 25 | + int pivot = findPivotIndex(arr, 0, arr.length - 1); |
27 | 26 |
|
28 |
| - if (pivot == -1 || a[pivot] == n) { |
| 27 | + if (pivot == -1 || arr[pivot] == n) { |
29 | 28 | return pivot;
|
30 |
| - } else if (n <= a[0]) { |
31 |
| - return BinarySearch.binarySearch(a, n, pivot + 1, a.length - 1); |
| 29 | + } else if (n < arr[0]) { |
| 30 | + return BinarySearch.binarySearch(arr, n, pivot + 1, arr.length - 1); |
32 | 31 | } else {
|
33 |
| - return BinarySearch.binarySearch(a, n, 0, pivot - 1); |
| 32 | + return BinarySearch.binarySearch(arr, n, 0, pivot - 1); |
34 | 33 | }
|
35 | 34 | }
|
36 | 35 |
|
37 | 36 | /**
|
38 |
| - * Finds the pivot element in array {@param a}. Pivot element is the only |
| 37 | + * Finds the pivot element in array {@code arr}. Pivot element is the only |
39 | 38 | * element for which next element to it is smaller than it.
|
40 | 39 | *
|
41 |
| - * @param a |
| 40 | + * @param arr |
42 | 41 | * @param low
|
43 | 42 | * @param high
|
44 |
| - * @return |
| 43 | + * @return the index of the pivot element in the {@code arr}. |
45 | 44 | */
|
46 |
| - public static int findPivot(int a[], int low, int high) { |
| 45 | + public static int findPivotIndex(int[] arr, int low, int high) { |
47 | 46 | if (low > high) return -1;
|
48 |
| - if (low == high) return low; |
49 | 47 |
|
50 | 48 | int mid = (low + high) / 2;
|
51 | 49 |
|
52 |
| - if (a[mid] > a[mid + 1] && a[mid] > a[mid - 1]) { |
| 50 | + if (mid == 0 || mid == arr.length - 1) return -1; |
| 51 | + |
| 52 | + if (arr[mid] > arr[mid + 1] && arr[mid] > arr[mid - 1]) { |
53 | 53 | return mid;
|
54 |
| - } else if (a[mid] > a[mid - 1] && a[mid] < a[mid + 1]) { |
55 |
| - return findPivot(a, mid + 1, a.length - 1); |
| 54 | + } else if (arr[mid] > arr[mid - 1] && arr[mid] < arr[mid + 1]) { |
| 55 | + return findPivotIndex(arr, mid + 1, arr.length - 1); |
56 | 56 | } else {
|
57 |
| - return findPivot(a, 0, mid - 1); |
| 57 | + return findPivotIndex(arr, 0, mid - 1); |
58 | 58 | }
|
59 | 59 | }
|
60 | 60 |
|
61 | 61 | public static void main(String[] args) {
|
62 |
| - System.out.println("Pivot: " + findPivot(new int[]{1, 2, 3, 4, 5}, 0, 3)); |
| 62 | + System.out.println("Pivot: " + findPivotIndex(new int[]{3, 4, 5, 1, 2}, 0, 4)); |
| 63 | + System.out.println("Index: " + pivotedBinarySearch(new int[]{3, 4, 5, 1, 2}, 5)); |
| 64 | + |
| 65 | + System.out.println("Pivot: " + findPivotIndex(new int[]{1, 2, 3, 4, 5}, 0, 4)); |
63 | 66 | System.out.println("Index: " + pivotedBinarySearch(new int[]{1, 2, 3, 4, 5}, 4));
|
64 | 67 |
|
65 |
| - System.out.println("Pivot: " + findPivot(new int[]{5}, 0, 0)); |
66 |
| - System.out.println("Index: " + pivotedBinarySearch(new int[]{5}, 5)); |
| 68 | + System.out.println("Pivot: " + findPivotIndex(new int[]{5, 4, 3, 2, 1}, 0, 4)); |
| 69 | + System.out.println("Index: " + pivotedBinarySearch(new int[]{5, 4, 3, 2, 1}, 4)); |
| 70 | + |
| 71 | + System.out.println("Pivot: " + findPivotIndex(new int[]{5}, 0, -1)); |
| 72 | + System.out.println("Index: " + pivotedBinarySearch(new int[]{5}, -1)); |
67 | 73 | }
|
68 | 74 | }
|
0 commit comments