Skip to content

octo-space/Binary-Search-Algorithm-Implementation-Using-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Binary Search Algorithm Implementation

This repository contains a Python implementation of the Binary Search algorithm. Binary Search is a classic algorithm used to efficiently find a target value within a sorted sequence or array. It works by repeatedly dividing in half the portion of the list that could contain the target value until it isolates the value or determines that it's not present.

Overview

Binary Search is preferred for sorted arrays due to its time complexity of O(log n), making it significantly faster than linear search algorithms for large datasets. This implementation assumes the input array (arr) is sorted in ascending order.

Implementation Details

The binary_search function takes two parameters:

  • arr: A sorted list of elements where the target value will be searched.
  • target: The value to be searched within the arr.

The function returns:

  • The index of target in arr if target is present.
  • -1 if target is not found in arr.

Here is the Python code for the binary_search function:

def binary_search(arr, target):
    left = 0
    right = len(arr) - 1
    
    while left <= right:
        mid = (left + right) // 2
        
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    
    return -1

Example Usage

You can use the binary_search function as follows:

arr = [2, 4, 6, 8, 10, 12, 14, 16]
target = 16

result = binary_search(arr, target)

if result != -1:
    print(f"Element found at index {result}")
else:
    print("Element not found")

In this example:

  • arr is a sorted list of integers.
  • target is the value we want to find within arr.
  • binary_search(arr, target) returns the index of target if found, or -1 if not found. .py

Feel free to explore, modify, and integrate this Binary Search implementation into your own projects. If you have any questions, suggestions, or improvements, please feel free to open an issue or pull request. Your feedback is highly appreciated!

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages