|
|
|
@ -168,6 +168,27 @@ public class BSNear {
|
|
|
|
return index;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int BSNearLeft8(int[] arr, int value) {
|
|
|
|
|
|
|
|
if (arr.length == 0 || arr == null) {
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int L = 0;
|
|
|
|
|
|
|
|
int R = arr.length - 1;
|
|
|
|
|
|
|
|
int mid = 0;
|
|
|
|
|
|
|
|
int index = -1;
|
|
|
|
|
|
|
|
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
|
|
|
|
@ -308,6 +329,26 @@ public class BSNear {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int BSNearRight6(int[] arr, int value) {
|
|
|
|
|
|
|
|
if (arr.length == 0 || arr == null) {
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int L = 0;
|
|
|
|
|
|
|
|
int R = arr.length - 1;
|
|
|
|
|
|
|
|
int mid = 0;
|
|
|
|
|
|
|
|
int index = -1;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
@ -330,7 +371,7 @@ public class BSNear {
|
|
|
|
for (int i = 0; i < testTime; i++) {
|
|
|
|
for (int i = 0; i < testTime; i++) {
|
|
|
|
int[] sortArr = randomArray(maxSize, range);
|
|
|
|
int[] sortArr = randomArray(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 = BSNearLeft7(sortArr, value);
|
|
|
|
final int res1 = BSNearLeft8(sortArr, value);
|
|
|
|
final int res2 = forTestBSNearLeft(sortArr, value);
|
|
|
|
final int res2 = forTestBSNearLeft(sortArr, value);
|
|
|
|
if (res1 != res2) {
|
|
|
|
if (res1 != res2) {
|
|
|
|
success = false;
|
|
|
|
success = false;
|
|
|
|
@ -338,8 +379,8 @@ public class BSNear {
|
|
|
|
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 = BSNearRight5(sortArr, value);
|
|
|
|
/*final int res3 = BSNearRight6(sortArr, value);
|
|
|
|
final int res4 = forTestBSNearRight(sortArr, value);
|
|
|
|
final int res4 = forTestBSNearRight(sortArr, value);
|
|
|
|
if (res3 != res4) {
|
|
|
|
if (res3 != res4) {
|
|
|
|
success = false;
|
|
|
|
success = false;
|
|
|
|
@ -347,7 +388,7 @@ public class BSNear {
|
|
|
|
System.out.println("BSNearRight=" + res3);
|
|
|
|
System.out.println("BSNearRight=" + res3);
|
|
|
|
System.out.println("forTestBSNearRight=" + res4);
|
|
|
|
System.out.println("forTestBSNearRight=" + res4);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println(success ? "Nice!!" : "Fucking Fucked!");
|
|
|
|
System.out.println(success ? "Nice!!" : "Fucking Fucked!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|