Comparisons Log

Time Complexity O(n)
Space Complexity O(1)
Status Waiting

Search Algorithms

Linear & Binary Search

How it Works: Searching algorithms look for a specific element in an array. Linear search checks every element sequentially, while Binary search quickly halves a sorted array to find the target.

Instructions:

  • Sort manually: Drag and drop any array element to swap its position horizontally.
  • Binary Search Rule: The array MUST be sorted first!
  • Observe Comparisons: The log panel will show the exact code comparison made at each step.

Linear Search Pseudocode

FOR i = 0 TO length - 1:
  IF array[i] == target:
    RETURN i
RETURN -1

Binary Search Pseudocode

low = 0, high = length - 1
WHILE low <= high:
  mid = Math.floor((low + high) / 2)
  IF array[mid] == target:
    RETURN mid
  ELSE IF array[mid] < target:
    low = mid + 1
  ELSE:
    high = mid - 1
RETURN -1

Exercise Syntax:

To use the Exercise Mode, type the code syntax:

  • linearSearch(target)
  • binarySearch(target)
Designed by NDAHI CO. LTD
1.0x
10