Powershell Version of Binary Search (#15)

* Powershell Version of Binary Search
This commit is contained in:
itadder
2017-05-19 15:18:08 -04:00
committed by Aditya Bhargava
parent e4b95bd6c4
commit c5c2563d05

View File

@@ -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