当前位置:网站首页>二分法
二分法
2022-08-09 15:36:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
package 算法;
import java.util.Arrays;
public class 二分法 {
public static void main(String[] args) { // TODO Auto-generated method stub int[] arr={12,3,34,5,34,5,5,6,2,9,3}; int bar=34;//要找的元素 Arrays.sort(arr); int min=0; int max=arr.length-1; boolean ison=true; System.out.println(“e”); while(ison){ if(arr[(min+max)/2]==bar){ ison=false; System.out.println(“找到了”); break; } if(arr[(min+max)/2]>bar){ max=(min+max)/2-1; } if(arr[(min+max)/2]<bar){ min=(min+max)/2+1; } if(max-min<=0){ ison=false; System.out.println(“没找到”); } } if(ison)System.out.println(“没找到”); }
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106027.html原文链接:https://javaforall.cn
边栏推荐
猜你喜欢
第一篇博客
Swagger2 knife4j NullPointerException 空指针问题
Chapter 2: Creating Interactive Maps (2.4-2.6)
想通这点,治好 AI 打工人的精神内耗
Access Characteristics of Constructor under Inheritance Relationship
智慧灯杆网关智慧交通应用
Video chat source code - how to improve the quality of one-to-one live broadcast?
C语言分支语句if,switch语句详细讲解
C语言初印象(1.2w字粗略讲讲C)
初识C语言,了解一下C语言轮廓
随机推荐
一个程序员的水平能差到什么程度?
Qt学习第二天
2022年8月9日:用C#生成.NET应用程序--使用 Visual Studio Code 调试器,以交互方式调试 .NET 应用(不会,失败)
Chapter 3: Use of GEE Data (3.4-3.11)
Base64工具类
网络——IPV4地址(一)
六.数组越界问题引出对栈区内存的探索
Why does a four-byte float represent a wider range than an eight-byte long
uniapp 项目搭建
网络——路由器
C语言三大循环while,for,do...while
【科普】关于平板电脑的那些事
2022年深圳杯数学建模A题代码思路-- 破除“尖叫效应”与“回声室效应”,走出“信息茧房”
Video chat source code - how to improve the quality of one-to-one live broadcast?
Excel相关处理工具类
【Web渗透】信息收集篇——Google搜索引擎(一)
【Chinese and English Catalog】Introduction
Access Characteristics of Constructor under Inheritance Relationship
Three ways to find prime numbers
初始C语言 C生万物