Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1
A binary search orders an array into order (ascending order by value, or even alphabetical), we find the center element, and decide whether or not that center element is greater than or less than what we're looking for. We discard the elements which do not contain the value are looking for. Rinse and repeat. Working example: Find 'k': a b c d e f g h i j k l m n o p (1 + 16) / 2 = 8.5 = 9 = 'i' //finding the center element //be consistent with rounding is (i > k)? No. Discard 'i' and under. j k l m n o p (10 + 16) / 2 = 13 = 'm' //elements can keep their same index number, as shown, or the new array can be assigned to new index numbers, i.e. 1-7 is (m > k)? Yes. Discard 'm' and over. j k l (10 + 12) / 2 = 11 = 'k' 'k' found.
16th Nov 2016, 1:58 AM
Cohen Creber
Cohen Creber - avatar