package heap;
class ArraySearch{
public static int linSearch(int[] xs,int x){
int size = xs.length;
boolean found = false;
int i = 0;
while ((i < size) && (!found)){
if (xs[i] == x) found = true;
else i++;
}
return i;
}
public static int binSearch(int[] xs,int x){
int out = 0;
int size = xs.length;
int h = 0;
int f = size;
int c = 0;
if (size == 0) return 0;
if (x > xs[size-1]) return size;
if (x == xs[size-1]) return size-1;
if (x <= xs[0]) return 0;
// xs[0] < x < xs[size-1]
while ((c+1) != f){
h = (c+f)/2;
if (x > xs[h])
c = h;
else
f = h;
}//while
out = f;
return out;
}
public boolean equals(Object obj){
if (obj instanceof ArraySearch){
return true;
}
return false;
}
public static void main(String[] args){
int[] xs = {2};
int i = binSearch(xs,1);
System.out.println(i);
}
}
|