A Java program that illustrates the execution of Selection Sort and Insertion Sort step-by-step on a real-world scenario: a DARPA drone swarm returning from a mission needs to be recalled in order of lowest battery first to prevent crashes. After the sorts complete, xAI's Grok analyzes the sorted result and produces a concise tactical recall plan — adding the operational layer (action window + risk level per drone) that pure sorting cannot provide.
Originally a CSC263 (Data Structures) class assignment, expanded into a portfolio piece.
- Two classic sorting algorithms, each printed step-by-step:
- Selection Sort — find the minimum in the unsorted suffix, swap into the boundary, repeat
- Insertion Sort — take the next element, shift larger elements right, insert into the sorted prefix
- Two views per algorithm:
- Full step-by-step trace (every comparison, every swap or shift)
- High-level pass-by-pass summary (textbook table style)
- ASCII bar chart of every drone's battery, color-coded by sorted/unsorted region
- Running counters for comparisons, swaps, and shifts
- Correctness verification — confirms both algorithms produce the same final order
- Agentic AI layer — Grok reads the sorted result and returns a structured tactical plan in a strict line format (
CRITICAL_COUNT,PLAN,SUMMARY); the program parses and renders it as a compact# / callsign / battery / action / risktable
git clone https://github.com/timastras9/java-sorting-darpa-demo.git
cd java-sorting-darpa-demo
cp .env.example .env # paste your xAI key inside
./run.shgit clone https://github.com/timastras9/java-sorting-darpa-demo.git
cd java-sorting-darpa-demo
copy .env.example .env
notepad .env :: paste your xAI key, save
run.batjavac SortIllustration/*.java
java -cp . SortIllustration.DroneBatterySortA pre-captured run of the program is included in
SortIllustration/run-output.txt
so you can see exactly what the program produces without running it
yourself.
Without a Grok key the program runs end-to-end normally; the tactical
layer prints (Grok offline) and is skipped.
- Java 11 or newer (developed and tested on Java 25; only uses the JDK standard library — no Maven, no Gradle, no external dependencies).
- An xAI API key (optional, for the Grok tactical layer). Get one at https://console.x.ai/
SortIllustration/
├── DroneBatterySort.java # Selection Sort + Insertion Sort + tactical layer + main
├── GrokClient.java # tiny xAI Grok REST wrapper (no SDK, java.net.http only)
└── run-output.txt # pre-captured plain-text dump of a full run
Two source files, no build system. About 500 lines of commented Java that read top to bottom.
| Method | Strategy | Best | Avg | Worst |
|---|---|---|---|---|
selectionSort |
Find min in unsorted suffix, swap to boundary | O(n²) | O(n²) | O(n²) |
insertionSort |
Shift larger elements right, drop key into the gap | O(n) | O(n²) | O(n²) |
selectionSortHighLevel |
Same algorithm, prints one line per pass (textbook view) | — | — | — |
insertionSortHighLevel |
Same algorithm, prints one line per pass (textbook view) | — | — | — |
- Insertion Sort produces the sorted array + matching callsigns.
- Both arrays are sent to Grok with a strict response schema:
CRITICAL_COUNT: <number> PLAN: - <callsign> | <action> | <CRITICAL|HIGH|MEDIUM|LOW> ... SUMMARY: <one short sentence> - The program parses each line, validates risk levels, and renders a compact color-coded table — the kind of output a mission commander would actually act on.
- Java 11+ (JDK standard library only)
- xAI Grok API (
grok-4.3) viajava.net.http.HttpClient - ANSI escape codes for color
.envfile loader built from scratch (no dotenv library needed)
MIT — do whatever you want with it.
Timothy Astras · github.com/timastras9
All original ideas for this program — the topic, the side-by-side illustration of Selection Sort and Insertion Sort, the ASCII bar chart rendering, the agentic tactical AI layer — are my own. Claude Code (Anthropic) was used as a coding assistant to help implement those ideas in Java. I directed every step, reviewed every line, ran the program, and verified the output.