Below is the implementation of the library book finder:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374// Recursive Binary Searchfunction binarySearch(bookCodes, targetCode) { return binarySearchRecursive(bookCodes, targetCode, 0, bookCodes.length - 1);} function binarySearchRecursive(bookCodes, targetCode, left, right) { // Base case: target not found if (left > right) { return -1; } // Calculate middle index const mid = Math.floor((left + right) / 2); // Check if we found the target if (bookCodes[mid] === targetCode) { return mid; } // If target is smaller, search in the left half if (bookCodes[mid] > targetCode) { return binarySearchRecursive(bookCodes, targetCode, left, mid - 1); } // If target is larger, search in the right half return binarySearchRecursive(bookCodes, targetCode, mid + 1, right);} // Iterative Binary Search (for comparison)function binarySearchIterative(bookCodes, targetCode) { let left = 0; let right = bookCodes.length - 1; while (left <= right) { // Calculate middle index const mid = Math.floor((left + right) / 2); // Check if we found the target if (bookCodes[mid] === targetCode) { return mid; } // If target is smaller, search in the left half if (bookCodes[mid] > targetCode) { right = mid - 1; } // If target is larger, search in the right half else { left = mid + 1; } } // Target not found return -1;} // Test casesconst bookCodes1 = [1023, 1052, 1198, 1276, 1352, 1414, 1553];const targetCode1 = 1276;console.log(`Searching for book code ${targetCode1} in: [${bookCodes1}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes1, targetCode1)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes1, targetCode1)}`); const bookCodes2 = [2001, 2010, 2015, 2023, 2030];const targetCode2 = 2020;console.log(`\nSearching for book code ${targetCode2} in: [${bookCodes2}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes2, targetCode2)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes2, targetCode2)}`); const bookCodes3 = [3001, 3002, 3003, 3004, 3005];const targetCode3 = 3001;console.log(`\nSearching for book code ${targetCode3} in: [${bookCodes3}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes3, targetCode3)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes3, targetCode3)}`);
Let's break down the implementation:
Implement the library book finder solution in different programming languages.
Below is the implementation of the library book finder in different programming languages. Select a language tab to view the corresponding code.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374// Recursive Binary Searchfunction binarySearch(bookCodes, targetCode) { return binarySearchRecursive(bookCodes, targetCode, 0, bookCodes.length - 1);} function binarySearchRecursive(bookCodes, targetCode, left, right) { // Base case: target not found if (left > right) { return -1; } // Calculate middle index const mid = Math.floor((left + right) / 2); // Check if we found the target if (bookCodes[mid] === targetCode) { return mid; } // If target is smaller, search in the left half if (bookCodes[mid] > targetCode) { return binarySearchRecursive(bookCodes, targetCode, left, mid - 1); } // If target is larger, search in the right half return binarySearchRecursive(bookCodes, targetCode, mid + 1, right);} // Iterative Binary Search (for comparison)function binarySearchIterative(bookCodes, targetCode) { let left = 0; let right = bookCodes.length - 1; while (left <= right) { // Calculate middle index const mid = Math.floor((left + right) / 2); // Check if we found the target if (bookCodes[mid] === targetCode) { return mid; } // If target is smaller, search in the left half if (bookCodes[mid] > targetCode) { right = mid - 1; } // If target is larger, search in the right half else { left = mid + 1; } } // Target not found return -1;} // Test casesconst bookCodes1 = [1023, 1052, 1198, 1276, 1352, 1414, 1553];const targetCode1 = 1276;console.log(`Searching for book code ${targetCode1} in: [${bookCodes1}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes1, targetCode1)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes1, targetCode1)}`); const bookCodes2 = [2001, 2010, 2015, 2023, 2030];const targetCode2 = 2020;console.log(`\nSearching for book code ${targetCode2} in: [${bookCodes2}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes2, targetCode2)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes2, targetCode2)}`); const bookCodes3 = [3001, 3002, 3003, 3004, 3005];const targetCode3 = 3001;console.log(`\nSearching for book code ${targetCode3} in: [${bookCodes3}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes3, targetCode3)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes3, targetCode3)}`);
We need to implement a binary search algorithm to find the position of a target book code in a sorted array of book codes.
Recognize that this is a classic Divide & Conquer problem where we recursively search in either the left or right half of the array based on the comparison with the middle element.
The base cases are when the target is found (return the index) or when the search space is empty (return -1).
Write a function that handles the base cases and recursively searches in either the left or right half of the array based on the comparison with the middle element.
Implement an iterative solution for comparison, which has the same time complexity but better space complexity.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the library book finder problem using a different approach than shown above.
The algorithm should correctly find a target that's at the beginning of the array.
The algorithm should correctly find a target that's at the end of the array.
The algorithm should return -1 when the target is not in the array.
The algorithm should work correctly for an array with just one element.
Below is the implementation of the library book finder:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374// Recursive Binary Searchfunction binarySearch(bookCodes, targetCode) { return binarySearchRecursive(bookCodes, targetCode, 0, bookCodes.length - 1);} function binarySearchRecursive(bookCodes, targetCode, left, right) { // Base case: target not found if (left > right) { return -1; } // Calculate middle index const mid = Math.floor((left + right) / 2); // Check if we found the target if (bookCodes[mid] === targetCode) { return mid; } // If target is smaller, search in the left half if (bookCodes[mid] > targetCode) { return binarySearchRecursive(bookCodes, targetCode, left, mid - 1); } // If target is larger, search in the right half return binarySearchRecursive(bookCodes, targetCode, mid + 1, right);} // Iterative Binary Search (for comparison)function binarySearchIterative(bookCodes, targetCode) { let left = 0; let right = bookCodes.length - 1; while (left <= right) { // Calculate middle index const mid = Math.floor((left + right) / 2); // Check if we found the target if (bookCodes[mid] === targetCode) { return mid; } // If target is smaller, search in the left half if (bookCodes[mid] > targetCode) { right = mid - 1; } // If target is larger, search in the right half else { left = mid + 1; } } // Target not found return -1;} // Test casesconst bookCodes1 = [1023, 1052, 1198, 1276, 1352, 1414, 1553];const targetCode1 = 1276;console.log(`Searching for book code ${targetCode1} in: [${bookCodes1}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes1, targetCode1)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes1, targetCode1)}`); const bookCodes2 = [2001, 2010, 2015, 2023, 2030];const targetCode2 = 2020;console.log(`\nSearching for book code ${targetCode2} in: [${bookCodes2}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes2, targetCode2)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes2, targetCode2)}`); const bookCodes3 = [3001, 3002, 3003, 3004, 3005];const targetCode3 = 3001;console.log(`\nSearching for book code ${targetCode3} in: [${bookCodes3}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes3, targetCode3)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes3, targetCode3)}`);
Let's break down the implementation:
Implement the library book finder solution in different programming languages.
Below is the implementation of the library book finder in different programming languages. Select a language tab to view the corresponding code.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374// Recursive Binary Searchfunction binarySearch(bookCodes, targetCode) { return binarySearchRecursive(bookCodes, targetCode, 0, bookCodes.length - 1);} function binarySearchRecursive(bookCodes, targetCode, left, right) { // Base case: target not found if (left > right) { return -1; } // Calculate middle index const mid = Math.floor((left + right) / 2); // Check if we found the target if (bookCodes[mid] === targetCode) { return mid; } // If target is smaller, search in the left half if (bookCodes[mid] > targetCode) { return binarySearchRecursive(bookCodes, targetCode, left, mid - 1); } // If target is larger, search in the right half return binarySearchRecursive(bookCodes, targetCode, mid + 1, right);} // Iterative Binary Search (for comparison)function binarySearchIterative(bookCodes, targetCode) { let left = 0; let right = bookCodes.length - 1; while (left <= right) { // Calculate middle index const mid = Math.floor((left + right) / 2); // Check if we found the target if (bookCodes[mid] === targetCode) { return mid; } // If target is smaller, search in the left half if (bookCodes[mid] > targetCode) { right = mid - 1; } // If target is larger, search in the right half else { left = mid + 1; } } // Target not found return -1;} // Test casesconst bookCodes1 = [1023, 1052, 1198, 1276, 1352, 1414, 1553];const targetCode1 = 1276;console.log(`Searching for book code ${targetCode1} in: [${bookCodes1}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes1, targetCode1)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes1, targetCode1)}`); const bookCodes2 = [2001, 2010, 2015, 2023, 2030];const targetCode2 = 2020;console.log(`\nSearching for book code ${targetCode2} in: [${bookCodes2}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes2, targetCode2)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes2, targetCode2)}`); const bookCodes3 = [3001, 3002, 3003, 3004, 3005];const targetCode3 = 3001;console.log(`\nSearching for book code ${targetCode3} in: [${bookCodes3}]`);console.log(`Recursive Binary Search: Found at index ${binarySearch(bookCodes3, targetCode3)}`);console.log(`Iterative Binary Search: Found at index ${binarySearchIterative(bookCodes3, targetCode3)}`);
We need to implement a binary search algorithm to find the position of a target book code in a sorted array of book codes.
Recognize that this is a classic Divide & Conquer problem where we recursively search in either the left or right half of the array based on the comparison with the middle element.
The base cases are when the target is found (return the index) or when the search space is empty (return -1).
Write a function that handles the base cases and recursively searches in either the left or right half of the array based on the comparison with the middle element.
Implement an iterative solution for comparison, which has the same time complexity but better space complexity.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the library book finder problem using a different approach than shown above.
The algorithm should correctly find a target that's at the beginning of the array.
The algorithm should correctly find a target that's at the end of the array.
The algorithm should return -1 when the target is not in the array.
The algorithm should work correctly for an array with just one element.