Last active
September 22, 2024 13:38
-
-
Save Calvindd2f/513045508c37d096b2dc3cd1e998e87d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Sorts an array using the Bubble Sort algorithm. | |
| .DESCRIPTION | |
| Sorts the input array in ascending order using the Bubble Sort algorithm. | |
| .PARAMETER s | |
| The sequence of values to be sorted. Supports numbers and strings. | |
| .OUTPUTS | |
| Sorted array. | |
| .EXAMPLE | |
| $s = 4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1 | |
| Write-Output "Before Sorting:" | |
| Write-Output (bubble_sort $s) | |
| .LINK | |
| https://github.com/Calvindd2f | |
| #> | |
| # Variable Declaration | |
| <# | |
| readonly | |
| ###################################################### | |
| ########## INPUT | |
| ###################################################### | |
| $s = @(); | |
| ###################################################### | |
| ########## OUTPUT | |
| ###################################################### | |
| $activityOutput = [pscustomobject]@{ | |
| success = $true; | |
| error = $null; | |
| debug = $null; | |
| output = $null; | |
| }; | |
| #> | |
| function Verify-Activity { | |
| param( | |
| [Array]$s | |
| ) | |
| $activityOutput = [pscustomobject]@{ | |
| success = $true; | |
| error = $null; | |
| debug = $null; | |
| output = $null; | |
| } | |
| try { | |
| if (-not $s) { | |
| throw "Input array cannot be null or empty." | |
| } | |
| $activityOutput.output = $true | |
| } | |
| catch { | |
| $activityOutput.success = $false | |
| $activityOutput.error = $_.Exception.Message | |
| $activityOutput.debug = $_.Exception | |
| } | |
| return $activityOutput | |
| } | |
| function Main-Activity { | |
| param( | |
| [Array]$s | |
| ) | |
| [object]$tmp | |
| [bool]$changed | |
| for ($j = $s.Length - 1; $j -gt 0; $j--) { | |
| $changed = $false | |
| for ($i = 0; $i -lt $j; $i++) { | |
| if ($s[$i] -gt $s[$i + 1]) { | |
| $tmp = $s[$i] | |
| $s[$i] = $s[$i + 1] | |
| $s[$i + 1] = $tmp | |
| $changed = $true | |
| } | |
| } | |
| if (-not $changed) { break } | |
| } | |
| return $s | |
| } | |
| function Execute-Activity { | |
| param( | |
| [Array]$s | |
| ) | |
| $activityOutput = [pscustomobject]@{ | |
| success = $true; | |
| error = $null; | |
| debug = $null; | |
| output = $null; | |
| } | |
| try { | |
| # Verify input | |
| $verifyResult = Verify-Activity -s $s | |
| if (-not $verifyResult.success) { | |
| throw $verifyResult.error | |
| } | |
| # Perform sorting | |
| $activityOutput.output = Main-Activity -s $s | |
| } | |
| catch { | |
| $activityOutput.success = $false | |
| $activityOutput.error = $_.Exception.Message | |
| $activityOutput.debug = $_.Exception | |
| } | |
| return $activityOutput | |
| } | |
| # Example of execution | |
| $s = 4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1 | |
| Write-Output "Before Sorting:" | |
| Write-Output $s | |
| # Execution | |
| $result = Execute-Activity -s $s | |
| # Output results | |
| if ($result.success) { | |
| Write-Output "After Sorting:" | |
| $result.output | ForEach-Object { Write-Output $_ } | |
| } else { | |
| Write-Error $result.error | |
| } |
Author
Author
Explanation:
Input Validation: Verify-Activity checks if the array is valid (not null or empty).
Sorting Logic: The Main-Activity function contains the core logic of the bubble sort algorithm, iterating over the array and swapping values where necessary.
Activity Execution: The Execute-Activity function handles the full lifecycle, from verifying input to returning sorted results or an error message if the validation fails.
Structured Output: Results are captured in the activityOutput object, which is checked for success or failure and outputs the sorted array or error message accordingly.
This approach provides a modular and robust structure, improving maintainability and error handling.
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

refactor the bubble_sort function to follow a structured approach using the Verify-Activity, Main-Activity, and Execute-Activity pattern, and to ensure it's verbose and consistent with advanced error handling and output management, I have implemented the following changes:
Key Changes:
Function Structure: The sorting logic is divided between the Main-Activity and Execute-Activity.
Error Handling: Added validation for the input using Verify-Activity.
activityOutput Object: Used to track the success, debug, error, and output for logging purposes.
Refactored Script: