Search Ready
size 16
sorted yes
key —
  • Being compared
  • Found
  • Still possible
  • Ruled out

Ready

Comparisons 0 Shifts 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

Why sorting changes everything

In an unsorted array a value could be anywhere, so the only safe method is to look at every element. In a sorted array one comparison tells you which SIDE the value must be on — everything on the other side can be ignored. That single idea turns O(n) into O(log n).

// unsorted: 42 could be anywhere
int a[] = { 17, 3, 58, 42, 8 };

// sorted: compare with the middle (17)
int b[] = { 3, 8, 17, 42, 58 };
//            42 > 17 → only { 42, 58 } is left

Linear search

  1. Start at index 0.
  2. Compare each element with the key.
  3. Stop as soon as it matches.
  4. Reached the end? Return −1.

Binary search

  1. low = 0, high = n − 1.
  2. mid = the middle of low…high.
  3. Equal → found. Key larger → low = mid + 1. Smaller → high = mid − 1.
  4. low > high → not found.

Jump search

  1. Pick a block size of √n.
  2. Check the last element of each block.
  3. Stop at the first block whose last element ≥ key.
  4. Walk through that block one by one.

Halving is powerful

  1. 16 elements → at most 5 binary steps.
  2. 1,000 elements → about 10 steps.
  3. 1,000,000 elements → about 20 steps.
  4. log₂ n grows very slowly.

A classic bug

  1. mid = (low + high) / 2 can overflow for huge arrays.
  2. Safer: mid = low + (high − low) / 2.
  3. Use low <= high (not <) in the loop.
  4. Move to mid ± 1, never to mid — or it may loop forever.

Which one to use?

  1. Small or unsorted array → linear.
  2. Sorted array with fast index access → binary.
  3. Sorted, but jumping back is costly → jump search.
  4. Many searches? Sort once, then binary search.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 LINEAR-SEARCH(A, n, key)
2 for i ← 0 to n − 1
3 if A[i] = key then return i
4 return −1
Complexity

How fast is each operation?

n = number of elements. All three searches use O(1) extra space (recursive binary search uses O(log n) for the call stack).

Operation Best Average Worst Speed
Linear search O(1) O(n) O(n) Grows with n
Binary search O(1) O(log n) O(log n) Grows with n
Jump search O(1) O(√n) O(√n) Grows with n
Sort first, then binary search O(n log n) O(n log n) O(n log n) Grows with n

Worst-case comparisons

Array size n Linear (n) Jump (≈ 2√n) Binary (≈ log₂ n)
16 16 8 5
1,000 1,000 63 10
1,000,000 1,000,000 2,000 20
1,000,000,000 1,000,000,000 63,246 30

The three methods side by side

Linear Binary Jump
Needs a sorted array? No Yes Yes
Needs jumping to any index? No Yes Yes (but only forwards)
Time (worst) O(n) O(log n) O(√n)
Works on a linked list? Yes Poorly — no direct access to the middle Poorly
Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 6 correct
Question 1

Which search can be used on an UNSORTED array?

Question 2

Binary search on [3, 8, 12, 17, 21, 25, 30]: which element is compared first?

Question 3

In binary search, arr[mid] is SMALLER than the key. What happens next?

Question 4

About how many comparisons does binary search need in the worst case for 1,000,000 sorted elements?

Question 5

What block size does jump search use for an array of n elements?

Question 6

When does binary search stop and report "not found"?

Assessment

Searching quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.