题目:
冒泡排序+二分查找法
代码:
package scan;
import java.util.Scanner;
//import java.util.Scanner;
public class Yy{
public static void main(String[] args) {
int[] arr = new int[] {22,88,44,55,66};
for (int i = 0 ; i < arr.length - 1 ; i++) {
for(int j =0 ; j < arr.length - i -1 ; j++) {
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp ;
}
}
}
for (int i = 0 ; i < arr.length ; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println();
Scanner scan = new Scanner(System.in);
for( ; ; ) {
System.out.println("请输入:");
int dest = scan.nextInt() ;
int head = 0 ;
int end = arr.length - 1 ;
boolean flag = true ;
for( ; head <= end ; ) {
int middle = (head + end)/2 ;
if(dest == arr[middle]) {
System.out.println("找到了,位置在" + middle);
flag = false ;
break ;
}else if(dest < arr[middle]) {
end = middle - 1 ;
}else {
head = middle + 1 ;
}
}
if(flag) {
System.out.println("没找到");
}
}
}
}