Free Preview
Selection Sort Algorithm Visualizer
Selection Sort is a fundamental comparison-based sorting algorithm designed to introduce students to array element traversal, greedy index optimization, and memory swapping behaviors. T...
Selection Sort is a fundamental comparison-based sorting algorithm designed to introduce students to array element traversal, greedy index optimization, and memory swapping behaviors. The algorithm splits a linear array into two logical boundaries: a sorted sub-segment built left-to-right, and an unsorted sub-segment covering the remaining elements. By selecting the absolute minimum element from the unsorted segment and shifting it to its permanent location, the lab visually maps complex theoretical concepts into an intuitive interactive timeline.
Greedy Min-Inversion Mapping: To isolate, monitor, and update an array's positional boundary line using a minimisation path.Empirical Complexity Proof: To verify experimentally that the algorithm uniformly logs exactly $\frac{N(N-1)}{2}$ comparison loops regardless of the data distribution curve.In-Place Mutation Analysis: To monitor the structural memory optimization of an algorithm working under an auxiliary space boundaries limit of $\mathcal{O}(1)$.Stability Breakdown Evaluation: To observe how identical keys shift positions during non-adjacent macro swaps, demonstrating why Selection Sort is structurally unstable.
No details provided.
No details provided.
Programming / Output Details
Logic / Description
Selection Sort approaches sorting with a straightforward, greedy strategy. By separating the array into an uncompressed unsorted section and a locked sorted section, it eliminates the need for complex lookaheads. The inner loop systematically scans the remaining unsorted values to find the absolute minimum. Once found, this value is swapped into the first unsorted slot, expanding the sorted boundary by one.Because the inner loop must always complete its scan to confirm it has found the true minimum, the algorithm performs the exact same number of operations regardless of whether the starting data is random, already sorted, or completely reversed. This makes it an ideal case study for demonstrating to students why certain algorithms maintain a fixed quadratic time complexity ($\mathcal{O}(N^2)$) across all execution paths.
Program Code (Raw Text)
/**
* Selection Sort - Algorithmic Optimization Routine
* Design and Analysis of Algorithms (DAA) Laboratory
* * Standard Environment Hook API:
* compare(i, j) -> Evaluates if element at index i < index j (Returns boolean)
* swap(i, j) -> Logically and visually swaps element vectors at indices i and j
* markSorted(i) -> Sets element state to permanently sorted (Emerald flag)
* N -> Global integer representing current array size
*/
function runSelectionSortEngine() {
// Outer boundary tracker isolating sorted elements on the left
for (let i = 0; i < N - 1; i++) {
let absolute_min_index = i;
// Inner scanning pointer searching the unsorted section on the right
for (let j = i + 1; j < N; j++) {
if (compare(j, absolute_min_index)) {
// Update track index when an inversion is detected
absolute_min_index = j;
}
}
// Execute a structural swap only if the minimum value isn't already in place
if (absolute_min_index !== i) {
swap(i, absolute_min_index);
}
// Finalize index slot as sorted
markSorted(i);
}
// Final remaining element is naturally locked into place
markSorted(N - 1);
}
runSelectionSortEngine();
Expected Output (Raw Text)
[Environment] Spawning Data Structures Sorting Context... Online.
[Compiler] Tokenizing algorithm source text buffer... Success.
[Memory] Allocating random scalar array bounds for N = 10 items.
[DAA Trace] Baseline comparison loop maximum calculated: 45 iterations.
[Runtime] Invoking execution thread: runSelectionSortEngine()
[Queue] Step 01 | API Call: compare(1, 0) -> Evaluated: FALSE
[Queue] Step 02 | API Call: compare(2, 0) -> Evaluated: TRUE | min_index shifted to 2
[Queue] Step 03 | API Call: compare(3, 2) -> Evaluated: FALSE
[Queue] Step 04 | API Call: compare(4, 2) -> Evaluated: FALSE
[Queue] Step 05 | API Call: compare(5, 2) -> Evaluated: FALSE
[Queue] Step 06 | API Call: compare(6, 2) -> Evaluated: FALSE
[Queue] Step 07 | API Call: compare(7, 2) -> Evaluated: FALSE
[Queue] Step 08 | API Call: compare(8, 2) -> Evaluated: FALSE
[Queue] Step 09 | API Call: compare(9, 2) -> Evaluated: FALSE
[Queue] Step 10 | API Call: swap(0, 2) -> Structural vectors mutated.
[Queue] Step 11 | API Call: markSorted(0) -> Element color state locked.
[Queue] Step 12 | API Call: compare(2, 1) -> Evaluated: TRUE | min_index shifted to 2
...
[Queue] Step 54 | API Call: markSorted(8) -> Element color state locked.
[Queue] Step 55 | API Call: markSorted(9) -> Element color state locked.
[Runtime] Sorting verification pass completed: Array is strictly ordered.
[Runtime] Script execution terminated. Final stats -> Comparisons: 45, Swaps: 6.
Initializing secure sandbox environment & decrypting simulation...