Master fundamental searching algorithms through hands-on implementation and practical exercises.
Search algorithms are fundamental techniques used to find specific elements within data structures. In this guided project, we'll focus on implementing and understanding different search approaches, from basic linear search to more optimized methods.
Linear search is the simplest searching algorithm that works by sequentially checking each element in a collection until the target element is found or the collection is exhausted.
Set up array traversal and target value
public int linearSearch(int[] arr, int target) {
// Check for null or empty array
if (arr == null || arr.length == 0) {
return -1; // Element not found
}
// Traverse the array
for (int i = 0; i < arr.length; i++) {
// Return index when target is found
if (arr[i] == target) {
return i;
}
}
// Element not found after traversal
return -1;
}
Traverse array and compare elements
The search logic is straightforward:
Consider empty arrays and element not found scenarios
Let's visualize a linear search for target value 7 in array [5, 2, 9, 7, 3, 1, 6]:
Step | Array Position | Value | Comparison |
---|---|---|---|
1 | arr[0] | 5 | 5 ≠ 7 (continue) |
2 | arr[1] | 2 | 2 ≠ 7 (continue) |
3 | arr[2] | 9 | 9 ≠ 7 (continue) |
4 | arr[3] | 7 | 7 = 7 (found!) |
Result: Target found at index 3
Binary search is an efficient algorithm for finding an element in a sorted array. It works by repeatedly dividing the search interval in half.
Initialize left and right pointers
public int binarySearch(int[] arr, int target) {
// Check for null or empty array
if (arr == null || arr.length == 0) {
return -1; // Element not found
}
int left = 0;
int right = arr.length - 1;
while (left <= right) {
// Calculate middle index (avoid integer overflow)
int mid = left + (right - left) / 2;
// Check if target is present at mid
if (arr[mid] == target) {
return mid;
}
// If target is greater, search the right half
if (arr[mid] < target) {
left = mid + 1;
}
// If target is smaller, search the left half
else {
right = mid - 1;
}
}
// Element not found
return -1;
}
Use a while loop to narrow down the search range
The key steps in the binary search algorithm:
Consider empty arrays, unsorted arrays, and not found scenarios
mid = left + (right - left) / 2
instead of mid = (left + right) / 2
to prevent integer overflowLet's visualize a binary search for target value 42 in array [5, 13, 17, 25, 39, 42, 53, 88]:
Step | Left | Mid | Right | Comparison | Action |
---|---|---|---|---|---|
1 | 0 | 3 | 7 | 25 < 42 | Search right half |
2 | 4 | 5 | 7 | 42 = 42 | Found at index 5! |
Result: Target found at index 5
Binary search can also be implemented recursively:
public int binarySearchRecursive(int[] arr, int target) {
return binarySearchHelper(arr, target, 0, arr.length - 1);
}
private int binarySearchHelper(int[] arr, int target, int left, int right) {
// Base case: element not found
if (left > right) {
return -1;
}
// Calculate middle index
int mid = left + (right - left) / 2;
// Check if target is present at mid
if (arr[mid] == target) {
return mid;
}
// If target is greater, search the right half
if (arr[mid] < target) {
return binarySearchHelper(arr, target, mid + 1, right);
}
// If target is smaller, search the left half
return binarySearchHelper(arr, target, left, mid - 1);
}
Implement a linear search algorithm to find the index of a given element in an array.
public int linearSearch(int[] arr, int target) {
// Your implementation here
// Return the index of the target element or -1 if not found
}
Implement a binary search algorithm to find the index of a given element in a sorted array.
public int binarySearch(int[] arr, int target) {
// Your implementation here
// Return the index of the target element or -1 if not found
}
Implement a function to find the first and last occurrence of a value in a sorted array.
public int[] searchRange(int[] nums, int target) {
// Your implementation here
// Return [firstIndex, lastIndex] or [-1, -1] if not found
}
Hint: Modify the binary search algorithm to find the leftmost and rightmost occurrences.
Implement a search function for a target value in a rotated sorted array (e.g., [4,5,6,7,0,1,2]).
public int search(int[] nums, int target) {
// Your implementation here
// Return the index of target or -1 if not found
}
Hint: Determine which half of the array is normally ordered, then check if the target is in that half.
Implement a jump search algorithm that works by jumping ahead by fixed steps and then using linear search.
public int jumpSearch(int[] arr, int target) {
// Your implementation here
// Return the index of target or -1 if not found
}
Hint: Jump size should be approximately √n where n is the array length.