Free Preview
Binary Search Lab
Binary Search is a highly efficient algorithm designed to find a specific target element within a pre-sorted linear array. It operates on the Divide-and-Conquer methodology, systematica...
Binary Search is a highly efficient algorithm designed to find a specific target element within a pre-sorted linear array. It operates on the Divide-and-Conquer methodology, systematically halving the search space by checking the midpoint element of the active range. If the target does not match the midpoint value, the half of the array where the target cannot exist is discarded. This visualizer dynamically isolates array spaces, shifting boundary pointers to help students trace structural logic trends.
- Divide-and-Conquer Trace Analysis: To track how logarithmic subdivisions partition a linear lookup grid.
- Logarithmic Bounds Evaluation: To verify experimentally that the algorithm takes at most $$ loor{rac{ ext{log}_2 N}{}} + 1 $$comparisons to locate a value or confirm its absence.
- Pointer State Monitoring: To observe the dynamic coordinate manipulation of active boundary indices (low, mid, high).
- Pruning Space Mechanics: To monitor how data spaces are pruned during target inequalities, eliminating unneeded reads.
Dynamic Interval Stage: Responsive canvas managing sorted vector structures from 5 to 40 items.
JavaScript Evaluation Sandbox: A platform for running and testing the algorithm.
1. Set up the responsive canvas with a sorted array of items.
2. Input a target value to search for within the array.
3. Observe the algorithm as it dynamically adjusts the boundary pointers (low, mid, high) during the search process.
4. Record the number of comparisons made to locate the target or confirm its absence.
5. Analyze how the search space is pruned based on target inequalities.
Programming / Output Details
Logic / Description
Binary Search takes advantage of sorted arrays to quickly isolate values. Instead of checking every item one by one from left to right like a linear search, it checks the middle value first. Because the data is sorted, comparing this middle value to the target immediately reveals which half of the array the target belongs in, allowing the algorithm to safely throw away the other half.This structural pruning dramatically minimizes the steps required as data scales up. Every single loop cuts the remaining search space exactly in half. This means that even if you increase the array size from $15$ to $1,000,000$ elements, a binary search can locate any target value in no more than $20$ total comparisons, illustrating the real-world efficiency of logarithmic scaling ($\mathcal{O}(\log N)$).
Program Code (Raw Text)
/**
* Binary Search Routine - Low, Mid, High Tracking
* Design and Analysis of Algorithms Laboratory Workspace
* * Environment API Integration Hooks:
* getTarget() -> Returns the active target value to find
* getValue(idx) -> Returns sorted element value at specified index
* markLow(idx), markHigh(idx), markMid(idx) -> Updates interface pointer tags
* discard(from, to) -> Dims eliminated index boundaries (Gray scale)
* markFound(idx) -> Highlights successful index coordinate (Emerald flash)
* N -> Global variable tracking active array item count
*/
function executeBinarySearch() {
let targetValue = getTarget();
let lowPointer = 0;
let highPointer = N - 1;
while (lowPointer <= highPointer) {
markLow(lowPointer);
markHigh(highPointer);
// Calculate midpoint with floor division
let midPointer = Math.floor((lowPointer + highPointer) / 2);
markMid(midPointer);
let midValue = getValue(midPointer);
if (midValue === targetValue) {
markFound(midPointer);
return midPointer; // Target located successfully
}
else if (midValue < targetValue) {
// Target is in the upper half; eliminate the lower half
discard(lowPointer, midPointer);
lowPointer = midPointer + 1;
}
else {
// Target is in the lower half; eliminate the upper half
discard(midPointer, highPointer);
highPointer = midPointer - 1;
}
}
return -1; // Target does not exist inside data matrix
}
executeBinarySearch();
Expected Output (Raw Text)
[Runtime] Initializing Search Simulation Frame... Online.
[Compiler] Tokenizing algorithm block... Successfully compiled.
[Memory] Creating sorted linear array vector for N = 15 components.
[Simulation] Assigned lookup search target value: 42
[Runtime] Launching execution thread: executeBinarySearch()
[Debugger] Step 01 | API Call: getTarget() -> Target value fetched: 42
[Debugger] Step 02 | API Call: markLow(0) -> Low boundary tagged.
[Debugger] Step 03 | API Call: markHigh(14) -> High boundary tagged.
[Debugger] Step 04 | API Call: markMid(7) -> Midpoint index calculated.
[Debugger] Step 05 | API Call: getValue(7) -> Read value: 28 (28 < 42)
[Debugger] Step 06 | API Call: discard(0, 7) -> Index sector cleared from view.
[Debugger] Step 07 | API Call: markLow(8) -> Low pointer incremented to 8.
[Debugger] Step 08 | API Call: markHigh(14) -> High pointer remains at 14.
[Debugger] Step 09 | API Call: markMid(11) -> Midpoint index calculated.
[Debugger] Step 10 | API Call: getValue(11) -> Read value: 51 (51 > 42)
[Debugger] Step 11 | API Call: discard(11, 14)-> Index sector cleared from view.
[Debugger] Step 12 | API Call: markLow(8) -> Low pointer remains at 8.
[Debugger] Step 13 | API Call: markHigh(10) -> High pointer decremented to 10.
[Debugger] Step 14 | API Call: markMid(9) -> Midpoint index calculated.
[Debugger] Step 15 | API Call: getValue(9) -> Read value: 42 (42 == 42)
[Debugger] Step 16 | API Call: markFound(9) -> Target location matched!
[Runtime] Array inspection complete. Index matching verified.
[Runtime] Thread closed successfully. Total index comparisons performed: 3.
Initializing secure sandbox environment & decrypting simulation...