public class testRun{
public static int find(int[] data, int target,int start, int end) {
int[] stored = sort(data);
if (start <= end)="">=>
int midPoint = (start + end / 2);
if (stored[midPoint] == target) {
return midPoint;
}
if (stored[midPoint] > target) {
return find(data, target, start, midPoint - 1);//return first half,exclusive itself
}
if (stored[midPoint] < target)="">
return find(data, target, start, midPoint + 1);//return last half,exclusive itself.
}
}
return -1;
}
public static int[] sort(int[] data){
// Find the smallest element in the list
for (int i = 0; i < data.length="" -1="" ;="" i++){="" generate="" the="" index="" position,not="">< b/c="" goes="" outer="">
int min = i; //assign index position to min in each iteration
for (int j = i + 1; j < data.length="" ;="">
if (data[j] < data[min]){="" if="" the="" element="" in="" the="" second="" index="" number="" smaller="">
// the element in previous index number
min = j; // assign j to the smallest number
}
}
swap (data,i,min);// data[i] = min the smallest number assign to data[i]
}
return data;
}
public static int[] swap(int[] data, int a, int b){
data[a] = b;
return data;
}
public static void main(String[] args){
int[] data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
sort(data);
find(data,6,1,5);
}
}
debug java language