Loading…
Loading…
On a sorted array, jumps ahead by fixed blocks of √n until it passes the target, then scans the last block linearly.
Jump in blocks of √n = 3 for 53.
Tip: click any bar to make it the target.
1int jumpSearch(int[] arr, int target) {2 int n = arr.length, step = (int)Math.sqrt(n), prev = 0; // Block size is √n3 while (prev < n && arr[Math.min(step, n)-1] < target) { // Block end below target4 prev = step; step += (int)Math.sqrt(n); // Jump one block5 if (prev >= n) return -1; // Past array end6 }7 for (int i = prev; i < Math.min(step, n); i++) // Scan block linearly8 if (arr[i] == target) return i; // Match found9 return -1; // Not in array10}