|
|
|
public static void main(String[] args) { |
|
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
int[] list = new int[]{1, 2, 5, 12, 17, 23, 24, 26, 45, 55, 77, 89, 91, 93, 95, 100, 113}; |
|
verify(5, search(list, 23)); |
|
verify(5, search(list, 23)); |
|
verify(8, search(list, 45)); |
|
verify(10, search(list, 77)); |
|
verify(-1, search(list, 78)); |
|
verify(14, search(list, 95)); |
|
verify(16, search(list, 113)); |
|
verify(0, search(list, 1)); |
|
verify(-1, search(list, 3)); |
|
verify(0, search(new int[]{3}, 3)); |
|
verify(-1, search(new int[]{}, 3)); |
|
verify(-1, search(new int[]{3}, 4)); |
|
|
|
//0 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 |
|
list = new int[]{1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 8, 9, 10, 11, 11, 11, 33, 33, 44, 75, 87, 87, 87, 90, 90, 90, 90, 100}; |
|
// Lower Limit Search |
|
verify(18, searchLowerLimit(list, 23)); |
|
verify(0, searchLowerLimit(list, 1)); |
|
verify(25, searchLowerLimit(list, 90)); |
|
verify(29, searchLowerLimit(list, 100)); |
|
verify(-1, searchLowerLimit(list, 101)); |
|
verify(-1, searchLowerLimit(new int[]{}, 101)); |
|
verify(0, searchLowerLimit(new int[]{10}, 9)); |
|
verify(-1, searchLowerLimit(new int[]{10}, 11)); |
|
|
|
// Lower Bound |
|
verify(29, searchLowerBound(list, 90)); |
|
verify(12, searchLowerBound(list, 5)); |
|
verify(-1, searchLowerBound(list, 100)); |
|
verify(18, searchLowerBound(list, 11)); |
|
verify(18, searchLowerBound(list, 25)); |
|
verify(4, searchLowerBound(list, 1)); |
|
|
|
// Upper Limit Search |
|
verify(17, searchUpperLimit(list, 23)); |
|
verify(3, searchUpperLimit(list, 1)); |
|
verify(28, searchUpperLimit(list, 90)); |
|
verify(29, searchUpperLimit(list, 100)); |
|
verify(29, searchUpperLimit(list, 101)); |
|
verify(-1, searchUpperLimit(new int[]{}, 101)); |
|
verify(-1, searchUpperLimit(new int[]{10}, 9)); |
|
verify(0, searchUpperLimit(new int[]{10}, 11)); |
|
|
|
// Upper Bound Search |
|
verify(17, searchUpperBound(list, 23)); |
|
verify(-1, searchUpperBound(list, 1)); |
|
verify(24, searchUpperBound(list, 90)); |
|
verify(28, searchUpperBound(list, 100)); |
|
verify(29, searchUpperBound(list, 101)); |
|
verify(-1, searchUpperBound(new int[]{}, 101)); |
|
verify(-1, searchUpperBound(new int[]{10}, 9)); |
|
verify(0, searchUpperBound(new int[]{10}, 11)); |
|
|
|
} // main |
|
|
|
//---------------------------------------------------------------------------------- |
|
|
|
/** |
|
* Verify expected and actual values |
|
* @param expected |
|
* @param actual |
|
*/ |
|
private static void verify(int expected, int actual) { |
|
if (expected == actual) { |
|
//System.out.println("Success"); |
|
} else { |
|
System.out.println("*** Failed"); |
|
} |
|
} // verify |
|
|
|
//---------------------------------------------------------------------------------- |