Free Preview
Insertion Sort Lab
Insertion Sort is an intuitive, in-place, adaptive sorting algorithm that builds a sorted array section one element at a time. It mimics the action of sorting a hand of playing cards: e...
Insertion Sort is an intuitive, in-place, adaptive sorting algorithm that builds a sorted array section one element at a time. It mimics the action of sorting a hand of playing cards: each new item is lifted from its unsorted position and shifted backward until it is inserted into its correct slot relative to the already sorted sub-array on its left. This module provides a clear visual demonstration of this shifting mechanism, highlighting adaptive data behaviors where best-case scenarios outperform standard quadratic limits.
- Incremental Loop Verification: To analyze how data structures grow a stable sorted prefix area from left to right.
- Adaptive Boundary Mapping: To demonstrate how the algorithm dynamically adapts its execution depth, reaching optimized linear bounds $$ ext{O}(N) $$on pre-sorted data profiles.
- In-Place Shift Tracing: To examine how elements slide between adjacent positions using a minimal auxiliary space envelope of $$ ext{O}(1) $$.
- Stability Conservation Analysis: To confirm that equal element keys retain their relative positions, illustrating why Insertion Sort is classified as a stable sorting algorithm.
Materials required for this lab include a computer with programming software, a visual demonstration tool, and sample datasets for sorting.
1. Set up the programming environment and load the visual demonstration tool.
2. Input sample datasets into the algorithm.
3. Observe the sorting process as elements are shifted and inserted into their correct positions.
4. Analyze the performance metrics for different data profiles.
5. Document findings on stability and efficiency.
Programming / Output Details
Logic / Description
Insertion Sort constructs its final sorted array incrementally. It maintains a running partition: everything to the left of the outer loop pointer i is kept sorted, while everything to the right remains unsorted. For each position i, the algorithm takes the element and drops it into an inner loop. This loop scans backward through the sorted section, shifting elements to the right to clear a path until the selected element finds its proper home.
Because the inner loop stops scanning the moment it hits an element that is smaller than or equal to the item being sorted, the algorithm adapts naturally to the order of the incoming data. If the array is already mostly sorted, very few shifts are required, which dramatically cuts down on the work the processor has to do. This adaptive design makes Insertion Sort a highly effective choice for cleaning up nearly sorted datasets, and a common component in hybrid sorting algorithms like Timsort.
Program Code (Raw Text)
/**
* Adaptive Insertion Sort Routine
* Data Structures and Algorithms (DSA) Workspace
* * System API Reference Hooks:
* compare(i, j) -> Evaluates if element value at index i < index j (Returns boolean)
* swap(i, j) -> Swaps positional states of element vectors at indices i and j
* markSorted(i) -> Sets element status to sorted (Emerald design state)
* N -> Global pointer representing current array element count
*/
function runInsertionSortEngine() {
// Index 0 is trivially sorted at startup
markSorted(0);
// Outer loop selects the unsorted element to be inserted into the sorted subarray
for (let i = 1; i < N; i++) {
let current_index = i;
// Inner loop slides the element leftward until it reaches its correct sorted position
while (current_index > 0 && compare(current_index, current_index - 1)) {
swap(current_index, current_index - 1);
current_index--; // Follow the element as it shifts left
}
// Lock and refresh the color-coded state of the newly sorted section
for (let k = 0; k <= i; k++) {
markSorted(k);
}
}
}
runInsertionSortEngine();
Expected Output (Raw Text)
[Sandbox] Spawning Algorithm Engine Framework... Online.
[IDE] Parsing code syntax tree structure... Compiled successfully.
[Memory] Generating randomized array values for N = 10 items.
[DAA Trace] Worst-case iteration bounds calculated: 45 comparisons.
[Runtime] Initializing thread context: runInsertionSortEngine()
[Terminal] Step 01 | API Call: markSorted(0) -> Index trivially sorted.
[Terminal] Step 02 | API Call: compare(1, 0) -> Evaluated: TRUE | Shifting left.
[Terminal] Step 03 | API Call: swap(1, 0) -> Vectors rearranged.
[Terminal] Step 04 | API Call: markSorted(0) -> Index color state updated.
[Terminal] Step 05 | API Call: markSorted(1) -> Index color state updated.
[Terminal] Step 06 | API Call: compare(2, 1) -> Evaluated: FALSE | Element home position found early. Adaptive skip applied.
[Terminal] Step 07 | API Call: markSorted(0) -> Index color state updated.
[Terminal] Step 08 | API Call: markSorted(1) -> Index color state updated.
[Terminal] Step 09 | API Call: markSorted(2) -> Index color state updated.
...
[Terminal] Step 64 | API Call: markSorted(9) -> Final array verification complete.
[Runtime] Memory array validation check: Elements strictly sorted.
[Runtime] Thread completed safely. Final stats -> Comparisons: 24, Swaps: 15.
Initializing secure sandbox environment & decrypting simulation...