Skip to main content

How to Sort an Array of Numbers (Bubble Sort) in Batch Script

While the Windows sort command is excellent for text files, it is primarily designed for strings, not numeric comparisons. For example, sort will place 100 before 2 because "1" is smaller than "2." If you have an array of numbers stored in variables (like file sizes or performance scores) and need to sort them mathematically, you must implement a sorting algorithm. The Bubble Sort is the most common and easiest to understand algorithm for this task.

In this guide, we will demonstrate how to perform a numeric sort using nested loops.

The Strategy: The Bubble Swap​

Bubble sort works by stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. The largest numbers "Bubble up" to the end of the list with every pass.

note

Bubble Sort has a time complexity of O(n²). It is ideal for small arrays of up to a few hundred elements. For larger datasets, consider padding numbers with leading zeros and using the native sort command on a temporary file.

Implementation Script​

@echo off
setlocal enabledelayedexpansion

:: 1. Define the Array (Unsorted)
set "size=5"
set "ARR_1=45"
set "ARR_2=12"
set "ARR_3=89"
set "ARR_4=3"
set "ARR_5=34"

echo Unsorted: !ARR_1! !ARR_2! !ARR_3! !ARR_4! !ARR_5!

:: 2. Bubble Sort Algorithm
:: Outer loop: Number of passes
for /L %%i in (1,1,%size%) do (
set "swapped=0"

rem Inner loop: Compare adjacent pairs
rem Limit shrinks each pass since the last elements are already sorted
set /a "limit=size - %%i"
for /L %%j in (1,1,!limit!) do (
set /a "next=%%j + 1"

rem Resolve dynamic variable names without call (faster and safer)
set "val1=!ARR_%%j!"
for %%N in (!next!) do set "val2=!ARR_%%N!"

rem Compare: If Left > Right, Swap them
if !val1! GTR !val2! (
set "ARR_%%j=!val2!"
set "ARR_!next!=!val1!"
set "swapped=1"
)
)

rem Early exit: If no swaps occurred, the array is already sorted
if !swapped!==0 goto :sorted
)

:sorted
echo Sorted: !ARR_1! !ARR_2! !ARR_3! !ARR_4! !ARR_5!

endlocal
pause

Why Use Bubble Sort in Batch?​

  1. Mathematical Accuracy: Ensures that 10 comes after 9, which the standard sort command fails to do without complex padding logic.
  2. Internal Arrays: If your script calculates values (like disk usage per user) and stores them in variables, you can sort them without ever writing to a temporary file.
  3. Visual Polish: Displaying a "Top 10" list of items in a user interface requires sorting by value.

Important Limitations​

warning

Batch arithmetic (set /a) and comparisons (if GTR) only work with signed 32-bit integers. Numbers larger than approximately 2.1 billion or any decimal values cannot be compared accurately. For such cases, use the PowerShell bridge.

  1. Performance: Bubble sort is an O(n²) algorithm. This means if you have 10 items, it is fast. If you have 1,000 items, it will be extremely slow. For massive lists, use a temporary file and the sort command (after padding numbers with leading zeros).
  2. No Decimals: Batch arithmetic (set /a) and comparisons (if GTR) only work with whole numbers (integers).
  3. Maximum Integers: Remember the 32-bit limit (~2.1 billion). Numbers larger than this cannot be compared accurately using GTR or LSS.

Best Practices​

  1. Padding Shortcut: If your numbers are all between 0 and 999, you can pad them with zeros (009, 010, 100) and then use the native sort command. This is much faster than implementing Bubble Sort for large lists.
  2. Early Exit: To optimize Bubble Sort, add a flag to track whether any swaps happened during a pass. If no swaps occur, the list is already sorted and you can skip all remaining passes. This optimization is implemented in the script above.
tip

To sort in descending order, simply change if !val1! GTR !val2! to if !val1! LSS !val2!. This reverses the comparison so the largest values bubble to the front instead of the back.

Conclusion​

Implementing the Bubble Sort algorithm adds a powerful numeric processing capability to your scripts. It allows you to transform a jumble of raw metrics into ordered, meaningful data. By understanding the "Compare and Swap" logic, you build scripts that are not just processing text, but performing intelligent data analysis and presentation in any Windows command-line environment.