ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

PowerShell: Implement Insertion Sort for Small Arrays

PowerShell

Goal -- WPM

Ready
Exercise Algorithm Area
1function InsertionSort {
2param(
3[int[]]$ArrayToSort
4)
5
6$n = $ArrayToSort.Length
7
8# Handle empty or single-element array edge cases
9if ($n -le 1) {
10return $ArrayToSort
11}
12
13# Iterate from the second element (index 1)
14for ($i = 1; $i -lt $n; $i++) {
15# The element to be inserted into the sorted portion
16$currentElement = $ArrayToSort[$i]
17# The index of the last element in the sorted portion
18$j = $i - 1
19
20# Move elements of arr[0..i-1], that are greater than currentElement,
21# to one position ahead of their current position
22while ($j -ge 0 -and $ArrayToSort[$j] -gt $currentElement) {
23$ArrayToSort[$j + 1] = $ArrayToSort[$j]
24$j--
25}
26# Insert the currentElement at its correct position
27$ArrayToSort[$j + 1] = $currentElement
28}
29
30return $ArrayToSort
31}
32
33# Example Usage:
34# $myArray = @(12, 11, 13, 5, 6)
35# $sortedArray = InsertionSort $myArray
36# Write-Host "Sorted array: $($sortedArray -join ', ')"
Algorithm description viewbox

PowerShell: Implement Insertion Sort for Small Arrays

Algorithm description:

This PowerShell script implements the Insertion Sort algorithm. It builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages: simple implementation, efficient for small data sets, and efficient for data sets that are already substantially sorted.

Algorithm explanation:

Insertion Sort iterates through the input array and for each element, it finds the correct position within the already sorted portion of the array and inserts it there. The algorithm divides the input into a sorted and an unsorted region. Initially, the sorted region contains only the first element. The algorithm iterates from the second element onwards. For each element in the unsorted region, it is compared with elements in the sorted region, and if it is smaller, elements in the sorted region are shifted one position to the right to make space for the current element. This continues until the correct position is found or the beginning of the sorted region is reached. The time complexity is O(n^2) in the worst and average cases, and O(n) in the best case (already sorted array). The space complexity is O(1) as it sorts in-place. Edge cases include empty or single-element arrays, which are handled by an initial check.

Pseudocode:

Function InsertionSort(ArrayToSort):
  n = length of ArrayToSort
  If n <= 1:
    Return ArrayToSort

  For i from 1 to n - 1:
    currentElement = ArrayToSort[i]
    j = i - 1
    While j >= 0 AND ArrayToSort[j] > currentElement:
      ArrayToSort[j + 1] = ArrayToSort[j]
      j = j - 1
    ArrayToSort[j + 1] = currentElement

  Return ArrayToSort