From c5c2563d0554f80025c3da26dad7d067f02b4aae Mon Sep 17 00:00:00 2001 From: itadder Date: Fri, 19 May 2017 15:18:08 -0400 Subject: [PATCH] Powershell Version of Binary Search (#15) * Powershell Version of Binary Search --- .../01_binary_search/Search-Binary.ps1 | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 01_introduction_to_algorithms/PowerShell/01_binary_search/Search-Binary.ps1 diff --git a/01_introduction_to_algorithms/PowerShell/01_binary_search/Search-Binary.ps1 b/01_introduction_to_algorithms/PowerShell/01_binary_search/Search-Binary.ps1 new file mode 100644 index 0000000..50e18d0 --- /dev/null +++ b/01_introduction_to_algorithms/PowerShell/01_binary_search/Search-Binary.ps1 @@ -0,0 +1,58 @@ +#Binary Search in Powershell / .net +#Author ITAdder (Justino Garcia) +#clear screen after every run. +clear-host + +$mylist = (1,3,5,7,9); + + +function Search-Binary +{ + #[CmdletBinding()] + #[Alias()] + #[OutputType([int])] + Param + ( + # numeric lists + [Parameter(Mandatory=$true, + ValueFromPipelineByPropertyName=$true, + Position=0)] + + [int[]]$list, + + # $item will then be passed between calls as you reduce your list + + $item + ) + + [int] $low = 0; + $high = $list.Length -1; + + while ($low -lt $high) { + + $mid = ($low + $high) / 2; + + $guess = $list[$mid]; + + + if ($guess -eq $item) { + + return $mid; + } + + if ($guess -gt $item) + { + $high = ($mid -1); + } + + else{ + + $low = $mid + 1 + } + return $null; + } + + + } + + Search-Binary -list $mylist -item 2 \ No newline at end of file