import java.util.Random;
public class mytest {
public static void main(String[] args) {
Random rand = new Random();
int[] aa ;
int len = 100;
aa = new int[len];
for(int i = 0 ;i < len ;i++)
{
aa[i] = rand.nextInt(len);
}
//sort
for(int i = 0 ; i < len ; i ++){
for(int j = i ; j < len ; j ++){
if(aa[i]>aa[j]){
int tmp = aa[i];
aa[i] = aa[j];
aa[j] = tmp;
}
}
}
//print
for(int i = 0 ; i < len ; i ++){
System.out.println(aa[i]);
}
System.out.println("--------------------------------------------");
//find
int searchVal = 77;
int resultPos = searchInt(aa,searchVal,0,len);
System.out.println(resultPos);
System.exit(0);
}
public static int searchInt(int[] intArr,int searchV,int beginIndex,int endIndex){
int modPs = beginIndex + ( endIndex - beginIndex ) / 2;
if(intArr[modPs]==searchV){
return modPs;
}else if(endIndex < beginIndex || endIndex - beginIndex == 1){
return -1;
}else if(intArr[modPs]<searchV){
beginIndex = modPs;
return searchInt(intArr,searchV,beginIndex,endIndex);
}else{
endIndex = modPs;
return searchInt(intArr,searchV,beginIndex,endIndex);
}
}
}
public class mytest {
public static void main(String[] args) {
Random rand = new Random();
int[] aa ;
int len = 100;
aa = new int[len];
for(int i = 0 ;i < len ;i++)
{
aa[i] = rand.nextInt(len);
}
//sort
for(int i = 0 ; i < len ; i ++){
for(int j = i ; j < len ; j ++){
if(aa[i]>aa[j]){
int tmp = aa[i];
aa[i] = aa[j];
aa[j] = tmp;
}
}
}
for(int i = 0 ; i < len ; i ++){
System.out.println(aa[i]);
}
System.out.println("--------------------------------------------");
//find
int searchVal = 77;
int resultPos = searchInt(aa,searchVal,0,len);
System.out.println(resultPos);
System.exit(0);
}
public static int searchInt(int[] intArr,int searchV,int beginIndex,int endIndex){
int modPs = beginIndex + ( endIndex - beginIndex ) / 2;
if(intArr[modPs]==searchV){
return modPs;
}else if(endIndex < beginIndex || endIndex - beginIndex == 1){
return -1;
}else if(intArr[modPs]<searchV){
beginIndex = modPs;
return searchInt(intArr,searchV,beginIndex,endIndex);
}else{
endIndex = modPs;
return searchInt(intArr,searchV,beginIndex,endIndex);
}
}
}