Implementing block search in JavaScript

Block Search is a searching algorithm for sorted arrays that improves upon linear search by jumping ahead in fixed steps rather than checking every element. It combines the efficiency of skipping elements with linear search for the final location.

How Block Search Works

The algorithm divides the array into blocks of size ?n and performs these steps:

  1. Jump through blocks of size ?n until finding a block where the target might exist
  2. Perform linear search within that specific block
  3. Return the index if found, or -1 if not found
Block Search Visualization 1 0 4 1 6 2 7 3 9 4 12 5 15 6 16 7 17 8 23 9 25 10 26 11 Block 1 (?14 ? 4) Block 2 Block 3 (Target: 25) Step 1: Jump by ?n (4) ? Check indexes 0, 4, 8... Step 2: Found target range in Block 3 ? Linear search within block

Time Complexity

Block Search has a time complexity of O(?n), where n is the number of elements in the array. This makes it more efficient than linear search O(n) but less efficient than binary search O(log n).

Implementation

const arr = [1, 4, 6, 7, 9, 12, 15, 16, 17, 23, 25, 26, 27, 31];
const target = 25;

const blockSearch = (arr = [], target) => {
    let { length: len } = arr;
    let step = Math.floor(Math.sqrt(len));
    let blockStart = 0;
    let currentStep = step;
    
    // Find the block where target might exist
    while (arr[Math.min(currentStep, len) - 1] = len)
            return -1;
    }
    
    // Linear search within the block
    while (arr[blockStart] 

Array: [1, 4, 6, 7, 9, 12, 15, 16, 17, 23, 25, 26, 27, 31]
Target: 25
Index found: 10
Value at index 10: 25

Complete Example with Multiple Test Cases

function blockSearch(arr, target) {
    const len = arr.length;
    const step = Math.floor(Math.sqrt(len));
    let blockStart = 0;
    let currentStep = step;
    
    console.log(`Array length: ${len}, Block size: ${step}`);
    
    // Jump through blocks
    while (arr[Math.min(currentStep, len) - 1] = len) {
            console.log("Target not found - exceeded array bounds");
            return -1;
        }
    }
    
    console.log(`Target might be in block starting at index ${blockStart}`);
    
    // Linear search within block
    while (arr[blockStart] = Math.min(currentStep, len)) {
            console.log("Target not found in block");
            return -1;
        }
    }
    
    return arr[blockStart] === target ? blockStart : -1;
}

const testArray = [1, 4, 6, 7, 9, 12, 15, 16, 17, 23, 25, 26, 27, 31];
const testCases = [25, 1, 31, 100];

testCases.forEach(target => {
    console.log(`
--- Searching for ${target} ---`); const result = blockSearch(testArray, target); console.log(`Result: ${result >= 0 ? `Found at index ${result}` : 'Not found'}
`); });
--- Searching for 25 ---
Array length: 14, Block size: 3
Checking block ending at index 2, value: 6
Checking block ending at index 5, value: 12
Checking block ending at index 8, value: 17
Target might be in block starting at index 9
Result: Found at index 10

--- Searching for 1 ---
Array length: 14, Block size: 3
Target might be in block starting at index 0
Result: Found at index 0

--- Searching for 31 ---
Array length: 14, Block size: 3
Checking block ending at index 2, value: 6
Checking block ending at index 5, value: 12
Checking block ending at index 8, value: 17
Checking block ending at index 11, value: 26
Target might be in block starting at index 12
Result: Found at index 13

--- Searching for 100 ---
Array length: 14, Block size: 3
Checking block ending at index 2, value: 6
Checking block ending at index 5, value: 12
Checking block ending at index 8, value: 17
Checking block ending at index 11, value: 26
Checking block ending at index 13, value: 31
Target not found - exceeded array bounds
Result: Not found

Key Points

  • Optimal block size: ?n provides the best balance between jumping and linear searching
  • Prerequisites: Array must be sorted for Block Search to work correctly
  • Use case: When you need better performance than linear search but binary search isn't suitable
  • Space complexity: O(1) - uses constant extra space

Conclusion

Block Search offers a middle-ground approach between linear and binary search with O(?n) time complexity. It's particularly useful when you need predictable performance on sorted arrays and want to avoid the complexity of binary search implementation.

Updated on: 2026-03-15T23:19:00+05:30

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements