Skip to content

Instantly share code, notes, and snippets.

@Calvindd2f
Last active September 22, 2024 07:10
Show Gist options
  • Select an option

  • Save Calvindd2f/f0ee0f9b62a87ce3ef423c3399c77ce3 to your computer and use it in GitHub Desktop.

Select an option

Save Calvindd2f/f0ee0f9b62a87ce3ef423c3399c77ce3 to your computer and use it in GitHub Desktop.
Sorts an integer array with the insertion sort algorithm.
<#
.SYNOPSIS
Sorts an integer array using the insertion sort algorithm.
.DESCRIPTION
The script implements insertion sort, a comparison-based sorting algorithm.
.PARAMETER ToSort
Specifies the array of integers to be sorted. Example: 8, 3, 2, 9, 5.
.PARAMETER Start
Specifies the start index for sorting within the array.
.PARAMETER End
Specifies the end index for sorting within the array.
.OUTPUTS
Sorted array of integers.
.EXAMPLE
PS> InsertionSort.ps1 -ToSort 4,2,3,1
1
2
3
4
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[int[]]$ToSort = @(1, 4, 2, 3, 80, 70, 3, 5),
[Parameter(Mandatory = $false)]
[int]$Start = 0,
[Parameter(Mandatory = $false)]
[int]$End = $ToSort.Length - 1
)
# Read-only variables declaration (All-Man style)
<#
read_only
####################################################
########## INPUT
####################################################
$ToSort = "Array of integers to be sorted";
$Start = "Start index of sorting";
$End = "End index of sorting";
####################################################
########## OUTPUT
####################################################
$sortedArray = "Sorted array";
####################################################
#>
# Define activity output object template (for All-Man style consistency)
$activityOutput = [pscustomobject]@{
success = $true
debug = $null
error = $null
output = $null
}
# Verify-Activity: Ensures input validity before processing
function Verify-Activity {
param (
[int[]]$Array,
[int]$StartIndex,
[int]$EndIndex
)
$activityOutput = [pscustomobject]@{
success = $true
debug = $null
error = $null
output = $null
}
try {
if ($StartIndex -gt $EndIndex) {
throw "Start index must be less than or equal to End index."
}
if ($StartIndex -lt 0) {
throw "Start index cannot be negative."
}
if ($EndIndex -ge $Array.Length) {
throw "End index exceeds array bounds."
}
} catch {
$activityOutput.success = $false
$activityOutput.error = $_.Exception.Message
$activityOutput.debug = $_.Exception
}
return $activityOutput
}
# Main-Activity: Core logic for insertion sort
function Main-Activity {
param (
[int[]]$Array,
[int]$StartIndex,
[int]$EndIndex
)
try {
for ($i = $StartIndex; $i -le $EndIndex; $i++) {
$j = $i
$temp = $Array[$i]
while ($j -gt $StartIndex -and $temp -lt $Array[$j - 1]) {
$Array[$j] = $Array[$j - 1]
$j--
}
$Array[$j] = $temp
}
$activityOutput.output = $Array
} catch {
$activityOutput.success = $false
$activityOutput.error = $_.Exception.Message
$activityOutput.debug = $_.Exception
}
return $activityOutput
}
# Execute-Activity: Integrates the verification and sorting steps
function Execute-Activity {
param (
[int[]]$ToSortArray,
[int]$StartIdx,
[int]$EndIdx
)
# Step 1: Verification
$verificationResult = Verify-Activity -Array $ToSortArray -StartIndex $StartIdx -EndIndex $EndIdx
if (-not $verificationResult.success) {
Write-Host "Verification failed: $($verificationResult.error)"
return $verificationResult
}
# Step 2: Sorting
$sortingResult = Main-Activity -Array $ToSortArray -StartIndex $StartIdx -EndIndex $EndIdx
if ($sortingResult.success) {
Write-Host "Sorting successful. Sorted array: $($sortingResult.output)"
} else {
Write-Host "Sorting failed: $($sortingResult.error)"
}
return $sortingResult
}
# Execute the script
$finalResult = Execute-Activity -ToSortArray $ToSort -StartIdx $Start -EndIdx $End
$finalResult.output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment