kids encyclopedia robot

Image: Binary Search Depiction

Kids Encyclopedia Facts
Original image(SVG file, nominally 470 × 200 pixels, file size: 6 KB)

Description: 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 low = 0 high = 16 middle = (0+16)/2 = 16/2 = 8 7 (target at index 4) < 14 (middle at index 8) high = middle - 1 = 7iteration 2 low = 0 high = 7 middle = (0+7)/2 = 7/2 = 3 R 1 7 (target at index 4) > 6 (middle at index 3) low = middle + 1 = 4iteration 3 low = 4 high = 7 middle = (4+7)/2 = 11/2 = 5 R 1 7 (target at index 4) < 8 (middle at index 5) high = middle - 1 = 4iteration 4 low = 4 high = 4 middle = (4+4)/2 = 8/2 = 4 7 (target at index 4) == 7 (middle at index 4)
Title: Binary Search Depiction
Credit: Own work
Author: AlwaysAngry
Usage Terms: Creative Commons Attribution-Share Alike 4.0
License: CC BY-SA 4.0
License Link: https://creativecommons.org/licenses/by-sa/4.0
Attribution Required?: Yes

The following page links to this image:

kids search engine