Bubble Sort in Powershell

Published on 14 July 2025 (Updated: 14 July 2025)

Welcome to the Bubble Sort in Powershell page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

function Show-Usage() {
    Write-Host 'Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"'
    Exit 1
}

function Parse-IntList([string]$Str) {
    @($Str.Split(",") | ForEach-Object { [int]::Parse($_) })
}

# Source: https://en.wikipedia.org/wiki/Bubble_sort#Optimizing_bubble_sort
function Invoke-BubbleSort([array]$Values) {
    $n = $Values.Length
    do {
        $newN = 0
        for ($i = 1; $i -lt $n; $i++) {
            if ($Values[$i - 1] -gt $Values[$i]) {
                $Values[$i - 1], $Values[$i] = $Values[$i], $Values[$i - 1]
                $newN = $i
            }
        }

        $n = $newN
    } while ($n -gt 1)
}

if ($args.Length -lt 1 -or -not $args[0]) {
    Show-Usage
}

try {
    $values = Parse-IntList $args[0]
    if ($values.Length -lt 2) {
        Show-Usage
    }
} catch {
    Show-Usage
}

Invoke-BubbleSort $values
Write-Output ($values -join ', ')

Bubble Sort in Powershell was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.