File:Binary Search Depiction.svg
Summary
Description |
English: This is an example of a binary search steps through time. Source code and output:
int[] numbersArray = {1,3,4,6,7,8,10,13,14,18,19,21,24,37,40,45,71};
public boolean binarySearchAlgorithm(int targetNumber, int[] numbers)
{
int lowerLimit = 0;
int upperLimit = numbers.length - 1;
while (upperLimit >= lowerLimit)
{
int middleNumber = (lowerLimit + upperLimit)/2;
if (numbers[middleNumber] == targetNumber)
{
return true;
}
if (numbers[middleNumber] < targetNumber)
{
lowerLimit = middleNumber + 1;
}
if (numbers[middleNumber] > targetNumber)
{
upperLimit = middleNumber - 1;
}
}
return false;
}
System.out.println(binarySearchAlgorithm(7, numbersArray)); //prints "true"
iteration 1
iteration 2
iteration 3
iteration 4
|
Date | |
Source | Own work |
Author | AlwaysAngry |
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
- You are free:
- to share – to copy, distribute and transmit the work
- to remix – to adapt the work
- Under the following conditions:
- attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.