当前位置:网站首页>How to explain binary search to my sister? This is really difficult, fan!
How to explain binary search to my sister? This is really difficult, fan!
2022-07-02 13:33:00 【Java geek Technology】
Every day in the morning It's seven thirty , Push dry goods on time
Ah fan was almost stumped by her sister , Let me talk about the details slowly
What is binary search
During lunch a few days ago , The girl in the team suddenly asked me , What is binary search .
Make ah fan a little confused , What do you mean , This is testing me ? If I say the definition of binary search, wouldn't it make me too bookish , It's not fun ? This is not in line with ah fan's character , Suddenly, my brain became excited and I had an idea .
Afan : Before we talk about binary search , Let's play a game . The price of this short sleeve I wear is 100 within , Guess the actual price ? Just say the numbers , If I say too much, I'll say you say too much , If I say less, I will answer truthfully , Until you get the right answer .
sister :100 within ? yes 50 Do you ?
Afan : You said less , Guess again .
sister :75 ?
Afan : Less said , Guess again .
sister :88 ?
Afan : You guessed right this time ! Take a closer look at the process of your guess , The first guess was 50 , Why not other numbers ?
sister : Because you said the price was 100 within , if 50 Words , You told me too much , That's less than 50 I don't have to care 了 , If it is less , That's bigger than that 50 I don't have to guess the number of , In this way, you can guess the correct answer relatively faster .
Afan : You think like this , It reflects the binary search . The process of binary search is , Start with the middle element of the array , If the middle element is exactly the element to find , Then the search process is over ; If a particular element is greater than or less than the middle element , Find in the half of the array that is larger or smaller than the middle element , And it's the same as the beginning, starting from the middle element ; Until you find the number you want . Yes, of course , In the process , You may not find the number you are looking for .
sister : Oh , So this is binary search , It's quite simple , Ha ha ha ha
Afan : It's not simple , If you don't believe it , Try writing code , See if it can be realized .
As soon as ah fan finished speaking, she saw her sister trying to knock the code
For a while , The sister wrote the code :
public static int binarySearch(int[] arr, int n, int value){
int low = 0;
int high = n-1;
while (low < = high){
/ / Define intermediate values mid
int mid = low + (( high - low) >> 1);
// If arr[mid] == value , Then the middle number at this time is the desired , Just go back
// If arr[mid] != value , Change the value range
if ( arr[mid] == value){
return mid;
}else if( arr[mid] < value){
low = mid + 1;
}else{
high = mid - 1;
}
}
/ / while The required number is still not found at the end of the cycle , That is, there is no
return -1;
}
public static void main(String[] args){
int[] arr = {1,37,8,27,13,66,25,30,47,95};
/ / call BinarySearch
int binarySearch = binarySearch(arr,arr.length,27);
System.out.println(binarySearch);
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
sister : Little brother fan , Why is the program I wrote wrong , There is 27 , Why is the value given to me by the running result -1 Well
A fan has a look , It's really , What's the matter? , Mingming binarySearch There is nothing wrong with that part of the code , Why can't it run , A fan looked sweating
Suddenly ah fan saw the array defined by sister , Suddenly understand .
Afan : Do you know the premise of using binary search ?
sister : Clitoris ? Why are there preconditions ? You didn't tell me just now
Afan : Let's go back to the game we just played together , At the beginning, you guessed 50 , Then greater than 50 Words , Less than 50 Let's not consider whether it's right , So do we default that the whole array is ordered , So we can not care less than 50 The number of the , Am I right? ?
sister : You say so , That's right . So the array I defined is not ordered , When binary search this array , Will check 13 , The number I'm looking for is 27 , Greater than 13 , So the program looks back , But there is no more 27 了 , So it will return to me -1
Afan : Yes ! You are too clever, aren't you . Turn the numbers in the array into order and you can get the result you want .
sister : wow , Little brother fan , Really , The program returned me the correct result . You are really great
Ah fan blushed again after being praised ...
About binary search , Ladies and gentlemen get Did you? ~
< END >
边栏推荐
- 口袋奇兵点评
- Unity skframework framework (XIII), question module
- 量子三体问题: Landau Fall
- Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
- 能自动更新的万能周报模板,有手就会用!
- Solution: Compression Technology (original version and sequel version)
- Jerry's watch stops ringing [article]
- 挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
- Security RememberMe原理分析
- 操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
猜你喜欢
Unity SKFramework框架(十二)、Score 计分模块
Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
What are eNB, EPC and PGW?
Engineers who can't read device manuals are not good cooks
How to modify the error of easydss on demand service sharing time?
Unity skframework framework (XII), score scoring module
Unity skframework framework (XX), VFX lab special effects library
de4000h存储安装配置
Unity SKFramework框架(二十)、VFX Lab 特效库
Essential for operation and maintenance - Elk log analysis system
随机推荐
Three talking about exception -- error handling
Unity SKFramework框架(十三)、Question 问题模块
为什么switch 的default后面要跟break?
Unity skframework framework (XII), score scoring module
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
Jerry's watch time synchronization [chapter]
MAC (MacOS Monterey 12.2 M1) personal use PHP development
Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
P3807 [template] Lucas theorem /lucas theorem
How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
Unity skframework framework (XX), VFX lab special effects library
[OpenGL] notes 29. Advanced lighting (specular highlights)
I did it with two lines of code. As a result, my sister had a more ingenious way
伙伴云表格强势升级!Pro版,更非凡!
研究表明“气味相投”更易成为朋友
Ruby: how to copy variables without pointing to the same object- Ruby: how can I copy a variable without pointing to the same object?
net share
互联网常见34个术语解释
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script