关于排序之二分查找特定元素

本文介绍了如何使用非递归及递归方式实现二分查找算法,以定位特定整数在一个有序数组中的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是从优快云的论坛上看到的一道题目,要求使用二分法从一个数组中查询出某特定数字是否存在,若存在,输出其位置。这是使用的非递归的方法实现的。
源码:
package com.zhaozy.sort;
 
import java.util.Scanner;
 
/**
 * 给定一个数组和一个特定的整数,找出整数在数组中的位置
 *
 * @author zhaozy
 *
 */
public class BinarySearch {
    public static void main(String[] args) {
       int [] dataset = new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
       Scanner s = new Scanner(System. in );
       System. out .println( " 请输入要查询的整数: " );
       int num = s.nextInt();
       int midIndex = findNum (dataset, num);
       if (midIndex == -1) {
           System. out .println( " 此数不存在! " );
       } else {
           System. out .println( " 位置为: " + midIndex);
       }
    }
 
    public static int findNum( int [] dataset, int data) {
       int beginIndex = 0;
       int endIndex = dataset. length - 1;
       int midIndex = -1;
 
       if (data < dataset[beginIndex] || data > dataset[endIndex]
              || beginIndex > endIndex) {
           return -1;
       }
       while (beginIndex <= endIndex) {
           midIndex = (beginIndex + endIndex) / 2;
           System. out .println(midIndex);
           if (data > dataset[midIndex]) {
              beginIndex = midIndex + 1;
           } else if (data < dataset[midIndex]) {
              endIndex = midIndex - 1;
           } else {
              return midIndex;
           }
       }
       return -1;
    }
}
整个程序都是使用的数组实现的,说实话,自己以前一直很看不起数组,以为他的使用范围很局限,不像集合那样的灵活,但我发现,我错了,(⊙o⊙)
还有一种是使用递归实现的,网上一直有人说递归的使用降低了代码的执行效率,以为自己没有太多深入的研究,所以两种方法都写上,以后好好研究一下。
使用递归的实现方法:
源码:
package com.zhaozy.sort;
 
import java.util.Scanner;
 
/**
 * 使用递归
 *
 * @author zhaozy
 *
 */
public class BinarySearch2 {
 
    public static void main(String[] args) {
 
       int [] dataset = new int []{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 
       Scanner scanner = new Scanner(System. in );
       System. out .println( " 请输入要查找的数据: " );
       int data = scanner.nextInt();
 
       BinarySearch2 search = new BinarySearch2();
       int position = search.search(dataset, data, 0, dataset. length - 1);
 
       if (position == -1) {
           System. out .println( " 当前数组中没有此数据! " );
       } else {
           System. out .println( " 在数组中的位置为: " + position);
       }
    }
    /**
     *
     * @param dataset
     * @param data
     * @param beginIndex
     * @param endIndex
     * @return 要找的值在数组中的位置
     */
    public int search( int [] dataset, int data, int beginIndex, int endIndex) {
       // 取出中间的数据,作为标准进行判断
       int midIndex = (beginIndex + endIndex) / 2;
       // 如果中间的数据大于数组的最大值 || 中间的数据小于数组的最小值 ||beginIndex>endIndex 直接返回没有找到的信息
       if (data > dataset[endIndex] || data < dataset[beginIndex]
              || beginIndex > endIndex) {
           return -1;
       }
 
       if (data < dataset[midIndex]) {
           return this .search(dataset, data, beginIndex, midIndex - 1);
       } else if (data > dataset[midIndex]) {
           return this .search(dataset, data, midIndex + 1, endIndex);
       } else {
           return midIndex;
       }
    }
}
 

 

这是从优快云的论坛上看到的一道题目,要求使用二分法从一个数组中查询出某特定数字是否存在,若存在,输出其位置。
源码:
package com.zhaozy.sort;
 
import java.util.Scanner;
 
/**
 * 给定一个数组和一个特定的整数,找出整数在数组中的位置
 *
 * @author zhaozy
 *
 */
public class BinarySearch {
    public static void main(String[] args) {
       int [] dataset = new int [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
       Scanner s = new Scanner(System. in );
       System. out .println( " 请输入要查询的整数: " );
       int num = s.nextInt();
       int midIndex = findNum (dataset, num);
       if (midIndex == -1) {
           System. out .println( " 此数不存在! " );
       } else {
           System. out .println( " 位置为: " + midIndex);
       }
    }
 
    public static int findNum( int [] dataset, int data) {
       int beginIndex = 0;
       int endIndex = dataset. length - 1;
       int midIndex = -1;
 
       if (data < dataset[beginIndex] || data > dataset[endIndex]
              || beginIndex > endIndex) {
           return -1;
       }
       while (beginIndex <= endIndex) {
           midIndex = (beginIndex + endIndex) / 2;
           System. out .println(midIndex);
           if (data > dataset[midIndex]) {
              beginIndex = midIndex + 1;
           } else if (data < dataset[midIndex]) {
              endIndex = midIndex - 1;
           } else {
              return midIndex;
           }
       }
       return -1;
    }
}
整个程序都是使用的数组实现的,说实话,自己以前一直很看不起数组,以为他的使用范围很局限,不像集合那样的灵活,但我发现,我错了,(⊙o⊙)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值