0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script for Selection Sort Algorithm

Use a PowerShell script that loops through the array, finds the smallest element in the unsorted part, and swaps it with the current position using code like: for ($i=0; $i -lt $arr.Length-1; $i++) { $minIndex = $i; for ($j=$i+1; $j -lt $arr.Length; $j++) { if ($arr[$j] -lt $arr[$minIndex]) { $minIndex = $j } } if ($minIndex -ne $i) { $temp = $arr[$i]; $arr[$i] = $arr[$minIndex]; $arr[$minIndex] = $temp } }.
📋

Examples

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

How to Think About It

Think of selection sort as repeatedly picking the smallest item from the unsorted part of the list and moving it to the front. You start at the first element, find the smallest element in the rest, swap them, then move to the next element and repeat until the list is sorted.
📐

Algorithm

1
Start from the first element of the array.
2
Find the smallest element in the unsorted part of the array.
3
Swap the smallest element with the current element.
4
Move to the next element and repeat until the entire array is sorted.
💻

Code

powershell
function SelectionSort([int[]]$arr) {
    for ($i = 0; $i -lt $arr.Length - 1; $i++) {
        $minIndex = $i
        for ($j = $i + 1; $j -lt $arr.Length; $j++) {
            if ($arr[$j] -lt $arr[$minIndex]) {
                $minIndex = $j
            }
        }
        if ($minIndex -ne $i) {
            $temp = $arr[$i]
            $arr[$i] = $arr[$minIndex]
            $arr[$minIndex] = $temp
        }
    }
    return $arr
}

# Example usage
$arr = 5,3,8,1
$sorted = SelectionSort $arr
Write-Output $sorted
Output
1 3 5 8
🔍

Dry Run

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

1

First pass

Start at index 0 (value 5). Find smallest from index 0 to end: smallest is 1 at index 3. Swap 5 and 1. Array becomes [1, 3, 8, 5].

2

Second pass

At index 1 (value 3). Find smallest from index 1 to end: smallest is 3 at index 1. No swap needed. Array stays [1, 3, 8, 5].

3

Third pass

At index 2 (value 8). Find smallest from index 2 to end: smallest is 5 at index 3. Swap 8 and 5. Array becomes [1, 3, 5, 8].

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

Why This Works

Step 1: Finding the smallest element

The inner loop uses -lt to compare elements and find the smallest value in the unsorted part.

Step 2: Swapping elements

If a smaller element is found, it swaps places with the current element using a temporary variable.

Step 3: Repeating for all elements

The outer loop moves the boundary of the sorted part forward until the whole array is sorted.

🔄

Alternative Approaches

Using PowerShell's built-in Sort-Object
powershell
$arr = 5,3,8,1
$sorted = $arr | Sort-Object
Write-Output $sorted
This is simpler and faster for real use but does not teach the sorting logic.
Bubble Sort in PowerShell
powershell
function BubbleSort([int[]]$arr) {
    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
            }
        }
    }
    return $arr
}

$arr = 5,3,8,1
$sorted = BubbleSort $arr
Write-Output $sorted
Bubble sort is easier to understand but less efficient than selection sort.

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

Time Complexity

Selection sort uses two nested loops over the array, resulting in O(n^2) time because it compares each element with all others.

Space Complexity

It sorts the array in place using only a few extra variables, so space complexity is O(1).

Which Approach is Fastest?

Using PowerShell's built-in Sort-Object is fastest and simplest, but selection sort teaches the sorting logic clearly.

ApproachTimeSpaceBest For
Selection SortO(n^2)O(1)Learning sorting basics
Bubble SortO(n^2)O(1)Simple concept, less efficient
Sort-Object cmdletO(n log n)O(n)Real-world sorting tasks
💡
Always check if the minimum index changed before swapping to avoid unnecessary swaps.
⚠️
Forgetting to update the minimum index inside the inner loop causes incorrect sorting.