|
|
|
@ -40,6 +40,51 @@ public class BSNear {
|
|
|
|
return index;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int BSNearLeft2(int[] arr, int value) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int index = -1;
|
|
|
|
|
|
|
|
if (arr.length == 0 || arr == null) {
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int L = 0;
|
|
|
|
|
|
|
|
int R = arr.length-1;
|
|
|
|
|
|
|
|
int mid = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (L <= R) {
|
|
|
|
|
|
|
|
mid = L + ((R -L ) >> 1);
|
|
|
|
|
|
|
|
if (arr[mid] >= value) {
|
|
|
|
|
|
|
|
index = mid;
|
|
|
|
|
|
|
|
R = mid - 1;
|
|
|
|
|
|
|
|
}else {
|
|
|
|
|
|
|
|
L = mid + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int BSNearLeft3(int[] arr, int value) {
|
|
|
|
|
|
|
|
int index = -1;
|
|
|
|
|
|
|
|
if (arr.length == 0 || arr == null) {
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int L = 0;
|
|
|
|
|
|
|
|
int R = arr.length - 1;
|
|
|
|
|
|
|
|
int mid = 0;
|
|
|
|
|
|
|
|
while (L <= R) {
|
|
|
|
|
|
|
|
mid = L + ((R - L) >> 1);
|
|
|
|
|
|
|
|
if (arr[mid] >= value) {
|
|
|
|
|
|
|
|
index = mid;
|
|
|
|
|
|
|
|
R = mid - 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
L = mid + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 功能描述 : 在有序数组中找出>=某个数最左侧的位置(for test)
|
|
|
|
* 功能描述 : 在有序数组中找出>=某个数最左侧的位置(for test)
|
|
|
|
* @author Leo
|
|
|
|
* @author Leo
|
|
|
|
@ -94,6 +139,27 @@ public class BSNear {
|
|
|
|
return index;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int BSNearRight2(int[] arr, int value) {
|
|
|
|
|
|
|
|
int index = -1;
|
|
|
|
|
|
|
|
if (arr.length == 0 || arr == null) {
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int L = 0;
|
|
|
|
|
|
|
|
int R = arr.length - 1;
|
|
|
|
|
|
|
|
int mid = 0;
|
|
|
|
|
|
|
|
while (L <= R) {
|
|
|
|
|
|
|
|
mid = L + ((R - L) >> 1);
|
|
|
|
|
|
|
|
if (arr[mid] <= value) {
|
|
|
|
|
|
|
|
index = mid;
|
|
|
|
|
|
|
|
L = mid + 1;
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
R = mid - 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static int forTestBSNearRight(int[] arr, int value) {
|
|
|
|
public static int forTestBSNearRight(int[] arr, int value) {
|
|
|
|
int index = -1;
|
|
|
|
int index = -1;
|
|
|
|
if (arr == null || arr.length == 0) {
|
|
|
|
if (arr == null || arr.length == 0) {
|
|
|
|
@ -116,18 +182,17 @@ public class BSNear {
|
|
|
|
for (int i = 0; i < testTime; i++) {
|
|
|
|
for (int i = 0; i < testTime; i++) {
|
|
|
|
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
|
|
|
|
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
|
|
|
|
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
|
|
|
|
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
|
|
|
|
final int res1 = BSNearLeft(sortArr, value);
|
|
|
|
/*final int res1 = BSNearLeft3(sortArr, value);
|
|
|
|
final int res2 = forTestBSNearLeft(sortArr, value);
|
|
|
|
final int res2 = forTestBSNearLeft(sortArr, value);
|
|
|
|
final int res3 = BSNearRight(sortArr, value);
|
|
|
|
|
|
|
|
final int res4 = forTestBSNearRight(sortArr, value);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (res1 != res2) {
|
|
|
|
if (res1 != res2) {
|
|
|
|
success = false;
|
|
|
|
success = false;
|
|
|
|
ArrayUtil.printArr(sortArr);
|
|
|
|
ArrayUtil.printArr(sortArr);
|
|
|
|
System.out.println("BSNearLeft=" + res1);
|
|
|
|
System.out.println("BSNearLeft=" + res1);
|
|
|
|
System.out.println("forTestBSNearLeft=" + res2);
|
|
|
|
System.out.println("forTestBSNearLeft=" + res2);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
final int res3 = BSNearRight(sortArr, value);
|
|
|
|
|
|
|
|
final int res4 = forTestBSNearRight(sortArr, value);
|
|
|
|
if (res3 != res4) {
|
|
|
|
if (res3 != res4) {
|
|
|
|
success = false;
|
|
|
|
success = false;
|
|
|
|
ArrayUtil.printArr(sortArr);
|
|
|
|
ArrayUtil.printArr(sortArr);
|
|
|
|
|