How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Binary Search is a powerful algorithm used to find an element in a sorted array efficiently by repeatedly reducing the search space.
Imagine you are searching for a word in a dictionary.
๐ Instead of checking every page one by one, you:
This is exactly how Binary Search works.
Instead of checking all elements:
๐ It eliminates half of the remaining elements in every step
This makes it much faster than linear search.
๐ Binary Search works ONLY on sorted arrays
If the array is not sorted, the algorithm will not work correctly.
Initialize Pointers
left = 0right = n - 1Find Middle Element
mid = left + Math.floor((right - left) / 2);
Compare with Target
arr[mid] === target โ โ
Foundarr[mid] < target โ search right half โ left = mid + 1arr[mid] > target โ search left half โ right = mid - 1Repeat
left > rightIf Not Found
-1Binary Search becomes extremely easy when visualized:
๐ Using DrawToCode, you can:
left, mid, and right changefunction binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
let mid = Math.floor(left + (right - left) / 2);
if (arr[mid] === target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
| Scenario | Time Complexity | Explanation |
|---|---|---|
| Best Case | O(1) | Target found at middle immediately |
| Average Case | O(log n) | Search space halves each step |
| Worst Case | O(log n) | Maximum divisions until single element |
arr = [1, 3, 5, 7, 9, 11]
target = 7
๐ Output:
Index = 3
Binary Search is one of the most important algorithms for:
๐ Once you visualize how the search space shrinks, the concept becomes very intuitive
โ Written by Saurabh Patil โข B.Tech CSE โข Software Developer๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.