101 Logo
onenoughtone

Code Implementation

Library Book Finder Implementation

Below is the implementation of the library book finder:

solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Recursive Binary Search
function 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 cases
const 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)}`);

Step-by-Step Explanation

Let's break down the implementation:

  1. Understand the Problem: We need to implement a binary search algorithm to find the position of a target book code in a sorted array of book codes.
  2. Identify the Recursive Structure: 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.
  3. Define the Base Cases: The base cases are when the target is found (return the index) or when the search space is empty (return -1).
  4. Implement the Recursive Solution: 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.
  5. Consider Alternative Approaches: Implement an iterative solution for comparison, which has the same time complexity but better space complexity.
ProblemSolutionCode
101 Logo
onenoughtone