Binary Search in Powershell

Published on 07 July 2025 (Updated: 07 July 2025)

Welcome to the Binary Search in Powershell page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

function Show-Usage() {
    Write-Host 'Usage: please provide a list of sorted integers ("1, 4, 5, 11, 12") and the integer to find ("11")'
    Exit 1
}

function Parse-IntList([string]$Str) {
    @($Str.Split(",") | ForEach-Object { [int]::Parse($_) })
}

function Test-IsSorted([int[]]$arr) {
    for ($i = 1; $i -lt $arr.Length; $i++) {
        if ($arr[$i - 1] -gt $arr[$i]) {
            return $false
        }
    }

    $true
}

function Invoke-BinarySearch([int[]]$arr, [int]$target) {
    [Array]::BinarySearch($arr, $target)
}

if ($args.Length -lt 2 -or -not $args[0]) {
    Show-Usage
}

try {
    $arr = Parse-IntList $args[0]
    $target = [int]::Parse($args[1])
} catch {
    Show-Usage
}

if (-not (Test-IsSorted $arr)) {
    Show-Usage
}

$idx = Invoke-BinarySearch $arr $target
Write-Output (($idx -ge 0) ? "true" : "false")

Binary Search in Powershell was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.