Binary Search (With Code) 2
Binary Search (With Code) 2
66%
Learn to code efficiently with DSA Learn with Programiz PRO (https://programiz.pro/course/dsa-with-python)
off
PRO (https://programiz.pro/course/dsa-with-python)
ProgramizSearch tutorials & examples
(/)
www.domain-name.com
Binary Search
Binary Search is a searching algorithm for finding an
element's position in a sorted array.
1. Iterative Method
2. Recursive Method
Initial array
2. Set two pointers low and high at the lowest and the
highest positions respectively.
Thank you for printing our content at www.domain-name.com.
Setting pointers Please check back soon for new contents.
66% 3. Find
Learn to code efficiently withthe
DSAmiddle position
Learn with PROof the
mid
Programiz array ie. (https://programiz.pro/course/dsa-with-python)
off
mid = (low + high)/2 and arr[mid] = 6 .
PRO (https://programiz.pro/course/dsa-with-python)
ProgramizSearch tutorials & examples
(/)
www.domain-name.com
Mid element
Mid element
8. x = 4 is found.
Thank you for printing our content at www.domain-name.com.
Found Please check back soon for new contents.
66%
Learn to code efficiently with DSA Learn with Programiz PRO (https://programiz.pro/course/dsa-with-python)
off
PRO (https://programiz.pro/course/dsa-with-python)
ProgramizSearch tutorials & examples
Binary
(/) Search Algorithm
www.domain-name.com
Iteration Method
Recursive Method
if x == array[mid]:
return mid
else:
high = mid - 1
return -1
array = [3, 4, 5, 6, 7, 8, 9]
x = 4
if result != -1:
Python, Java, C/C++ Examples
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
else:
return -1
array = [3, 4, 5, 6, 7, 8, 9]
x = 4
Time Complexities
Space Complexity
PRO (https://programiz.pro/course/dsa-with-python)
ProgramizSearch tutorials & examples
(/)
www.domain-name.com
Next Tutorial:
(/dsa/greedy-algorithm)
Greedy Algorithm
Previous Tutorial:
(/dsa/linear-search)
Linear Search
Share on:
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/intent/tweet?
u=https://www.programiz.com/dsa/binary- text=Check%20this%20amazing%20article%20on%2
search) search)
Related Tutorials
DS & Algorithms
Linear Search
(/dsa/linear-search)
DS & Algorithms
Quicksort Algorithm
(/dsa/quick-sort)
DS & Algorithms
66%
Learn to code efficiently
DS &with DSA Learn with Programiz PRO
Algorithms (https://programiz.pro/course/dsa-with-python)
off
(/dsa/binary-search-tree)