0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script for Bubble Sort - Simple Sorting Example

Use a nested loop in PowerShell to compare adjacent elements and swap them if out of order, like for ($i=0; $i -lt $arr.Length-1; $i++) { for ($j=0; $j -lt $arr.Length-$i-1; $j++) { if ($arr[$j] -gt $arr[$j+1]) { $temp = $arr[$j]; $arr[$j] = $arr[$j+1]; $arr[$j+1] = $temp } } } to bubble sort an array.
📋

Examples

Input[5, 3, 8, 1]
Output[1, 3, 5, 8]
Input[10, 9, 8, 7]
Output[7, 8, 9, 10]
Input[1]
Output[1]
🧠

How to Think About It

Bubble sort works by repeatedly stepping through the list, comparing each pair of adjacent items and swapping them if they are in the wrong order. This process repeats until no swaps are needed, meaning the list is sorted.
📐

Algorithm

1
Get the array to sort
2
Repeat for each element except the last
3
Compare each pair of adjacent elements
4
If the first is greater than the second, swap them
5
Repeat until no swaps occur in a full pass
6
Return the sorted array
💻

Code

powershell
$arr = @(5, 3, 8, 1)
for ($i = 0; $i -lt $arr.Length - 1; $i++) {
    for ($j = 0; $j -lt $arr.Length - $i - 1; $j++) {
        if ($arr[$j] -gt $arr[$j + 1]) {
            $temp = $arr[$j]
            $arr[$j] = $arr[$j + 1]
            $arr[$j + 1] = $temp
        }
    }
}
Write-Output $arr
Output
1 3 5 8
🔍

Dry Run

Let's trace sorting [5, 3, 8, 1] through the bubble sort code

1

First pass comparisons

Compare 5 and 3, swap -> [3, 5, 8, 1]; compare 5 and 8, no swap; compare 8 and 1, swap -> [3, 5, 1, 8]

2

Second pass comparisons

Compare 3 and 5, no swap; compare 5 and 1, swap -> [3, 1, 5, 8]

3

Third pass comparisons

Compare 3 and 1, swap -> [1, 3, 5, 8]

PassArray State
1[3, 5, 1, 8]
2[3, 1, 5, 8]
3[1, 3, 5, 8]
💡

Why This Works

Step 1: Compare adjacent elements

The code uses if ($arr[$j] -gt $arr[$j + 1]) to check if the current element is bigger than the next.

Step 2: Swap if needed

If the current element is bigger, it swaps them using a temporary variable to hold one value.

Step 3: Repeat passes

The outer loop repeats the process enough times to ensure all elements bubble up to their correct position.

🔄

Alternative Approaches

Using a while loop with a swapped flag
powershell
$arr = @(5, 3, 8, 1)
do {
    $swapped = $false
    for ($i = 0; $i -lt $arr.Length - 1; $i++) {
        if ($arr[$i] -gt $arr[$i + 1]) {
            $temp = $arr[$i]
            $arr[$i] = $arr[$i + 1]
            $arr[$i + 1] = $temp
            $swapped = $true
        }
    }
} while ($swapped)
Write-Output $arr
This stops early if no swaps happen, making it more efficient on nearly sorted arrays.
Using PowerShell's built-in sort
powershell
$arr = @(5, 3, 8, 1)
$arr = $arr | Sort-Object
Write-Output $arr
This is simpler and faster but does not teach the bubble sort algorithm.

Complexity: O(n^2) time, O(1) space

Time Complexity

Bubble sort uses nested loops over the array, so it takes roughly n squared steps for n elements.

Space Complexity

It sorts the array in place, so it only needs a small fixed amount of extra memory.

Which Approach is Fastest?

Using a swapped flag can reduce time on nearly sorted data, but built-in sort commands are much faster overall.

ApproachTimeSpaceBest For
Basic Bubble SortO(n^2)O(1)Learning sorting basics
Bubble Sort with Swapped FlagO(n) best, O(n^2) worstO(1)Nearly sorted arrays
PowerShell Sort-ObjectO(n log n)O(n)Real-world sorting tasks
💡
Use a swapped flag to stop early if the array is already sorted to save time.
⚠️
Forgetting to reduce the inner loop range each pass causes unnecessary comparisons.