zoukankan      html  css  js  c++  java
  • java折半查找(递归版)

     1 import java.util.Scanner;
     2 
     3 /**
     4  * @author Administrator 递归算法折半查找
     5  */
     6 public class zhebanchazhao_1 {
     7 
     8     public static void main(String[] args) {
     9 
    10         int[] a = new int[100];
    11         int x;
    12         Scanner cin = new Scanner(System.in);
    13 
    14         x = cin.nextInt();
    15         for (int i = 0; i < x; i++) {
    16             a[i] = cin.nextInt();
    17         }
    18         int b = cin.nextInt();
    19         System.out.println(IterBiSearch(a, b, 0, x));
    20 
    21     }
    22 
    23     public static int IterBiSearch(int data[], int x, int beg, int last) {
    24         int mid = -1;
    25         mid = (beg + last) / 2;
    26         if (x == data[mid]) {
    27             return mid;
    28         } else if (x < data[mid]) {
    29             return IterBiSearch(data, x, beg, mid - 1);
    30         } else if (x > data[mid]) {
    31             return IterBiSearch(data, x, mid + 1, last);
    32         }
    33         return -1;
    34     }
    35 
    36 }
    递归折半查找
  • 相关阅读:
    实验 6 数组1输出最大值和它所对应的下标
    实验5第二题
    实验5第一题
    作业 3 应用分支与循环结构解决问题 判断是否闰年
    心得与体会1
    第七章
    第六章
    第五章
    第四章
    第一章
  • 原文地址:https://www.cnblogs.com/sxmcACM/p/3468371.html
Copyright © 2011-2022 走看看