|
| 1 | +package search; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +/** |
| 11 | + * |
| 12 | + * @GitHub : https://github.com/zacscoding |
| 13 | + */ |
| 14 | +public class BinarySearchTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void runTests() { |
| 18 | + ///////////////// |
| 19 | + // Case1 |
| 20 | + // stored : |
| 21 | + // new : A A A |
| 22 | + runTests0( |
| 23 | + Arrays.asList(), |
| 24 | + Arrays.asList("A", "A", "A"), |
| 25 | + 0 |
| 26 | + ); |
| 27 | + |
| 28 | + runTests0( |
| 29 | + Arrays.asList("A", "A", "A"), |
| 30 | + Arrays.asList("A", "A", "A", "B"), |
| 31 | + 3 |
| 32 | + ); |
| 33 | + |
| 34 | + runTests0( |
| 35 | + Arrays.asList("A", "A", "A"), |
| 36 | + Arrays.asList("A", "A", "A", "B", "B"), |
| 37 | + 3 |
| 38 | + ); |
| 39 | + |
| 40 | + runTests0( |
| 41 | + Arrays.asList("A", "A", "A"), |
| 42 | + Arrays.asList("A", "A", "A", "B", "B", "B"), |
| 43 | + 3 |
| 44 | + ); |
| 45 | + |
| 46 | + runTests0( |
| 47 | + Arrays.asList("A", "B", "B", "C"), |
| 48 | + Arrays.asList("A", "B1", "B1", "B1", "B1", "B1"), |
| 49 | + 1 |
| 50 | + ); |
| 51 | + |
| 52 | + runTests0( |
| 53 | + Arrays.asList("A", "A", "A"), |
| 54 | + Arrays.asList("A", "A", "A"), |
| 55 | + 3 |
| 56 | + ); |
| 57 | + |
| 58 | + runTests0( |
| 59 | + Arrays.asList("A", "A", "A", "B", "D", "E"), |
| 60 | + Arrays.asList("A", "A", "A", "C"), |
| 61 | + 3 |
| 62 | + ); |
| 63 | + |
| 64 | + runTests0( |
| 65 | + Arrays.asList("A", "B", "C", "D", "E", "F"), |
| 66 | + Arrays.asList("B", "B1", "B2"), |
| 67 | + 0 |
| 68 | + ); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + private void runTests0(List<String> stored, List<String> newEvents, int expected) { |
| 73 | + System.out.println("================================================"); |
| 74 | + int diffStartIdx = binarySearch(newEvents, stored); |
| 75 | + |
| 76 | + for (int i = 0; i < stored.size(); i++) { |
| 77 | + if (i == diffStartIdx) { |
| 78 | + System.out.print("\'"); |
| 79 | + } |
| 80 | + System.out.print(stored.get(i)); |
| 81 | + if (i == diffStartIdx) { |
| 82 | + System.out.print("\'"); |
| 83 | + } |
| 84 | + System.out.print(" "); |
| 85 | + } |
| 86 | + System.out.println(); |
| 87 | + |
| 88 | + for (int i = 0; i < newEvents.size(); i++) { |
| 89 | + if (i == diffStartIdx) { |
| 90 | + System.out.print("\'"); |
| 91 | + } |
| 92 | + System.out.print(newEvents.get(i)); |
| 93 | + if (i == diffStartIdx) { |
| 94 | + System.out.print("\'"); |
| 95 | + } |
| 96 | + System.out.print(" "); |
| 97 | + } |
| 98 | + System.out.println(); |
| 99 | + System.out.println(">> " + diffStartIdx); |
| 100 | + |
| 101 | + if (expected != diffStartIdx) { |
| 102 | + System.err.printf("Expected : %d but %d\n", expected, diffStartIdx); |
| 103 | + } |
| 104 | + System.out.println("================================================"); |
| 105 | + } |
| 106 | + |
| 107 | + private int binarySearch(List<String> stored, List<String> newEvents) { |
| 108 | + int low = 0; |
| 109 | + int high = newEvents.size() - 1; |
| 110 | + |
| 111 | + while (low <= high) { |
| 112 | + int mid = (low + high) >>> 1; |
| 113 | + |
| 114 | + if (mid >= stored.size()) { |
| 115 | + high = mid - 1; |
| 116 | + } else { |
| 117 | + String exist = stored.get(mid); |
| 118 | + String newEvent = newEvents.get(mid); |
| 119 | + |
| 120 | + if (newEvent.equals(exist)) { |
| 121 | + if (mid == newEvents.size() - 1) { |
| 122 | + return newEvents.size(); |
| 123 | + } |
| 124 | + |
| 125 | + low = mid + 1; |
| 126 | + } else { |
| 127 | + high = mid - 1; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return low; |
| 133 | + } |
| 134 | +} |
0 commit comments