Skip to content

Cocodernero/binarysplitter.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

binarysplitter.py

This repository contains a Python implementation of the Binary Search algorithm, a classic searching algorithm used to find the position of an element in a sorted list. The function takes a list and a target element as input and returns the index of the target element if found, otherwise -1. Function Details

The binary_search function performs a binary search on the input list to find the target element efficiently. The function uses the following parameters:

list: The input list in which the target element is to be found. The list should be sorted in ascending order.
element: The target element that needs to be located within the list.

Algorithm Steps

Initialize variables:
    middle: To keep track of the middle index during each iteration.
    start: The starting index of the current search space.
    end: The ending index of the current search space.
    steps: A counter to keep track of the number of steps taken during the search process.

Perform a while loop while the start index is less than or equal to the end index.

Calculate the middle index as the floor division of the sum of start and end divided by 2.

Compare the element with the value at the middle index in the list.
    If the element is found at the middle index, return the index.
    If the element is less than the value at the middle index, update the end index to middle - 1.
    If the element is greater than the value at the middle index, update the start index to middle + 1.

Repeat steps 3 and 4 until the element is found or the search space becomes empty.

If the element is not found in the list, return -1.

Usage

To use the binary_search function, follow these steps:

Ensure you have Python installed on your system.

Copy the binary_search function into your Python script or import it from this module.

Create a sorted list my_list containing the elements in which you want to search.

Define the target element that you want to find in the list.

Call the binary_search function with the my_list and target as arguments.

The function will return the index of the target element if found; otherwise, it will return -1.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages