在arr数组上,找满足>=value的最左位置
带有对数器的二分查找
1、查找第一个大于等于给定值的元素
public class Code05_BSNearLeft {
public static int nearestIndex ( int [ ] arr, int value) {
int L = 0 ;
int R = arr. length- 1 ;
int index = - 1 ;
while ( L <= R ) {
int mid= L + ( ( R - L ) >> 1 ) ;
if ( arr[ mid] >= value) {
index= mid;
R = mid- 1 ;
} else {
L = mid+ 1 ;
}
}
return index;
}
public static int test ( int [ ] arr, int value) {
for ( int i= 0 ; i< arr. length; i++ ) {
if ( arr[ i] >= value) {
return i;
}
}
return - 1 ;
}
public static int [ ] generateRandomArray ( int maxSize, int maxValue) {
int [ ] arr= new int [ ( int ) ( ( maxSize+ 1 ) * Math . random ( ) ) ] ;
for ( int i= 0 ; i< arr. length; i++ ) {
arr[ i] = ( int ) ( ( maxSize+ 1 ) * Math . random ( ) ) - ( int ) ( maxValue* Math . random ( ) ) ;
}
return arr;
}
public static void printArray ( int [ ] arr) {
if ( arr== null ) {
return ;
}
for ( int i= 0 ; i< arr. length; i++ ) {
System . out. print ( arr[ i] + " " ) ;
}
System . out. println ( ) ;
}
public static void main ( String [ ] args) {
int testTime= 500000 ;
int maxSize= 100 ;
int maxValue= 100 ;
boolean succeed= true ;
for ( int i= 0 ; i< testTime; i++ ) {
int [ ] arr= generateRandomArray ( maxSize, maxValue) ;
Arrays . sort ( arr) ;
int value= ( int ) ( ( maxSize+ 1 ) * Math . random ( ) ) - ( int ) ( maxValue* Math . random ( ) ) ;
if ( test ( arr, value) != nearestIndex ( arr, value) ) {
printArray ( arr) ;
System . out. println ( "目标值: " + value) ;
System . out. println ( "暴力完的下标: " + test ( arr, value) ) ;
System . out. println ( "二分法的下标: " + nearestIndex ( arr, value) ) ;
succeed= false ;
break ;
}
}
System . out. println ( succeed? "Nice!漂亮!" : "Fucking fucked!继续努力!" ) ;
}
}
2、查找最后一个小于等于给定值的元素
public class Code06_BSNearRight {
public static int nearestIndex ( int [ ] arr, int value) {
int L = 0 ;
int R = arr. length- 1 ;
int index = - 1 ;
while ( L <= R ) {
int mid= L + ( ( R - L ) >> 1 ) ;
if ( arr[ mid] <= value) {
index= mid;
L = mid+ 1 ;
} else {
R = mid- 1 ;
}
}
return index;
}
public static int test ( int [ ] arr, int value) {
for ( int i= arr. length- 1 ; i>= 0 ; i-- ) {
if ( arr[ i] <= value) {
return i;
}
}
return - 1 ;
}
public static int [ ] generateRandomArray ( int maxSize, int maxValue) {
int [ ] arr= new int [ ( int ) ( ( maxSize+ 1 ) * Math . random ( ) ) ] ;
for ( int i= 0 ; i< arr. length; i++ ) {
arr[ i] = ( int ) ( ( maxSize+ 1 ) * Math . random ( ) ) - ( int ) ( maxValue* Math . random ( ) ) ;
}
return arr;
}
public static void printArray ( int [ ] arr) {
if ( arr== null ) {
return ;
}
for ( int i= 0 ; i< arr. length; i++ ) {
System . out. print ( arr[ i] + " " ) ;
}
System . out. println ( ) ;
}
public static void main ( String [ ] args) {
int testTime= 500000 ;
int maxSize= 100 ;
int maxValue= 100 ;
boolean succeed= true ;
for ( int i= 0 ; i< testTime; i++ ) {
int [ ] arr= generateRandomArray ( maxSize, maxValue) ;
Arrays . sort ( arr) ;
int value= ( int ) ( ( maxSize+ 1 ) * Math . random ( ) ) - ( int ) ( maxValue* Math . random ( ) ) ;
if ( test ( arr, value) != nearestIndex ( arr, value) ) {
printArray ( arr) ;
System . out. println ( "目标值: " + value) ;
System . out. println ( "暴力完的下标: " + test ( arr, value) ) ;
System . out. println ( "二分法的下标: " + nearestIndex ( arr, value) ) ;
succeed= false ;
break ;
}
}
System . out. println ( succeed? "Nice!漂亮!" : "Fucking fucked!继续努力!" ) ;
}
}