二分查找
//二分查找;
package ErfenChazhao;
public class ErfenChaZhao {
public static void main(String[] args) {
//二分查找的数组必须已经排好序;
int[] Array = {1,2,3,4,5,6,7,9,56,236,489};
int num = 1;
int start = 0;
int end = Array.length - 1;
while (start <= end) {
int middle = (start + end)/2;
if (num == Array[middle]) {
System.out.println("您查找的数字是第" + middle + "个");
break;
}else if (num < Array[middle]) {
end = middle - 1;
}else if (num > Array[middle]) {
start = middle + 1;
}
}
}
}
package ErfenChazhao;
public class ErfenChaZhao {
public static void main(String[] args) {
//二分查找的数组必须已经排好序;
int[] Array = {1,2,3,4,5,6,7,9,56,236,489};
int num = 1;
int start = 0;
int end = Array.length - 1;
while (start <= end) {
int middle = (start + end)/2;
if (num == Array[middle]) {
System.out.println("您查找的数字是第" + middle + "个");
break;
}else if (num < Array[middle]) {
end = middle - 1;
}else if (num > Array[middle]) {
start = middle + 1;
}
}
}
}