Please, merge my PowerShell examples for all chapters (#106)

* PowerShell 01_introduction_to_algorithms example

* PowerShell 02_selection_sort example

* PowerShell 03_recursion examples

* PowerShell 04_quicksort examples

* PowerShell 05_hash_tables examples

* PowerShell 06_breadth-first_search example

* PowerShell 07_dijkstras_algorithm example

* PowerShell 08_greedy_algorithms example

* Powershell 09_dynamic_programming example
This commit is contained in:
Oleg A. Glushko
2019-03-29 07:49:20 +10:00
committed by Aditya Bhargava
parent d7de908a82
commit 06ee65d9e5
16 changed files with 436 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
function Get-GRKSum
{
param($arr)
$total = 0
foreach ($x in $arr)
{
$total += $x
}
return $total
}
Get-GRKSum (1, 2, 3, 4)

View File

@@ -0,0 +1,12 @@
function Get-GRKSum
{
param($list)
if ($list -eq $null)
{
return 0
}
return $list[0] + (Get-GRKSum ($list | Select-Object -Skip 1))
}
Get-GRKSum (1, 2, 3, 4)

View File

@@ -0,0 +1,11 @@
function Get-GRKCount
{
param($list)
if ($list -eq $null)
{
return 0
}
return 1 + (Get-GRKCount ($list | Select-Object -Skip 1))
}
Get-GRKCount ("one", "two", "three", "four")

View File

@@ -0,0 +1,27 @@
function Get-GRKMax
{
param($lst)
if ($lst.count -eq 0)
{
return $null
}
if ($lst.count -eq 1)
{
return $lst[0]
}
else
{
$sub_max = Get-GRKMax ($lst | Select-Object -Skip 1)
if ($lst[0] -gt $sub_max)
{
return $lst[0]
}
else
{
return $sub_max
}
}
}
Get-GRKMax (33, 7, 6, 100, 15, 13)

View File

@@ -0,0 +1,28 @@
function Get-GRKQuickSort
{
param($array)
if ($array.count -lt 2)
{
# base case, arrays with 0 or 1 element are already "sorted"
if ($null -ne $array)
{
return , $array
}
else
{
return $null
}
}
else
{
# recursive case
$pivot = $array[0]
# sub-array of all the elements less than the pivot
$less = @($array | Select-Object -Skip 1 | Where-Object {$_ -le $pivot})
# sub-array of all the elements greater than the pivot
$greater = @($array | Select-Object -Skip 1 | Where-Object {$_ -gt $pivot})
return @((Get-GRKQuickSort $less) + , $pivot + (Get-GRKQuickSort $greater))
}
}
Get-GRKQuickSort (5, 3, 6, 2, 10)