Java编程内功-数据结构与算法「二分查找」

开发 后端 算法
本篇给大家介绍Java编程数据结构与算法中二分查找的相关内容,希望能够帮助到你!

[[395207]]

需求

对于一个有序的数组进行二分查找{1,8,10,89,1000,1234},输入一个数看看该数组是否存在此数,并求出下标,如果没有就提示”没有这个数据”。

思路分析

  1. 首先确定该数组中间的下标 mid=(left+right)/2.
  2. 然后让需要查找的数findValue和arr[mid]比较,findValue > arr[mid],说明要查找的数在arr[mid]的右边,因此向右递归.findValue < arr[mid],说明要查找的数在arr[mid]的左边,因此向左递归.findValue == arr[mid],说明找打,就返回。
  3. 退出递归的条件找到就结束递归。递归完整个数组仍然没有找到findValue,需要结束递归,即当 left > right

代码案例

  1. package com.xie.search; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.Arrays; 
  5. import java.util.List; 
  6.  
  7. public class BinarySearch { 
  8.     static int count = 0; 
  9.  
  10.     public static void main(String[] args) { 
  11.         int[] arr = new int[102]; 
  12.         arr[0] = 1; 
  13.         arr[1] = 1; 
  14.         for (int i = 2; i < 102; i++) { 
  15.             arr[i] = i; 
  16.         } 
  17.         List<Integer> indexList = binarySearch(arr, 0, arr.length - 1, 1); 
  18.         System.out.println("indexList = " + indexList); 
  19.         System.out.println("查找次数:" + count); 
  20.         /* 
  21.         indexList = [1, 0] 
  22.         查找次数:6 
  23.          */ 
  24.     } 
  25.  
  26.     /** 
  27.      * 二分查找,查找符合值得所有索引集合 
  28.      * 
  29.      * @param arr       数组 
  30.      * @param left      左边索引 
  31.      * @param right     右边索引 
  32.      * @param findValue 查找的值 
  33.      * @return 找到就返回所有索引的集合,没有就返回空 
  34.      */ 
  35.     public static List<Integer> binarySearch(int[] arr, int leftint rightint findValue) { 
  36.         count++; 
  37.         List<Integer> indexList = new ArrayList<Integer>(); 
  38.         //当left > right时,说明递归完毕 
  39.         if (left > right) { 
  40.             return new ArrayList<Integer>(); 
  41.         } 
  42.         int mid = (left + right) / 2; 
  43.         int midVal = arr[mid]; 
  44.         if (findValue > midVal) { 
  45.             //查找的值比中间值大,向右递归 
  46.             return binarySearch(arr, mid + 1, right, findValue); 
  47.         } else if (findValue < midVal) { 
  48.             //查找的值比中间值小,向左递归 
  49.             return binarySearch(arr, left, mid - 1, findValue); 
  50.         } else { 
  51.             //如果找到了,再向左扫描,将满足条件的加入indexList 
  52.             int temp = mid - 1; 
  53.             while (true) { 
  54.                 if (temp < 0 || arr[temp] != findValue) { 
  55.                     break; 
  56.                 } 
  57.                 indexList.add(temp); 
  58.                 temp--; 
  59.             } 
  60.  
  61.             //再向右扫描,将满足条件的加入indexList 
  62.             temp = mid + 1; 
  63.             while (true) { 
  64.                 if (temp > right || arr[temp] != findValue) { 
  65.                     break; 
  66.                 } 
  67.                 indexList.add(temp); 
  68.                 temp++; 
  69.             } 
  70.             indexList.add(mid); 
  71.             return indexList; 
  72.         } 
  73.     } 

 【编辑推荐】

 

责任编辑:姜华 来源: 今日头条
相关推荐

2021-04-27 06:21:29

Java数据结构算法

2021-04-07 09:26:37

Java数据结构算法

2021-03-09 06:30:32

JAVA数据结构算法

2021-04-13 09:37:41

Java数据结构算法

2021-03-18 08:44:20

Java数据结构算法

2021-05-12 09:07:09

Java数据结构算法

2021-05-08 08:28:38

Java数据结构算法

2021-03-10 08:42:19

Java数据结构算法

2021-03-08 06:28:57

JAVA数据结构与算法稀疏数组

2021-03-17 09:27:36

Java数据结构算法

2021-03-12 09:13:47

Java数据结构算法

2021-03-26 08:40:28

Java数据结构算法

2021-03-23 08:33:22

Java数据结构算法

2021-04-15 09:36:44

Java数据结构算法

2021-04-22 10:07:45

Java数据结构算法

2021-03-14 08:27:40

Java数据结构算法

2021-04-16 09:40:52

Java数据结构算法

2021-03-19 10:25:12

Java数据结构算法

2021-03-29 10:13:47

Java编程数据结构算法

2021-04-01 10:34:18

Java编程数据结构算法
点赞
收藏

51CTO技术栈公众号