- package com.itheima;
- import java.util.Scanner;
- public class Registe {
- public static void main(String[] args) {
- numFunction();
- }
- public static void numFunction() {
- int[] arrs = {10,46,448,22,66,77,20,50,11,1,0,45,33};
- bubbleSort(arrs);
- System.out.println("输入要查找的数值");
- Scanner num = new Scanner(System.in);
- int reNum = halfSearch(arrs,num.nextInt());
- if(reNum>0)
- System.out.println("找到数值 " + arrs[reNum]);
- else
- System.out.println("数值不存在");
- }
-
- private static int halfSearch(int[] arrs, int key){
- int max = arrs.length-1,
- min = 0,
- mid;
- while(min<=max){
- mid = (max + min) >> 1;
- if(key>arrs[mid])
- min = mid+1;
- else if(key<arrs[mid])
- max = mid-1;
- else
- return mid;
- }
- return -1;
- }
-
- private static void bubbleSort(int[] arrs) {
- for (int x = 0; x < arrs.length; x++) {
- for (int y = 0; y < arrs.length - x - 1; y++) {
- if (arrs[y] > arrs[y + 1]) {
- int temp = arrs[y];
- arrs[y] = arrs[y + 1];
- arrs[y + 1] = temp;
- }
- }
- }
- }
- }
复制代码 |