Algorithm Visualizer

Binary Search


Binary search is an efficient algorithm for finding an element's position in a sorted array. It works by repeatedly dividing the search interval in half until the key is found or the interval becomes empty.

TIME COMPLEXITY: O(log N)What is time complexity?

PSEUDO CODE:

Set the first element as the sorted element

For each unsorted element X

For j = lastSortedIndex down to 0

If current element > X

Move the current element

Break the loop and insert X