DeveloperBreeze

Binary Search in Python

python
# Function to perform binary search on a sorted array
def binary_search(arr, target):
    low, high = 0, len(arr) - 1

    while low <= high:
        mid = (low + high) // 2

        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low = mid + 1
        else:
            high = mid - 1

    return -1

# Usage: Perform binary search on a sorted array
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
target = 5
result = binary_search(arr, target)

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
javascript

JavaScript DSA (Data Structures and Algorithms) Tutorial: A Beginner's Guide

Algorithms are step-by-step procedures or formulas for solving problems. In the context of programming, algorithms are used to manipulate data within data structures, perform calculations, and more.

Common Algorithm Types:

Aug 30, 2024
Read More
Code
javascript

Prime Number Check and Prime Number Generation in JavaScript

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Generate Fibonacci Sequence

No preview available for this content.

Jan 26, 2024
Read More
Code
java

Swap Two Numbers Without Using a Temporary Variable

No preview available for this content.

Jan 26, 2024
Read More
Code
java

Calculate Factorial

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!