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:
committed by
Aditya Bhargava
parent
d7de908a82
commit
06ee65d9e5
12
04_quicksort/PowerShell/01_loop_sum.ps1
Normal file
12
04_quicksort/PowerShell/01_loop_sum.ps1
Normal 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)
|
||||
12
04_quicksort/PowerShell/02_recursive_sum.ps1
Normal file
12
04_quicksort/PowerShell/02_recursive_sum.ps1
Normal 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)
|
||||
11
04_quicksort/PowerShell/03_recursive_count.ps1
Normal file
11
04_quicksort/PowerShell/03_recursive_count.ps1
Normal 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")
|
||||
27
04_quicksort/PowerShell/04_recursive_max.ps1
Normal file
27
04_quicksort/PowerShell/04_recursive_max.ps1
Normal 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)
|
||||
28
04_quicksort/PowerShell/05_quicksort.ps1
Normal file
28
04_quicksort/PowerShell/05_quicksort.ps1
Normal 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)
|
||||
Reference in New Issue
Block a user