|
|
|
|
@ -2,6 +2,8 @@ package leo.class01;
|
|
|
|
|
|
|
|
|
|
import leo.util.ArrayUtil;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Leo
|
|
|
|
|
* @ClassName BSNear
|
|
|
|
|
@ -125,6 +127,46 @@ public class BSNear {
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int BSNearLeft6(int[] arr, int value) {
|
|
|
|
|
if (arr.length <= 0 || arr == null) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int index = -1;
|
|
|
|
|
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 BSNearLeft7(int[] arr, int value) {
|
|
|
|
|
if (arr.length <= 0 || arr == null) {
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int L = 0;
|
|
|
|
|
int R = arr.length - 1;
|
|
|
|
|
int index = -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)
|
|
|
|
|
@ -244,6 +286,28 @@ public class BSNear {
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int BSNearRight5(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) {
|
|
|
|
|
int index = -1;
|
|
|
|
|
if (arr == null || arr.length == 0) {
|
|
|
|
|
@ -260,13 +324,13 @@ public class BSNear {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args){
|
|
|
|
|
int maxSize = 80;
|
|
|
|
|
int range = 800;
|
|
|
|
|
int range = 80;
|
|
|
|
|
int testTime = 10000;
|
|
|
|
|
boolean success = true;
|
|
|
|
|
for (int i = 0; i < testTime; i++) {
|
|
|
|
|
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
|
|
|
|
|
int[] sortArr = randomArray(maxSize, range);
|
|
|
|
|
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
|
|
|
|
|
/* final int res1 = BSNearLeft5(sortArr, value);
|
|
|
|
|
/* final int res1 = BSNearLeft7(sortArr, value);
|
|
|
|
|
final int res2 = forTestBSNearLeft(sortArr, value);
|
|
|
|
|
if (res1 != res2) {
|
|
|
|
|
success = false;
|
|
|
|
|
@ -275,7 +339,7 @@ public class BSNear {
|
|
|
|
|
System.out.println("forTestBSNearLeft=" + res2);
|
|
|
|
|
break;
|
|
|
|
|
}*/
|
|
|
|
|
final int res3 = BSNearRight4(sortArr, value);
|
|
|
|
|
final int res3 = BSNearRight5(sortArr, value);
|
|
|
|
|
final int res4 = forTestBSNearRight(sortArr, value);
|
|
|
|
|
if (res3 != res4) {
|
|
|
|
|
success = false;
|
|
|
|
|
@ -289,6 +353,16 @@ public class BSNear {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int[] randomArray(int sizeMax, int range) {
|
|
|
|
|
int[] arr = new int[(int) (sizeMax * Math.random() + 1)];
|
|
|
|
|
for (int i = 0; i < arr.length; i++) {
|
|
|
|
|
arr[i] = (int) ((range * Math.random() + 1) - (range * Math.random() + 1));
|
|
|
|
|
}
|
|
|
|
|
Arrays.sort(arr);
|
|
|
|
|
return arr;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|