Linear Search in a 3D Array with Java
Linear Search, a versatile algorithm for element retrieval, becomes a fascinating exploration when applied to three-dimensional (3D) arrays. In this guide, we’ll dive into the intricacies of implementing Linear Search in a 3D array using Java, unravelling the complexities of navigating through multi-dimensional data structures.
Understanding 3D Array Linear Search:
Searching for a specific element in a 3D array involves traversing not only rows and columns but also the depth of the array. Systematically check each element in a 3D array until you find the desired target or explore the entire array. This algorithmic approach sets the groundwork for efficient searching in multidimensional datasets.
Linear Search in a 3D Array Program:
public class LinearSearch3D {
// Method: Perform Linear Search in a 3D array
public static int[] linearSearch3D(int[][][] array, int target) {
// Loop through layers
for (int i = 0; i < array.length; i++) {
// Loop through rows in each layer
for (int j = 0; j < array[i].length; j++) {
// Loop through columns in each row
for (int k = 0; k < array[i][j].length; k++) {
if (array[i][j][k] == target) {
// Return indices when finding the target element
return new int[]{i, j, k};
}
}
}
}
// Return {-1, -1, -1} if the target element is not found in the 3D array
return new int[]{-1, -1, -1};
}
// Main method: Test Linear Search in 3D array
public static void main(String[] args) {
// Example 3D array
int[][][] array3D = {
{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}
};
int targetElement = 11; // Target element for demonstration
// Perform Linear Search and store the result in the 'result' array
int[] result = linearSearch3D(array3D, targetElement);
// Targeted element
System.out.println("Element to find: "+targetElement);
// Check if the target element was found and print the result accordingly
if (result[0] != -1) {
System.out.println("Found element " + targetElement + " at position: (" + result[0] + ", " + result[1] + ", " + result[2] + ")");
} else {
System.out.println("Element " + targetElement + " not found in the 3D array");
}
}
}Breaking Down the Implementation:
Let’s break down the Java program that implements Linear Search in a 3D array:
Method linearSearch3D:
- This method performs Linear Search in a 3D array.
- It takes a 3D array (
array) and a target element (target) as parameters. - The method uses nested loops to traverse through layers (
i), rows (j), and columns (k) of the 3D array. - If the target element is found, it returns an array containing the indices (
new int[]{i, j, k}). - If the algorithm doesn’t find the target element, it returns {-1, -1, -1}.
Main Method:
- In the
mainmethod, an example 3D array (array3D) is declared and initialized. - The target element (
targetElement) is set to 11 for demonstration purposes. - The
linearSearch3Dmethod is called with the 3D array and the target element, and the result is stored in theresultarray. - The program then checks if the first index of the result is not -1. If the condition is true, the program prints the position where it found the element. If false, it prints a message indicating the element’s absence in the 3D array.
Output:
Element to find: 11
Found element 11 at position: (1, 1, 1)Conclusion:
Implementing Linear Search in a 3D array opens up new dimensions of understanding array manipulation in Java. This foundational knowledge lays the groundwork for more complex algorithms and efficient data retrieval in multidimensional scenarios. Happy coding!
Latest Posts:
- Minimum Number of Operations to Make Array EqualTitle: Minimum Number of Operations to Make Array Equal In this guide, you’ll learn how to solve the Minimum Number… Read more: Minimum Number of Operations to Make Array Equal
- Minimum Number of Deletions to Make an Array SortedTitle: Minimum Number of Deletions to Make an Array Sorted Sorting problems often hide a subtler question: what is the… Read more: Minimum Number of Deletions to Make an Array Sorted
- Minimum Number of Swaps to Bring All Elements Less Than K TogetherTitle: Minimum Number of Swaps to Bring All Elements Less Than K Together In this guide, you’ll learn how to… Read more: Minimum Number of Swaps to Bring All Elements Less Than K Together
- Minimum Number of Swaps to Sort an ArrayTitle: Minimum Number of Swaps to Sort an Array In this guide, you’ll learn how to find the minimum number… Read more: Minimum Number of Swaps to Sort an Array
- Split Array into Three Equal Sum PartsTitle: Split Array into Three Equal Sum Parts Splitting an array into three parts with equal sums is a classic… Read more: Split Array into Three Equal Sum Parts
- Partition Array into K Equal Sum SubsetsTitle: Partition Array into K Equal Sum Subsets In this guide, you’ll learn how to solve the Partition Array into… Read more: Partition Array into K Equal Sum Subsets



